Skip to content

Commit c0d1179

Browse files
committed
C#: Minimal update of relevant code to minimize project dependencies and hide some implementation details behind interfaces.
1 parent f47e59d commit c0d1179

File tree

23 files changed

+178
-144
lines changed

23 files changed

+178
-144
lines changed

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.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).
Lines changed: 1 addition & 1 deletion
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.DependencyFetching/DependencyManager.cs

Lines changed: 17 additions & 18 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 a 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

@@ -285,7 +285,7 @@ private void AnalyseProject(FileInfo project)
285285

286286
try
287287
{
288-
var csProj = new Extraction.CSharp.CsProjFile(project);
288+
var csProj = new CsProjFile(project);
289289

290290
foreach (var @ref in csProj.References)
291291
{
@@ -329,7 +329,6 @@ private void Restore(IEnumerable<string> targets, string? pathToNugetConfig = nu
329329
}
330330
}
331331

332-
333332
private void DownloadMissingPackages()
334333
{
335334
var nugetConfigs = GetFiles("nuget.config", recurseSubdirectories: true).ToArray();
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 IList<string> DllDirs { get; set; } = new List<string>();
57+
58+
public IList<string> Excludes { get; set; } = new List<string>();
59+
60+
public bool UseNuGet { get; set; } = true;
61+
62+
public string? SolutionFile { get; set; }
63+
64+
public bool UseSelfContainedDotnet { get; set; } = false;
65+
66+
public bool ScanNetFrameworkDlls { get; set; } = true;
67+
68+
public bool UseMscorlib { get; set; } = true;
69+
70+
public bool ExcludesFile(string path) =>
71+
Excludes.Any(path.Contains);
72+
}
73+
}

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.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.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.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
{

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Properties/AssemblyInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reflection;
2+
using System.Runtime.CompilerServices;
23
using System.Runtime.InteropServices;
34

45
// General Information about an assembly is controlled through the following
@@ -21,6 +22,9 @@
2122
// The following GUID is for the ID of the typelib if this project is exposed to COM
2223
[assembly: Guid("8e902d1e-f639-4f9f-a6d2-71e8ade7c5a3")]
2324

25+
// Expose internals for testing purposes.
26+
[assembly: InternalsVisibleTo("Semmle.Extraction.Tests")]
27+
2428
// Version information for an assembly consists of the following four values:
2529
//
2630
// Major Version

0 commit comments

Comments
 (0)