Skip to content

Commit 9eb1383

Browse files
committed
C#: Code quality improvements.
1 parent 2bea927 commit 9eb1383

File tree

4 files changed

+29
-29
lines changed

4 files changed

+29
-29
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/AssemblyCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ internal class AssemblyCache
1919
/// assembly cache.
2020
/// </param>
2121
/// <param name="logger">Callback for progress.</param>
22-
public AssemblyCache(IEnumerable<AssemblyPath> paths, IEnumerable<string> frameworkPaths, ILogger logger)
22+
public AssemblyCache(IEnumerable<AssemblyLookupLocation> paths, IEnumerable<string> frameworkPaths, ILogger logger)
2323
{
2424
this.logger = logger;
2525
foreach (var path in paths)
2626
{
27-
path.Process(dllsToIndex, logger);
27+
dllsToIndex.AddRange(path.GetDlls(logger));
2828
}
2929
IndexReferences(frameworkPaths);
3030
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/AssemblyPath.cs renamed to csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/AssemblyLookupLocation.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
99
/// Used to represent a path to an assembly or a directory containing assemblies
1010
/// and a selector function to determine which files to include, when indexing the assemblies.
1111
/// </summary>
12-
internal sealed class AssemblyPath(string p, Func<string, bool> includeFileName)
12+
internal sealed class AssemblyLookupLocation(string p, Func<string, bool> includeFileName)
1313
{
1414
public string Path => p;
1515

16-
public AssemblyPath(string p) : this(p, _ => true) { }
16+
public AssemblyLookupLocation(string p) : this(p, _ => true) { }
1717

18-
public static implicit operator AssemblyPath(string path) => new(path);
18+
public static implicit operator AssemblyLookupLocation(string path) => new(path);
1919

2020
/// <summary>
2121
/// Finds all assemblies nested within the directory `path`
@@ -33,19 +33,18 @@ private void AddReferenceDirectory(List<string> dllsToIndex, ILogger logger)
3333
}
3434
else
3535
{
36-
logger.LogInfo($"AssemblyPath: Skipping {dll.FullName}.");
36+
logger.LogInfo($"AssemblyLookupLocation: Skipping {dll.FullName}.");
3737
}
3838
}
3939
}
4040

4141
/// <summary>
42-
/// Finds all assemblies in `p` that should be indexed and adds them to
43-
/// the list of assembly names to index.
42+
/// Returns a list of paths to all assemblies in `p` that should be indexed.
4443
/// </summary>
45-
/// <param name="dllsToIndex">List of assembly names to index.</param>
4644
/// <param name="logger">Logger</param>
47-
public void Process(List<string> dllsToIndex, ILogger logger)
45+
public List<string> GetDlls(ILogger logger)
4846
{
47+
var dllsToIndex = new List<string>();
4948
if (File.Exists(p))
5049
{
5150
if (includeFileName(System.IO.Path.GetFileName(p)))
@@ -54,24 +53,25 @@ public void Process(List<string> dllsToIndex, ILogger logger)
5453
}
5554
else
5655
{
57-
logger.LogInfo($"AssemblyPath: Skipping {p}.");
56+
logger.LogInfo($"AssemblyLookupLocation: Skipping {p}.");
5857
}
59-
return;
58+
return dllsToIndex;
6059
}
6160

6261
if (Directory.Exists(p))
6362
{
64-
logger.LogInfo($"AssemblyPath: Finding reference DLLs in {p}...");
63+
logger.LogInfo($"AssemblyLookupLocation: Finding reference DLLs in {p}...");
6564
AddReferenceDirectory(dllsToIndex, logger);
6665
}
6766
else
6867
{
69-
logger.LogInfo("AssemblyCache: Path not found: " + p);
68+
logger.LogInfo("AssemblyLookupLocation: Path not found: " + p);
7069
}
70+
return dllsToIndex;
7171
}
7272

7373
public override bool Equals(object? obj) =>
74-
obj is AssemblyPath ap && p.Equals(ap.Path);
74+
obj is AssemblyLookupLocation ap && p.Equals(ap.Path);
7575

7676
public override int GetHashCode() => p.GetHashCode();
7777

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.Nuget.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
1212
{
1313
public sealed partial class DependencyManager
1414
{
15-
private void RestoreNugetPackages(List<FileInfo> allNonBinaryFiles, IEnumerable<string> allProjects, IEnumerable<string> allSolutions, HashSet<AssemblyPath> dllPaths)
15+
private void RestoreNugetPackages(List<FileInfo> allNonBinaryFiles, IEnumerable<string> allProjects, IEnumerable<string> allSolutions, HashSet<AssemblyLookupLocation> dllPaths)
1616
{
1717
try
1818
{
@@ -55,7 +55,7 @@ private void RestoreNugetPackages(List<FileInfo> allNonBinaryFiles, IEnumerable<
5555
}
5656

5757
nugetPackageDllPaths.ExceptWith(excludedPaths);
58-
dllPaths.UnionWith(nugetPackageDllPaths.Select(p => new AssemblyPath(p)));
58+
dllPaths.UnionWith(nugetPackageDllPaths.Select(p => new AssemblyLookupLocation(p)));
5959
}
6060
catch (Exception exc)
6161
{
@@ -72,7 +72,7 @@ private void RestoreNugetPackages(List<FileInfo> allNonBinaryFiles, IEnumerable<
7272
.Paths
7373
.Select(d => Path.Combine(packageDirectory.DirInfo.FullName, d))
7474
.ToList();
75-
dllPaths.UnionWith(paths.Select(p => new AssemblyPath(p)));
75+
dllPaths.UnionWith(paths.Select(p => new AssemblyLookupLocation(p)));
7676

7777
LogAllUnusedPackages(dependencies);
7878
DownloadMissingPackages(allNonBinaryFiles, dllPaths);
@@ -148,7 +148,7 @@ private void RestoreProjects(IEnumerable<string> projects, out IEnumerable<strin
148148
CompilationInfos.Add(("Failed project restore with package source error", nugetSourceFailures.ToString()));
149149
}
150150

151-
private void DownloadMissingPackages(List<FileInfo> allFiles, ISet<AssemblyPath> dllPaths, bool withNugetConfig = true)
151+
private void DownloadMissingPackages(List<FileInfo> allFiles, ISet<AssemblyLookupLocation> dllPaths, bool withNugetConfig = true)
152152
{
153153
var alreadyDownloadedPackages = GetRestoredPackageDirectoryNames(packageDirectory.DirInfo);
154154
var alreadyDownloadedLegacyPackages = GetRestoredLegacyPackageNames();

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public DependencyManager(string srcDir, ILogger logger)
9191
this.generatedSources = new();
9292
var allProjects = allNonBinaryFiles.SelectFileNamesByExtension(".csproj").ToList();
9393
var allSolutions = allNonBinaryFiles.SelectFileNamesByExtension(".sln").ToList();
94-
var dllPaths = allFiles.SelectFileNamesByExtension(".dll").Select<string, AssemblyPath>(x => x).ToHashSet();
94+
var dllPaths = allFiles.SelectFileNamesByExtension(".dll").Select(x => new AssemblyLookupLocation(x)).ToHashSet();
9595

9696
logger.LogInfo($"Found {allFiles.Count} files, {nonGeneratedSources.Count} source files, {allProjects.Count} project files, {allSolutions.Count} solution files, {dllPaths.Count} DLLs.");
9797

@@ -192,7 +192,7 @@ void exitCallback(int ret, string msg, bool silent)
192192
]);
193193
}
194194

195-
private HashSet<string> AddFrameworkDlls(HashSet<AssemblyPath> dllPaths)
195+
private HashSet<string> AddFrameworkDlls(HashSet<AssemblyLookupLocation> dllPaths)
196196
{
197197
var frameworkLocations = new HashSet<string>();
198198

@@ -230,7 +230,7 @@ private HashSet<string> AddFrameworkDlls(HashSet<AssemblyPath> dllPaths)
230230
continue;
231231
}
232232

233-
dllPaths.UnionWith(dlls.Select<string, AssemblyPath>(x => x));
233+
dllPaths.UnionWith(dlls.Select(x => new AssemblyLookupLocation(x)));
234234
frameworkLocations.UnionWith(dlls);
235235
}
236236
catch (Exception e)
@@ -284,7 +284,7 @@ private void RemoveNugetAnalyzerReferences()
284284
}
285285
}
286286

287-
private void SelectNewestFrameworkPath(string frameworkPath, string frameworkType, ISet<AssemblyPath> dllPaths, ISet<string> frameworkLocations)
287+
private void SelectNewestFrameworkPath(string frameworkPath, string frameworkType, ISet<AssemblyLookupLocation> dllPaths, ISet<string> frameworkLocations)
288288
{
289289
var versionFolders = GetPackageVersionSubDirectories(frameworkPath);
290290
if (versionFolders.Length > 1)
@@ -313,7 +313,7 @@ private static DirectoryInfo[] GetPackageVersionSubDirectories(string packagePat
313313
.ToArray();
314314
}
315315

316-
private void RemoveFrameworkNugetPackages(ISet<AssemblyPath> dllPaths, int fromIndex = 0)
316+
private void RemoveFrameworkNugetPackages(ISet<AssemblyLookupLocation> dllPaths, int fromIndex = 0)
317317
{
318318
var packagesInPrioOrder = FrameworkPackageNames.NetFrameworks;
319319
for (var i = fromIndex; i < packagesInPrioOrder.Length; i++)
@@ -322,7 +322,7 @@ private void RemoveFrameworkNugetPackages(ISet<AssemblyPath> dllPaths, int fromI
322322
}
323323
}
324324

325-
private void AddNetFrameworkDlls(ISet<AssemblyPath> dllPaths, ISet<string> frameworkLocations)
325+
private void AddNetFrameworkDlls(ISet<AssemblyLookupLocation> dllPaths, ISet<string> frameworkLocations)
326326
{
327327
// Multiple dotnet framework packages could be present.
328328
// The order of the packages is important, we're adding the first one that is present in the nuget cache.
@@ -371,7 +371,7 @@ private void AddNetFrameworkDlls(ISet<AssemblyPath> dllPaths, ISet<string> frame
371371
if (runtimeLocation is null)
372372
{
373373
runtimeLocation ??= Runtime.ExecutingRuntime;
374-
dllPaths.Add(new AssemblyPath(runtimeLocation, name => !name.StartsWith("Semmle.")));
374+
dllPaths.Add(new AssemblyLookupLocation(runtimeLocation, name => !name.StartsWith("Semmle.")));
375375
}
376376
else
377377
{
@@ -382,7 +382,7 @@ private void AddNetFrameworkDlls(ISet<AssemblyPath> dllPaths, ISet<string> frame
382382
frameworkLocations.Add(runtimeLocation);
383383
}
384384

385-
private void RemoveNugetPackageReference(string packagePrefix, ISet<AssemblyPath> dllPaths)
385+
private void RemoveNugetPackageReference(string packagePrefix, ISet<AssemblyLookupLocation> dllPaths)
386386
{
387387
var packageFolder = packageDirectory.DirInfo.FullName.ToLowerInvariant();
388388
if (packageFolder == null)
@@ -404,7 +404,7 @@ private bool IsAspNetCoreDetected()
404404
return fileContent.IsNewProjectStructureUsed && fileContent.UseAspNetCoreDlls;
405405
}
406406

407-
private void AddAspNetCoreFrameworkDlls(ISet<AssemblyPath> dllPaths, ISet<string> frameworkLocations)
407+
private void AddAspNetCoreFrameworkDlls(ISet<AssemblyLookupLocation> dllPaths, ISet<string> frameworkLocations)
408408
{
409409
if (!IsAspNetCoreDetected())
410410
{
@@ -426,7 +426,7 @@ private void AddAspNetCoreFrameworkDlls(ISet<AssemblyPath> dllPaths, ISet<string
426426
}
427427
}
428428

429-
private void AddMicrosoftWindowsDesktopDlls(ISet<AssemblyPath> dllPaths, ISet<string> frameworkLocations)
429+
private void AddMicrosoftWindowsDesktopDlls(ISet<AssemblyLookupLocation> dllPaths, ISet<string> frameworkLocations)
430430
{
431431
if (GetPackageDirectory(FrameworkPackageNames.WindowsDesktopFramework, packageDirectory) is string windowsDesktopApp)
432432
{

0 commit comments

Comments
 (0)