-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
164 lines (138 loc) · 6.36 KB
/
Copy pathProgram.cs
File metadata and controls
164 lines (138 loc) · 6.36 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
157
158
159
160
161
162
163
164
using System.CommandLine;
using Microsoft.Build.Locator;
using Microsoft.Extensions.DependencyInjection;
using NuGetLicenseCollector.Interfaces;
using NuGetLicenseCollector.Services;
namespace NuGetLicenseCollector;
internal class Program
{
private const string SolutionExtension = ".sln";
private const string CSharpProjectExtension = ".csproj";
private const string VBProjectExtension = ".vbproj";
private const string JsonExtension = ".json";
private const string TextExtension = ".txt";
private static async Task Main(string[] args)
{
var inputFile = new Argument<string>(
name: "input",
description: $"Path to the solution file ({SolutionExtension}) or project file ({CSharpProjectExtension}, {VBProjectExtension})");
var outputOption = new Option<string>(
name: "--output",
description: "Output file path",
getDefaultValue: () => "nuget-licenses.txt");
outputOption.AddAlias("-o");
var jsonOption = new Option<bool>(
name: "--json",
description: "Output in JSON format");
jsonOption.AddAlias("-j");
var forceRefreshOption = new Option<bool>(
name: "--force-refresh",
description: "Clear license cache and download fresh license texts");
forceRefreshOption.AddAlias("-f");
var rootCommand = new RootCommand("NuGet License Collector - Analyze NuGet package licenses in a solution or project")
{
inputFile,
outputOption,
jsonOption,
forceRefreshOption
};
rootCommand.SetHandler(async (inputPath, outputPath, useJson, forceRefresh) =>
{
if (useJson && !outputPath.EndsWith(JsonExtension, StringComparison.OrdinalIgnoreCase))
{
outputPath = Path.ChangeExtension(outputPath, JsonExtension);
}
await ProcessFileAsync(inputPath, outputPath, useJson, forceRefresh);
}, inputFile, outputOption, jsonOption, forceRefreshOption);
await rootCommand.InvokeAsync(args);
}
private static ServiceProvider ConfigureServices(bool forceRefresh)
{
var services = new ServiceCollection();
services.AddHttpClient();
services.AddSingleton<ISolutionAnalyzer, SolutionAnalyzer>();
services.AddSingleton<ILicenseCacheService>(provider =>
{
var httpClientFactory = provider.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient();
return new LicenseCacheService(httpClient);
});
services.AddSingleton<IReportGenerator, ReportGenerator>();
services.AddSingleton<INuGetLicenseAnalyzerService>(provider =>
new NuGetLicenseAnalyzerService(provider.GetRequiredService<ILicenseCacheService>(), forceRefresh));
return services.BuildServiceProvider();
}
private static async Task ProcessFileAsync(string filePath, string outputPath, bool useJson, bool forceRefresh)
{
if (!File.Exists(filePath))
{
Console.WriteLine($"Error: File '{filePath}' not found.");
return;
}
var fileExtension = Path.GetExtension(filePath).ToLowerInvariant();
if (fileExtension != SolutionExtension && fileExtension != CSharpProjectExtension && fileExtension != VBProjectExtension)
{
Console.WriteLine($"Error: Unsupported file type '{fileExtension}'. Supported types: {SolutionExtension}, {CSharpProjectExtension}, {VBProjectExtension}");
return;
}
try
{
// CRITICAL: MSBuildLocator must be registered before using Microsoft.Build APIs
// This enables the tool to locate and use the correct MSBuild assemblies
if (!MSBuildLocator.IsRegistered)
{
MSBuildLocator.RegisterDefaults();
}
Console.WriteLine($"Analyzing file: {filePath}");
Console.WriteLine("This may take a while...");
using var serviceProvider = ConfigureServices(forceRefresh);
var solutionAnalyzer = serviceProvider.GetRequiredService<ISolutionAnalyzer>();
var licenseAnalyzerService = serviceProvider.GetRequiredService<INuGetLicenseAnalyzerService>();
var reportGenerator = serviceProvider.GetRequiredService<IReportGenerator>();
var projectFiles = new List<string>();
if (fileExtension == SolutionExtension)
{
projectFiles = await solutionAnalyzer.GetProjectFilesAsync(filePath);
Console.WriteLine($"Found {projectFiles.Count} projects");
}
else if (fileExtension == CSharpProjectExtension || fileExtension == VBProjectExtension)
{
projectFiles.Add(filePath);
Console.WriteLine($"Analyzing project file: {Path.GetFileName(filePath)}");
}
else
{
Console.WriteLine($"Unsupported file type: {fileExtension}");
return;
}
// Use HashSet to automatically deduplicate packages across multiple projects
var allPackages = new HashSet<string>();
foreach (var projectFile in projectFiles)
{
Console.WriteLine($"Analyzing project: {Path.GetFileName(projectFile)}");
var packages = await solutionAnalyzer.GetPackageReferencesAsync(projectFile);
foreach (var package in packages)
{
allPackages.Add(package);
}
}
Console.WriteLine($"Found {allPackages.Count} unique packages");
var packageInfos = await licenseAnalyzerService.GetPackageInfoAsync(allPackages.ToList());
Console.WriteLine($"Retrieved license information for {packageInfos.Count} packages");
if (useJson)
{
await reportGenerator.GenerateJsonReportAsync(packageInfos, outputPath);
}
else
{
await reportGenerator.GenerateReportAsync(packageInfos, outputPath);
}
Console.WriteLine("Analysis complete!");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
}
}
}