Skip to content

Commit 034db52

Browse files
authored
Merge pull request github#13986 from michaelnebel/csharp/refactordepencyfetching
C#: Re-factor dependency fetching into a separate project.
2 parents d109637 + 4fb73ea commit 034db52

File tree

24 files changed

+241
-146
lines changed

24 files changed

+241
-146
lines changed

csharp/CSharp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction.CSharp",
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction.CIL", "extractor\Semmle.Extraction.CIL\Semmle.Extraction.CIL.csproj", "{399A1579-68F0-40F4-9A23-F241BA697F9C}"
1313
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction.CSharp.DependencyFetching", "extractor\Semmle.Extraction.CSharp.DependencyFetching\Semmle.Extraction.CSharp.DependencyFetching.csproj", "{541D1AC5-E42C-4AB2-A1A4-C2355CE2A2EF}"
15+
EndProject
1416
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction.CSharp.Standalone", "extractor\Semmle.Extraction.CSharp.Standalone\Semmle.Extraction.CSharp.Standalone.csproj", "{D00E7D25-0FA0-48EC-B048-CD60CE1B30D8}"
1517
EndProject
1618
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction.CIL.Driver", "extractor\Semmle.Extraction.CIL.Driver\Semmle.Extraction.CIL.Driver.csproj", "{EFA400B3-C1CE-446F-A4E2-8B44E61EF47C}"
@@ -49,6 +51,10 @@ Global
4951
{399A1579-68F0-40F4-9A23-F241BA697F9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
5052
{399A1579-68F0-40F4-9A23-F241BA697F9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
5153
{399A1579-68F0-40F4-9A23-F241BA697F9C}.Release|Any CPU.Build.0 = Release|Any CPU
54+
{541D1AC5-E42C-4AB2-A1A4-C2355CE2A2EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
55+
{541D1AC5-E42C-4AB2-A1A4-C2355CE2A2EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
56+
{541D1AC5-E42C-4AB2-A1A4-C2355CE2A2EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
57+
{541D1AC5-E42C-4AB2-A1A4-C2355CE2A2EF}.Release|Any CPU.Build.0 = Release|Any CPU
5258
{D00E7D25-0FA0-48EC-B048-CD60CE1B30D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
5359
{D00E7D25-0FA0-48EC-B048-CD60CE1B30D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
5460
{D00E7D25-0FA0-48EC-B048-CD60CE1B30D8}.Release|Any CPU.ActiveCfg = Release|Any CPU

csharp/extractor/Semmle.Extraction.CSharp.Standalone/AssemblyCache.cs renamed to csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/AssemblyCache.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
1+
using System;
2+
using System.Collections.Generic;
33
using System.IO;
4-
using System;
4+
using System.Linq;
55

6-
namespace Semmle.BuildAnalyser
6+
namespace Semmle.Extraction.CSharp.DependencyFetching
77
{
88
/// <summary>
99
/// Manages the set of assemblies.

csharp/extractor/Semmle.Extraction.CSharp.Standalone/AssemblyInfo.cs renamed to csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Security.Cryptography;
77
using System.Text;
88

9-
namespace Semmle.BuildAnalyser
9+
namespace Semmle.Extraction.CSharp.DependencyFetching
1010
{
1111
/// <summary>
1212
/// Stores information about an assembly file (DLL).
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace Semmle.BuildAnalyser
3+
namespace Semmle.Extraction.CSharp.DependencyFetching
44
{
55
public class AssemblyLoadException : Exception { }
66
}

csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs renamed to csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
using Semmle.Util;
2-
using System;
1+
using System;
2+
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6-
using Semmle.Extraction.CSharp.Standalone;
7-
using System.Threading.Tasks;
8-
using System.Collections.Concurrent;
9-
using System.Text;
106
using System.Security.Cryptography;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Semmle.Util;
10+
using Semmle.Util.Logging;
1111

12-
namespace Semmle.BuildAnalyser
12+
namespace Semmle.Extraction.CSharp.DependencyFetching
1313
{
1414
/// <summary>
1515
/// Main implementation of the build analysis.
1616
/// </summary>
17-
internal sealed class BuildAnalysis : IDisposable
17+
public sealed class DependencyManager : IDisposable
1818
{
1919
private readonly AssemblyCache assemblyCache;
2020
private readonly ProgressMonitor progressMonitor;
@@ -25,25 +25,25 @@ internal sealed class BuildAnalysis : IDisposable
2525
private int succeededProjects;
2626
private readonly string[] allSources;
2727
private int conflictedReferences = 0;
28-
private readonly Options options;
28+
private readonly IDependencyOptions options;
2929
private readonly DirectoryInfo sourceDir;
3030
private readonly DotNet dotnet;
3131
private readonly FileContent fileContent;
3232
private readonly TemporaryDirectory packageDirectory;
3333

3434

3535
/// <summary>
36-
/// Performs a C# build analysis.
36+
/// Performs C# dependency fetching.
3737
/// </summary>
38-
/// <param name="options">Analysis options from the command line.</param>
39-
/// <param name="progressMonitor">Display of analysis progress.</param>
40-
public BuildAnalysis(Options options, ProgressMonitor progressMonitor)
38+
/// <param name="options">Dependency fetching options</param>
39+
/// <param name="logger">Logger for dependency fetching progress.</param>
40+
public DependencyManager(string srcDir, IDependencyOptions options, ILogger logger)
4141
{
4242
var startTime = DateTime.Now;
4343

4444
this.options = options;
45-
this.progressMonitor = progressMonitor;
46-
this.sourceDir = new DirectoryInfo(options.SrcDir);
45+
this.progressMonitor = new ProgressMonitor(logger);
46+
this.sourceDir = new DirectoryInfo(srcDir);
4747

4848
try
4949
{
@@ -55,7 +55,7 @@ public BuildAnalysis(Options options, ProgressMonitor progressMonitor)
5555
throw;
5656
}
5757

58-
this.progressMonitor.FindingFiles(options.SrcDir);
58+
this.progressMonitor.FindingFiles(srcDir);
5959

6060
packageDirectory = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName));
6161

@@ -106,7 +106,7 @@ public BuildAnalysis(Options options, ProgressMonitor progressMonitor)
106106
{
107107
Restore(solutions);
108108
Restore(allProjects);
109-
DownloadMissingPackages(allProjects);
109+
DownloadMissingPackages();
110110
}
111111
}
112112

@@ -143,6 +143,8 @@ public BuildAnalysis(Options options, ProgressMonitor progressMonitor)
143143
DateTime.Now - startTime);
144144
}
145145

146+
public DependencyManager(string srcDir) : this(srcDir, DependencyOptions.Default, new ConsoleLogger(Verbosity.Info)) { }
147+
146148
private IEnumerable<string> GetFiles(string pattern, bool recurseSubdirectories = true)
147149
{
148150
return sourceDir.GetFiles(pattern, new EnumerationOptions { RecurseSubdirectories = recurseSubdirectories, MatchCasing = MatchCasing.CaseInsensitive })
@@ -285,7 +287,7 @@ private void AnalyseProject(FileInfo project)
285287

286288
try
287289
{
288-
var csProj = new Extraction.CSharp.CsProjFile(project);
290+
var csProj = new CsProjFile(project);
289291

290292
foreach (var @ref in csProj.References)
291293
{
@@ -329,8 +331,7 @@ private void Restore(IEnumerable<string> targets, string? pathToNugetConfig = nu
329331
}
330332
}
331333

332-
333-
private void DownloadMissingPackages(IEnumerable<string> restoreTargets)
334+
private void DownloadMissingPackages()
334335
{
335336
var nugetConfigs = GetFiles("nuget.config", recurseSubdirectories: true).ToArray();
336337
string? nugetConfig = null;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System.Linq;
2+
using System.Collections.Generic;
3+
4+
namespace Semmle.Extraction.CSharp.DependencyFetching
5+
{
6+
/// <summary>
7+
/// Dependency fetching related options.
8+
/// </summary>
9+
public interface IDependencyOptions
10+
{
11+
/// <summary>
12+
/// Directories to search DLLs in.
13+
/// </summary>
14+
IList<string> DllDirs { get; }
15+
16+
/// <summary>
17+
/// Files/patterns to exclude.
18+
/// </summary>
19+
IList<string> Excludes { get; }
20+
21+
/// <summary>
22+
/// Whether to analyse NuGet packages.
23+
/// </summary>
24+
bool UseNuGet { get; }
25+
26+
/// <summary>
27+
/// The solution file to analyse, or null if not specified.
28+
/// </summary>
29+
string? SolutionFile { get; }
30+
31+
/// <summary>
32+
/// Whether to use the packaged dotnet runtime.
33+
/// </summary>
34+
bool UseSelfContainedDotnet { get; }
35+
36+
/// <summary>
37+
/// Whether to search the .Net framework directory.
38+
/// </summary>
39+
bool ScanNetFrameworkDlls { get; }
40+
41+
/// <summary>
42+
/// Whether to use mscorlib as a reference.
43+
/// </summary>
44+
bool UseMscorlib { get; }
45+
46+
/// <summary>
47+
/// Determine whether the given path should be excluded.
48+
/// </summary>
49+
/// <param name="path">The path to query.</param>
50+
/// <returns>True iff the path matches an exclusion.</returns>
51+
bool ExcludesFile(string path);
52+
}
53+
54+
public class DependencyOptions : IDependencyOptions
55+
{
56+
public static IDependencyOptions Default => new DependencyOptions();
57+
58+
public IList<string> DllDirs { get; set; } = new List<string>();
59+
60+
public IList<string> Excludes { get; set; } = new List<string>();
61+
62+
public bool UseNuGet { get; set; } = true;
63+
64+
public string? SolutionFile { get; set; }
65+
66+
public bool UseSelfContainedDotnet { get; set; } = false;
67+
68+
public bool ScanNetFrameworkDlls { get; set; } = true;
69+
70+
public bool UseMscorlib { get; set; } = true;
71+
72+
public bool ExcludesFile(string path) =>
73+
Excludes.Any(path.Contains);
74+
}
75+
}

csharp/extractor/Semmle.Extraction.CSharp.Standalone/DotNet.cs renamed to csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Diagnostics;
44
using Semmle.Util;
55

6-
namespace Semmle.BuildAnalyser
6+
namespace Semmle.Extraction.CSharp.DependencyFetching
77
{
88
internal interface IDotNet
99
{

csharp/extractor/Semmle.Extraction.CSharp.Standalone/FileContent.cs renamed to csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using Semmle.Util;
21
using System;
32
using System.Collections.Generic;
43
using System.IO;
54
using System.Linq;
65
using System.Text.RegularExpressions;
6+
using Semmle.Util;
77

8-
namespace Semmle.BuildAnalyser
8+
namespace Semmle.Extraction.CSharp.DependencyFetching
99
{
1010

1111
// <summary>
@@ -145,22 +145,22 @@ private void DoInitialize()
145145
[GeneratedRegex("<(.*\\s)?Project.*\\sSdk=\"(.*?)\".*/?>", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)]
146146
private static partial Regex ProjectSdk();
147147
}
148-
}
149148

150-
internal interface IUnsafeFileReader
151-
{
152-
IEnumerable<string> ReadLines(string file);
153-
}
149+
internal interface IUnsafeFileReader
150+
{
151+
IEnumerable<string> ReadLines(string file);
152+
}
154153

155-
internal class UnsafeFileReader : IUnsafeFileReader
156-
{
157-
public IEnumerable<string> ReadLines(string file)
154+
internal class UnsafeFileReader : IUnsafeFileReader
158155
{
159-
using var sr = new StreamReader(file);
160-
string? line;
161-
while ((line = sr.ReadLine()) != null)
156+
public IEnumerable<string> ReadLines(string file)
162157
{
163-
yield return line;
158+
using var sr = new StreamReader(file);
159+
string? line;
160+
while ((line = sr.ReadLine()) != null)
161+
{
162+
yield return line;
163+
}
164164
}
165165
}
166166
}

csharp/extractor/Semmle.Extraction.CSharp.Standalone/NugetPackages.cs renamed to csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackages.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using Semmle.Util;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Diagnostics;
54
using System.IO;
65
using System.Linq;
6+
using Semmle.Util;
77

8-
namespace Semmle.BuildAnalyser
8+
namespace Semmle.Extraction.CSharp.DependencyFetching
99
{
1010
/// <summary>
1111
/// Manage the downloading of NuGet packages.

csharp/extractor/Semmle.Extraction.CSharp.Standalone/ProgressMonitor.cs renamed to csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/ProgressMonitor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using Semmle.Util.Logging;
2-
using System;
1+
using System;
2+
using Semmle.Util.Logging;
33

4-
namespace Semmle.BuildAnalyser
4+
namespace Semmle.Extraction.CSharp.DependencyFetching
55
{
66
internal class ProgressMonitor
77
{

0 commit comments

Comments
 (0)