Skip to content

Commit e24fa47

Browse files
committed
C#: Introduce caching or detecting the newest runtimes and fetching all file names in the source dir.
1 parent ddb50b8 commit e24fa47

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ internal sealed partial class BuildAnalysis : IDisposable
2929
private readonly Options options;
3030
private readonly DirectoryInfo sourceDir;
3131
private readonly DotNet dotnet;
32+
private readonly Lazy<IEnumerable<string>> allFiles;
3233

3334
/// <summary>
3435
/// Performs a C# build analysis.
@@ -55,6 +56,7 @@ public BuildAnalysis(Options options, ProgressMonitor progressMonitor)
5556

5657
this.progressMonitor.FindingFiles(options.SrcDir);
5758

59+
this.allFiles = new(() => GetFiles("*.*"));
5860
this.allSources = GetFiles("*.cs").ToArray();
5961
var allProjects = GetFiles("*.csproj");
6062
var solutions = options.SolutionFile is not null
@@ -227,6 +229,11 @@ private void UseReference(string reference) =>
227229
private void UseSource(FileInfo sourceFile) =>
228230
sources[sourceFile.FullName] = sourceFile.Exists;
229231

232+
/// <summary>
233+
/// All files in the source directory.
234+
/// </summary>
235+
private IEnumerable<string> AllFiles => this.allFiles.Value;
236+
230237
/// <summary>
231238
/// The list of resolved reference files.
232239
/// </summary>
@@ -369,8 +376,7 @@ private static bool IsGroupMatch(ReadOnlySpan<char> line, Regex regex, string gr
369376
/// </summary>
370377
private bool UseAspNetDlls()
371378
{
372-
var allFiles = GetFiles("*.*");
373-
foreach (var file in allFiles)
379+
foreach (var file in AllFiles)
374380
{
375381
try
376382
{
@@ -414,8 +420,7 @@ private void DownloadMissingPackages(IEnumerable<string> restoreTargets)
414420
nugetConfig = nugetConfigs.FirstOrDefault();
415421
}
416422

417-
var allFiles = GetFiles("*.*");
418-
foreach (var file in allFiles)
423+
foreach (var file in AllFiles)
419424
{
420425
try
421426
{

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ internal partial class Runtime
1818
private const string aspNetCoreApp = "Microsoft.AspNetCore.App";
1919

2020
private readonly IDotNet dotNet;
21+
private readonly Lazy<Dictionary<string, RuntimeVersion>> newestRuntimes;
22+
private Dictionary<string, RuntimeVersion> NewestRuntimes => newestRuntimes.Value;
2123
private static string ExecutingRuntime => RuntimeEnvironment.GetRuntimeDirectory();
2224

23-
public Runtime(IDotNet dotNet) => this.dotNet = dotNet;
25+
public Runtime(IDotNet dotNet)
26+
{
27+
this.dotNet = dotNet;
28+
this.newestRuntimes = new(GetNewestRuntimes);
29+
}
2430

2531
internal record RuntimeVersion : IComparable<RuntimeVersion>
2632
{
@@ -150,11 +156,8 @@ public string GetRuntime(bool useSelfContained)
150156
return ExecutingRuntime;
151157
}
152158

153-
// Gets the newest version of the installed runtimes.
154-
var newestRuntimes = GetNewestRuntimes();
155-
156159
// Location of the newest .NET Core Runtime.
157-
if (newestRuntimes.TryGetValue(netCoreApp, out var netCoreVersion))
160+
if (NewestRuntimes.TryGetValue(netCoreApp, out var netCoreVersion))
158161
{
159162
return netCoreVersion.FullPath;
160163
}
@@ -168,16 +171,17 @@ public string GetRuntime(bool useSelfContained)
168171
return ExecutingRuntime;
169172
}
170173

174+
/// <summary>
175+
/// Gets the ASP.NET runtime location to use for extraction, if one exists.
176+
/// </summary>
171177
public string? GetAspRuntime()
172178
{
173-
var newestRuntimes = GetNewestRuntimes();
174-
175179
// Location of the newest ASP.NET Core Runtime.
176-
if (newestRuntimes.TryGetValue(aspNetCoreApp, out var aspNetCoreVersion))
180+
if (NewestRuntimes.TryGetValue(aspNetCoreApp, out var aspNetCoreVersion))
177181
{
178182
return aspNetCoreVersion.FullPath;
179183
}
180184
return null;
181185
}
182186
}
183-
}
187+
}

0 commit comments

Comments
 (0)