Skip to content

Commit c1f167c

Browse files
committed
C#: Move package filtering logic from FileContent to DependencyManager.
1 parent 87d0d72 commit c1f167c

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
6060

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

63-
this.fileContent = new FileContent(packageDirectory, progressMonitor, () => GetFiles("*.*"));
63+
this.fileContent = new FileContent(progressMonitor, () => GetFiles("*.*"));
6464
this.allSources = GetFiles("*.cs").ToList();
6565
var allProjects = GetFiles("*.csproj");
6666
var solutions = options.SolutionFile is not null
@@ -388,7 +388,11 @@ private void DownloadMissingPackages()
388388
nugetConfig = nugetConfigs.FirstOrDefault();
389389
}
390390

391-
foreach (var package in fileContent.NotYetDownloadedPackages)
391+
var alreadyDownloadedPackages = Directory.GetDirectories(packageDirectory.DirInfo.FullName)
392+
.Select(d => Path.GetFileName(d)
393+
.ToLowerInvariant());
394+
var notYetDownloadedPackages = fileContent.AllPackages.Except(alreadyDownloadedPackages);
395+
foreach (var package in notYetDownloadedPackages)
392396
{
393397
progressMonitor.NugetInstall(package);
394398
using var tempDir = new TemporaryDirectory(ComputeTempDirectory(package));

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@ internal partial class FileContent
1919
private readonly ProgressMonitor progressMonitor;
2020
private readonly IUnsafeFileReader unsafeFileReader;
2121
private readonly Func<IEnumerable<string>> getFiles;
22-
private readonly Func<HashSet<string>> getAlreadyDownloadedPackages;
2322
private readonly HashSet<string> allPackages = new HashSet<string>();
2423
private readonly Initializer initialize;
2524

26-
public IEnumerable<string> NotYetDownloadedPackages
25+
public HashSet<string> AllPackages
2726
{
2827
get
2928
{
3029
initialize.Run();
31-
var alreadyDownloadedPackages = getAlreadyDownloadedPackages();
32-
return allPackages.Except(alreadyDownloadedPackages);
30+
return allPackages;
3331
}
3432
}
3533

@@ -51,23 +49,18 @@ public bool UseAspNetDlls
5149
}
5250
}
5351

54-
internal FileContent(Func<HashSet<string>> getAlreadyDownloadedPackages,
55-
ProgressMonitor progressMonitor,
52+
internal FileContent(ProgressMonitor progressMonitor,
5653
Func<IEnumerable<string>> getFiles,
5754
IUnsafeFileReader unsafeFileReader)
5855
{
59-
this.getAlreadyDownloadedPackages = getAlreadyDownloadedPackages;
6056
this.progressMonitor = progressMonitor;
6157
this.getFiles = getFiles;
6258
this.unsafeFileReader = unsafeFileReader;
6359
this.initialize = new Initializer(DoInitialize);
6460
}
6561

6662

67-
public FileContent(TemporaryDirectory packageDirectory, ProgressMonitor progressMonitor, Func<IEnumerable<string>> getFiles) : this(() => Directory.GetDirectories(packageDirectory.DirInfo.FullName)
68-
.Select(d => Path.GetFileName(d)
69-
.ToLowerInvariant())
70-
.ToHashSet(), progressMonitor, getFiles, new UnsafeFileReader())
63+
public FileContent(ProgressMonitor progressMonitor, Func<IEnumerable<string>> getFiles) : this(progressMonitor, getFiles, new UnsafeFileReader())
7164
{ }
7265

7366
private static string GetGroup(ReadOnlySpan<char> input, ValueMatch valueMatch, string groupPrefix)
@@ -114,7 +107,7 @@ private void DoInitialize()
114107
{
115108
// We can't get the group from the ValueMatch, so doing it manually:
116109
var packageName = GetGroup(line, valueMatch, "Include");
117-
if (!string.IsNullOrEmpty(packageName) && !allPackages.Contains(packageName))
110+
if (!string.IsNullOrEmpty(packageName))
118111
{
119112
allPackages.Add(packageName);
120113
}

csharp/extractor/Semmle.Extraction.Tests/FileContent.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public IEnumerable<string> ReadLines(string file)
3434

3535
internal class TestFileContent : FileContent
3636
{
37-
public TestFileContent(List<string> lines) : base(() => new HashSet<string>(),
38-
new ProgressMonitor(new LoggerStub()),
37+
public TestFileContent(List<string> lines) : base(new ProgressMonitor(new LoggerStub()),
3938
() => new List<string>() { "test1.cs" },
4039
new UnsafeFileReaderStub(lines))
4140
{ }
@@ -58,15 +57,15 @@ public void TestFileContent1()
5857
var fileContent = new TestFileContent(lines);
5958

6059
// Execute
61-
var notYetDownloadedPackages = fileContent.NotYetDownloadedPackages;
60+
var allPackages = fileContent.AllPackages;
6261
var useAspNetDlls = fileContent.UseAspNetDlls;
6362

6463
// Verify
6564
Assert.False(useAspNetDlls);
66-
Assert.Equal(3, notYetDownloadedPackages.Count());
67-
Assert.Contains("DotNetAnalyzers.DocumentationAnalyzers".ToLowerInvariant(), notYetDownloadedPackages);
68-
Assert.Contains("Microsoft.CodeAnalysis.NetAnalyzers".ToLowerInvariant(), notYetDownloadedPackages);
69-
Assert.Contains("StyleCop.Analyzers".ToLowerInvariant(), notYetDownloadedPackages);
65+
Assert.Equal(3, allPackages.Count);
66+
Assert.Contains("DotNetAnalyzers.DocumentationAnalyzers".ToLowerInvariant(), allPackages);
67+
Assert.Contains("Microsoft.CodeAnalysis.NetAnalyzers".ToLowerInvariant(), allPackages);
68+
Assert.Contains("StyleCop.Analyzers".ToLowerInvariant(), allPackages);
7069
}
7170

7271
[Fact]
@@ -84,13 +83,13 @@ public void TestFileContent2()
8483

8584
// Execute
8685
var useAspNetDlls = fileContent.UseAspNetDlls;
87-
var notYetDownloadedPackages = fileContent.NotYetDownloadedPackages;
86+
var allPackages = fileContent.AllPackages;
8887

8988
// Verify
9089
Assert.True(useAspNetDlls);
91-
Assert.Equal(2, notYetDownloadedPackages.Count());
92-
Assert.Contains("Microsoft.CodeAnalysis.NetAnalyzers".ToLowerInvariant(), notYetDownloadedPackages);
93-
Assert.Contains("StyleCop.Analyzers".ToLowerInvariant(), notYetDownloadedPackages);
90+
Assert.Equal(2, allPackages.Count);
91+
Assert.Contains("Microsoft.CodeAnalysis.NetAnalyzers".ToLowerInvariant(), allPackages);
92+
Assert.Contains("StyleCop.Analyzers".ToLowerInvariant(), allPackages);
9493
}
9594
}
9695
}

0 commit comments

Comments
 (0)