-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
DotnetAnalyzer.cs
293 lines (253 loc) · 11 KB
/
DotnetAnalyzer.cs
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using System.Security.Cryptography;
using Buildalyzer;
using Microsoft.Build.Construction;
namespace Covenant.Analysis.Dotnet;
internal class DotnetAnalyzer : Analyzer
{
private const string DesignTimeBuildFlag = "--design-time-build";
private const string DisableDotnet = "--disable-dotnet";
private readonly IFileSystem _fileSystem;
private readonly IEnvironment _environment;
private readonly IGlobber _globber;
private readonly NuspecLicenseReader _nuspecReader;
private readonly DotnetAssetFileReader _assetFileReader;
private readonly AnalyzerManager _analyzerManager;
private bool _enabled = true;
public override bool Enabled => _enabled;
public override string[] Patterns { get; } = new[] { "**/*.sln" };
public DotnetAnalyzer(
IFileSystem fileSystem, IEnvironment environment, IGlobber globber)
{
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
_globber = globber ?? throw new ArgumentNullException(nameof(globber));
_nuspecReader = new NuspecLicenseReader(_fileSystem);
_assetFileReader = new DotnetAssetFileReader(_fileSystem);
_analyzerManager = new AnalyzerManager();
}
public override void Initialize(ICommandLineAugmentor cli)
{
cli.AddOption<bool>(DesignTimeBuildFlag, "Performs a design time build for .NET projects", false);
cli.AddOption<bool>(DisableDotnet, "Disables the .NET analyzer", false);
}
public override void BeforeAnalysis(AnalysisSettings settings)
{
if (settings.Cli.GetOption<bool>(DisableDotnet))
{
_enabled = false;
}
}
public override bool ShouldTraverse(DirectoryPath path)
{
return !path.Segments.Any(s => s.Equals("bin"))
&& !path.Segments.Any(s => s.Equals("obj"));
}
public override bool CanHandle(AnalysisContext context, FilePath path)
{
var isSolution = path.GetExtension()?.Equals(".sln", StringComparison.OrdinalIgnoreCase) ?? false;
var isCsProject = path.GetExtension()?.Equals(".csproj", StringComparison.OrdinalIgnoreCase) ?? false;
var isFsProject = path.GetExtension()?.Equals(".fsproj", StringComparison.OrdinalIgnoreCase) ?? false;
return isSolution || isCsProject || isFsProject;
}
public override void Analyze(AnalysisContext context, FilePath path)
{
path = path.MakeAbsolute(_environment);
var extension = path.GetExtension() ?? string.Empty;
if (extension.Equals(".sln", StringComparison.OrdinalIgnoreCase))
{
// Analyze solution
var solution = SolutionFile.Parse(path.FullPath);
// Collect all projects
var projects = solution.ProjectsInOrder.Where(
csproj => csproj.ProjectType != SolutionProjectType.SolutionFolder
&& csproj.ProjectType != SolutionProjectType.Unknown);
// Add all components in all projects
var assetFiles = new List<AssetFile>();
foreach (var csproj in projects)
{
var assetsFile = ReadAssetFile(context, new FilePath(csproj.AbsolutePath));
if (assetsFile != null)
{
assetFiles.Add(assetsFile);
AnalyzeProject(context, assetsFile, path.GetFilename().FullPath);
}
}
// Add all dependencies for all components in all projects
foreach (var assetFile in assetFiles)
{
AnalyzeProjectDependencies(context, assetFile);
}
}
else if (extension.Equals(".csproj", StringComparison.OrdinalIgnoreCase) ||
extension.Equals(".fsproj", StringComparison.OrdinalIgnoreCase))
{
var assetsFile = ReadAssetFile(context, path);
if (assetsFile != null)
{
AnalyzeProject(context, assetsFile);
AnalyzeProjectDependencies(context, assetsFile);
}
}
}
private void AnalyzeProject(AnalysisContext context, AssetFile assets, string? group = null)
{
var (version, copyright) = PerformDesignTimeBuild(context, assets);
// Add project as a component
context
.AddComponent(
new DotnetComponent(
assets.Project.Restore.Name,
version ?? assets.Project.Version,
BomComponentKind.Root))
.SetCopyright(copyright)
.AddParent(group);
// Add all libraries
foreach (var library in assets.Libraries)
{
if (library.Type == LibraryType.Project)
{
context
.AddComponent(
new DotnetComponent(
library.Name,
library.Version,
BomComponentKind.Root))
.AddParent(group);
}
else
{
var license = default(BomLicense);
var hash = default(BomHash);
var packageCopyright = default(string);
var packagePath = GetNuGetPackagePath(context, assets, library);
if (packagePath != null)
{
// Calculate the hash
using (var sha = SHA512.Create())
{
using (var stream = _fileSystem.GetFile(packagePath.FullPath).OpenRead())
{
hash = new BomHash(
BomHashAlgorithm.SHA512,
BitConverter.ToString(sha.ComputeHash(stream)).Replace("-", string.Empty));
}
}
// Read the license
if (_nuspecReader.TryReadLicense(packagePath, out var licenseInfo))
{
packageCopyright = licenseInfo.Copyright;
license = licenseInfo.ConvertToBomLicense();
}
else
{
context.AddError($"Could not parse the NuGet package information '{packagePath.FullPath}'");
}
}
context
.AddComponent(
new DotnetComponent(
library.Name,
library.Version))
.SetLicense(license)
.SetCopyright(packageCopyright)
.SetHash(hash);
}
}
}
private static bool AnalyzeProjectDependencies(AnalysisContext context, AssetFile assets)
{
foreach (var target in assets.Targets)
{
using (context.Scope(target.TargetFramework))
{
foreach (var library in target.Libraries)
{
var targetLibraryRef = DotnetComponent.GetBomRef(
library.Name,
library.Version,
library.Type == LibraryType.Project ? BomComponentKind.Root : BomComponentKind.Library);
var targetLibraryComponent = context.Graph.FindByBomRef(targetLibraryRef);
if (targetLibraryComponent == null)
{
context.AddError($"Could not find library '{library.Name}' in graph");
return false;
}
foreach (var dependency in library.Dependencies)
{
// Find the best suitable version
var dependencyComponent = context.Graph.FindDotnetComponent(dependency.Id, dependency.Version);
if (dependencyComponent == null)
{
context.AddWarning(
$"The dependency [yellow]{dependency.Id}[/]@[yellow]{dependency.Version.OriginalString.EscapeMarkup()}[/] " +
$"for [green]{targetLibraryComponent.Name}[/]@[green]{targetLibraryComponent.Version.EscapeMarkup()}[/] was not found.");
continue;
}
// Connect
context.Connect(targetLibraryComponent, dependencyComponent, target.TargetFramework);
}
}
}
}
return true;
}
private AssetFile? ReadAssetFile(AnalysisContext context, FilePath path)
{
path = path.GetDirectory().CombineWithFilePath("obj/project.assets.json");
var assets = _assetFileReader.ReadAssetFile(path);
if (assets == null)
{
context.AddError($"Could not find [yellow]project.assets.json[/] at [yellow]{path}[/]");
return null;
}
return assets;
}
private FilePath? GetNuGetPackagePath(AnalysisContext context, AssetFile assets, Library library)
{
if (library.Path == null)
{
context.AddWarning("Encountered library without a path");
return null;
}
var packagesFolders = new List<DirectoryPath>();
packagesFolders.AddIfNotNull(assets.Project.Restore.PackagesPath);
packagesFolders.AddRange(assets.Project.Restore.FallbackFolders);
foreach (var packagesFolder in packagesFolders)
{
if (!_fileSystem.Exist(packagesFolder))
{
context.AddWarning($"The packages folder '{packagesFolder}' did not exist on disk");
continue;
}
var packageFolder = packagesFolder.Combine(library.Path);
var packagePath = _globber.Match("./*.nupkg", new GlobberSettings { Root = packageFolder }).OfType<FilePath>().SingleOrDefault();
if (packagePath == null)
{
context.AddWarning($"The library folder '{packageFolder}' did not contain a NuSpec");
continue;
}
return packagePath;
}
return null;
}
private (NuGetVersion? Version, string? Copyright) PerformDesignTimeBuild(AnalysisContext context, AssetFile assets)
{
if (!context.Cli.GetOption<bool>(DesignTimeBuildFlag))
{
return (null, null);
}
var analyzer = _analyzerManager.GetProject(assets.Project.Restore.ProjectPath.FullPath);
var result = analyzer.Build().Results.FirstOrDefault();
if (result == null)
{
return (Version: null, Copyright: null);
}
var nugetVersion = default(NuGetVersion);
if (result.Properties.TryGetValue("AssemblyVersion", out var version))
{
nugetVersion = NuGetVersion.Parse(version);
}
result.Properties.TryGetValue("Copyright", out var copyright);
return (nugetVersion, copyright);
}
}