Skip to content

Commit dfb8761

Browse files
authored
C#: Add flag to Standalone extractor to use the self contained .Net framework (#4233)
1 parent e91d321 commit dfb8761

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public BuildAnalysis(Options options, IProgressMonitor progress)
8888
nuget = new NugetPackages(sourceDir.FullName, PackageDirectory);
8989
ReadNugetFiles();
9090
}
91-
catch(FileNotFoundException)
91+
catch (FileNotFoundException)
9292
{
9393
progressMonitor.MissingNuGet();
9494
}
@@ -97,7 +97,9 @@ public BuildAnalysis(Options options, IProgressMonitor progress)
9797
// Find DLLs in the .Net Framework
9898
if (options.ScanNetFrameworkDlls)
9999
{
100-
dllDirNames.Add(Runtime.Runtimes.First());
100+
var runtimeLocation = Runtime.GetRuntime(options.UseSelfContainedDotnet);
101+
progressMonitor.Log(Util.Logging.Severity.Debug, $"Runtime location selected: {runtimeLocation}");
102+
dllDirNames.Add(runtimeLocation);
101103
}
102104

103105
// These files can sometimes prevent `dotnet restore` from working correctly.
@@ -279,7 +281,7 @@ void AnalyseProjectFiles(IEnumerable<FileInfo> projectFiles)
279281

280282
void AnalyseProject(FileInfo project)
281283
{
282-
if(!project.Exists)
284+
if (!project.Exists)
283285
{
284286
progressMonitor.MissingProject(project.FullName);
285287
return;
@@ -323,7 +325,7 @@ void AnalyseProject(FileInfo project)
323325
void Restore(string projectOrSolution)
324326
{
325327
int exit = DotNet.RestoreToDirectory(projectOrSolution, PackageDirectory.DirInfo.FullName);
326-
switch(exit)
328+
switch (exit)
327329
{
328330
case 0:
329331
case 1:
@@ -342,7 +344,7 @@ public void RestoreSolutions(IEnumerable<string> solutions)
342344

343345
public void AnalyseSolutions(IEnumerable<string> solutions)
344346
{
345-
Parallel.ForEach(solutions, new ParallelOptions { MaxDegreeOfParallelism = 4 } , solutionFile =>
347+
Parallel.ForEach(solutions, new ParallelOptions { MaxDegreeOfParallelism = 4 }, solutionFile =>
346348
{
347349
try
348350
{

csharp/extractor/Semmle.Extraction.CSharp.Standalone/Options.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public sealed class Options : CommonOptions
1313
{
1414
public override bool handleFlag(string key, bool value)
1515
{
16-
switch(key)
16+
switch (key)
1717
{
1818
case "silent":
1919
Verbosity = value ? Verbosity.Off : Verbosity.Info;
@@ -36,14 +36,17 @@ public override bool handleFlag(string key, bool value)
3636
case "skip-dotnet":
3737
ScanNetFrameworkDlls = !value;
3838
return true;
39+
case "self-contained-dotnet":
40+
UseSelfContainedDotnet = value;
41+
return true;
3942
default:
4043
return base.handleFlag(key, value);
4144
}
4245
}
4346

4447
public override bool handleOption(string key, string value)
4548
{
46-
switch(key)
49+
switch (key)
4750
{
4851
case "exclude":
4952
Excludes.Add(value);
@@ -134,6 +137,11 @@ public override void invalidArgument(string argument)
134137
/// </summary>
135138
public bool Help = false;
136139

140+
/// <summary>
141+
/// Whether to use the packaged dotnet runtime.
142+
/// </summary>
143+
public bool UseSelfContainedDotnet = false;
144+
137145
/// <summary>
138146
/// Determine whether the given path should be excluded.
139147
/// </summary>
@@ -162,6 +170,7 @@ public void ShowHelp(System.IO.TextWriter output)
162170
output.WriteLine(" --threads:nnn Specify number of threads (default=CPU cores)");
163171
output.WriteLine(" --verbose Produce more output");
164172
output.WriteLine(" --pdb Cross-reference information from PDBs where available");
173+
output.WriteLine(" --self-contained-dotnet Use the .Net Framework packaged with the extractor");
165174
}
166175

167176
private Options()

csharp/extractor/Semmle.Extraction.CSharp.Standalone/ProgressMonitor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface IProgressMonitor
1616
void NugetInstall(string package);
1717
void ResolvedReference(string filename);
1818
void Summary(int existingSources, int usedSources, int missingSources, int references, int unresolvedReferences, int resolvedConflicts, int totalProjects, int failedProjects, TimeSpan analysisTime);
19-
void Warning(string message);
19+
void Log(Severity severity, string message);
2020
void ResolvedConflict(string asm1, string asm2);
2121
void MissingProject(string projectFile);
2222
void CommandFailed(string exe, string arguments, int exitCode);
@@ -93,9 +93,9 @@ public void Summary(int existingSources, int usedSources, int missingSources,
9393
logger.Log(Severity.Info, "Build analysis completed in {0}", analysisTime);
9494
}
9595

96-
public void Warning(string message)
96+
public void Log(Severity severity, string message)
9797
{
98-
logger.Log(Severity.Warning, message);
98+
logger.Log(severity, message);
9999
}
100100

101101
public void ResolvedConflict(string asm1, string asm2)

csharp/extractor/Semmle.Extraction.CSharp.Standalone/Runtime.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static class Runtime
1717
/// <summary>
1818
/// Locates .NET Core Runtimes.
1919
/// </summary>
20-
public static IEnumerable<string> CoreRuntimes
20+
private static IEnumerable<string> CoreRuntimes
2121
{
2222
get
2323
{
@@ -37,7 +37,7 @@ public static IEnumerable<string> CoreRuntimes
3737
/// Locates .NET Desktop Runtimes.
3838
/// This includes Mono and Microsoft.NET.
3939
/// </summary>
40-
public static IEnumerable<string> DesktopRuntimes
40+
private static IEnumerable<string> DesktopRuntimes
4141
{
4242
get
4343
{
@@ -63,7 +63,12 @@ public static IEnumerable<string> DesktopRuntimes
6363
}
6464
}
6565

66-
public static IEnumerable<string> Runtimes
66+
/// <summary>
67+
/// Gets the .NET runtime location to use for extraction
68+
/// </summary>
69+
public static string GetRuntime(bool useSelfContained) => useSelfContained ? ExecutingRuntime : Runtimes.First();
70+
71+
private static IEnumerable<string> Runtimes
6772
{
6873
get
6974
{

0 commit comments

Comments
 (0)