-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
156 lines (123 loc) · 4.86 KB
/
Copy pathProgram.cs
File metadata and controls
156 lines (123 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using CliWrap;
using CliWrap.Buffered;
namespace PackageUsage;
public class Program
{
public const string CommandName = "package-usage";
public const string OnlyDifferentPacksSwitch = "--only-different";
public static async Task Main(string[] args)
{
_ = args switch
{
["-h", ..] => PrintUsage(),
[] => await PrintPackages(),
[OnlyDifferentPacksSwitch] => await PrintPackages(null, true),
[_] => await PrintPackages(args[0]),
[_, OnlyDifferentPacksSwitch] => await PrintPackages(args[0], true),
_ => PrintUsage(),
};
}
private static async Task<object?> PrintPackages(string? solutionPath = null, bool onlyDifferent = false)
{
if (solutionPath is null)
{
var solutions = new DirectoryInfo(Environment.CurrentDirectory).EnumerateFiles().Where(f => f.Extension is ".sln");
if (!solutions.Any())
{
Console.WriteLine("No .sln files found here");
return null;
}
if (solutions.Count() > 1)
{
Console.WriteLine("Multiple .sln files found here");
}
solutionPath = solutions.First().FullName;
}
List<string> dotnetListOutput = await GetDotnetOutputAsync(solutionPath);
List<PackageInfo> projectPacks = GetPackageList(dotnetListOutput);
var groups = projectPacks.OrderBy(pack => pack.Name)
.ThenBy(pack => pack.ResolvedVersion)
.GroupBy(pack => pack.Name)
.OrderBy(group => group.Key)
.AsEnumerable();
if (onlyDifferent)
groups = groups.Where(g => g.DistinctBy(x => x.ResolvedVersion).Count() > 1);
DisplayResults(groups);
return null;
}
private static object? PrintUsage()
{
Console.WriteLine("Displays all packages in the solution in the current directory and prints projects in which each package is used, including different versions of the same package.");
Console.WriteLine();
Console.WriteLine($"{CommandName} [.sln path] [{OnlyDifferentPacksSwitch}]");
Console.WriteLine();
Console.WriteLine(OnlyDifferentPacksSwitch + "\t Show only packages which have different versions in multiple projects.");
return null;
}
private static void DisplayResults(IEnumerable<IGrouping<string, PackageInfo>> groups)
{
foreach (var group in groups)
{
Console.WriteLine(group.Key);
foreach (var pack in group)
{
Console.WriteLine($"{pack.ResolvedVersion,10}, {pack.Project}");
}
Console.WriteLine();
}
}
private static List<PackageInfo> GetPackageList(List<string> dotnetListOutput)
{
var projectPacks = new List<PackageInfo>();
dotnetListOutput = dotnetListOutput
.Where(s => !string.IsNullOrEmpty(s))
.Where(s => !char.IsWhiteSpace(s[0]) || s.Contains('>')).ToList();
var currentProjectName = string.Empty;
foreach (var line in dotnetListOutput)
{
if (char.IsLetter(line[0]))
{
currentProjectName = line.Split(' ')[1][1..^1];
}
else if (line.Contains('>'))
{
var values = line.Split(new[] { '>', ' ', '\t', ';' }, StringSplitOptions.RemoveEmptyEntries);
projectPacks.Add(new PackageInfo(values[0], currentProjectName, values[1], values[2]));
}
}
return projectPacks;
}
private static async Task<List<string>> GetDotnetOutputAsync(string solutionPath)
{
var processResultTask = Cli
.Wrap("dotnet")
.WithArguments(new[] { "list", solutionPath, "package" })
.ExecuteBufferedAsync();
Console.CursorVisible = false;
foreach (var symbol in LoadingSymbols())
{
if (processResultTask.Task.IsCompleted)
{
break;
}
Console.Write(symbol);
Console.CursorLeft = 0;
await Task.Delay(200);
}
Console.CursorVisible = true;
var processResult = await processResultTask;
var outputLines = processResult.StandardOutput.Split(Environment.NewLine).ToList();
return outputLines;
}
private static IEnumerable<char> LoadingSymbols()
{
while (true)
{
yield return '/';
yield return '-';
yield return '\\';
yield return '|';
}
}
}
record PackageInfo(string Name, string Project, string DemandedVersion, string ResolvedVersion);