Skip to content

Commit 5bb2cba

Browse files
committed
Merge branch 'main' into redsun82/kotlin
2 parents 6a83bf9 + d5073df commit 5bb2cba

17 files changed

Lines changed: 398 additions & 217 deletions

File tree

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

Lines changed: 55 additions & 128 deletions
Large diffs are not rendered by default.

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,6 @@ private static IEnumerable<string> SelectFilesAux(this IEnumerable<FileInfo> fil
1414
public static IEnumerable<FileInfo> SelectRootFiles(this IEnumerable<FileInfo> files, DirectoryInfo dir) =>
1515
files.Where(file => file.DirectoryName == dir.FullName);
1616

17-
internal static IEnumerable<FileInfo> SelectSmallFiles(this IEnumerable<FileInfo> files, ILogger logger)
18-
{
19-
const int oneMb = 1_048_576;
20-
return files.Where(file =>
21-
{
22-
if (file.Length > oneMb)
23-
{
24-
logger.LogDebug($"Skipping {file.FullName} because it is bigger than 1MB.");
25-
return false;
26-
}
27-
return true;
28-
});
29-
}
30-
3117
public static IEnumerable<string> SelectFileNamesByExtension(this IEnumerable<FileInfo> files, params string[] extensions) =>
3218
files.SelectFilesAux(fi => extensions.Contains(fi.Extension));
3319

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Security.Policy;
6+
using Semmle.Util.Logging;
7+
8+
namespace Semmle.Extraction.CSharp.DependencyFetching
9+
{
10+
public class FileProvider
11+
{
12+
private static readonly HashSet<string> binaryFileExtensions = [".dll", ".exe"]; // TODO: add more binary file extensions.
13+
14+
private readonly ILogger logger;
15+
private readonly FileInfo[] all;
16+
private readonly Lazy<FileInfo[]> allNonBinary;
17+
private readonly Lazy<string[]> smallNonBinary;
18+
private readonly Lazy<string[]> sources;
19+
private readonly Lazy<string[]> projects;
20+
private readonly Lazy<string[]> solutions;
21+
private readonly Lazy<string[]> dlls;
22+
private readonly Lazy<string[]> nugetConfigs;
23+
private readonly Lazy<string[]> globalJsons;
24+
private readonly Lazy<string[]> razorViews;
25+
private readonly Lazy<string?> rootNugetConfig;
26+
27+
public FileProvider(DirectoryInfo sourceDir, ILogger logger)
28+
{
29+
SourceDir = sourceDir;
30+
this.logger = logger;
31+
32+
all = GetAllFiles();
33+
allNonBinary = new Lazy<FileInfo[]>(() => all.Where(f => !binaryFileExtensions.Contains(f.Extension.ToLowerInvariant())).ToArray());
34+
smallNonBinary = new Lazy<string[]>(() =>
35+
{
36+
var ret = SelectSmallFiles(allNonBinary.Value).SelectFileNames().ToArray();
37+
logger.LogInfo($"Found {ret.Length} small non-binary files in {SourceDir}.");
38+
return ret;
39+
});
40+
sources = new Lazy<string[]>(() => SelectTextFileNamesByExtension("source", ".cs"));
41+
projects = new Lazy<string[]>(() => SelectTextFileNamesByExtension("project", ".csproj"));
42+
solutions = new Lazy<string[]>(() => SelectTextFileNamesByExtension("solution", ".sln"));
43+
dlls = new Lazy<string[]>(() => SelectBinaryFileNamesByExtension("DLL", ".dll"));
44+
nugetConfigs = new Lazy<string[]>(() => allNonBinary.Value.SelectFileNamesByName("nuget.config").ToArray());
45+
globalJsons = new Lazy<string[]>(() => allNonBinary.Value.SelectFileNamesByName("global.json").ToArray());
46+
razorViews = new Lazy<string[]>(() => SelectTextFileNamesByExtension("razor view", ".cshtml", ".razor"));
47+
48+
rootNugetConfig = new Lazy<string?>(() => all.SelectRootFiles(SourceDir).SelectFileNamesByName("nuget.config").FirstOrDefault());
49+
}
50+
51+
private string[] SelectTextFileNamesByExtension(string filetype, params string[] extensions)
52+
{
53+
var ret = allNonBinary.Value.SelectFileNamesByExtension(extensions).ToArray();
54+
logger.LogInfo($"Found {ret.Length} {filetype} files in {SourceDir}.");
55+
return ret;
56+
}
57+
58+
private string[] SelectBinaryFileNamesByExtension(string filetype, params string[] extensions)
59+
{
60+
var ret = all.SelectFileNamesByExtension(extensions).ToArray();
61+
logger.LogInfo($"Found {ret.Length} {filetype} files in {SourceDir}.");
62+
return ret;
63+
}
64+
65+
private IEnumerable<FileInfo> SelectSmallFiles(IEnumerable<FileInfo> files)
66+
{
67+
const int oneMb = 1_048_576;
68+
return files.Where(file =>
69+
{
70+
if (file.Length > oneMb)
71+
{
72+
logger.LogDebug($"Skipping {file.FullName} because it is bigger than 1MB.");
73+
return false;
74+
}
75+
return true;
76+
});
77+
}
78+
79+
private FileInfo[] GetAllFiles()
80+
{
81+
logger.LogInfo($"Finding files in {SourceDir}...");
82+
var files = SourceDir.GetFiles("*.*", new EnumerationOptions { RecurseSubdirectories = true });
83+
84+
var filteredFiles = files.Where(f =>
85+
{
86+
try
87+
{
88+
if (f.Exists)
89+
{
90+
return true;
91+
}
92+
93+
logger.LogWarning($"File {f.FullName} could not be processed.");
94+
return false;
95+
}
96+
catch (Exception ex)
97+
{
98+
logger.LogWarning($"File {f.FullName} could not be processed: {ex.Message}");
99+
return false;
100+
}
101+
});
102+
103+
var allFiles = new FilePathFilter(SourceDir, logger).Filter(filteredFiles).ToArray();
104+
105+
logger.LogInfo($"Found {allFiles.Length} files in {SourceDir}.");
106+
return allFiles;
107+
}
108+
109+
public DirectoryInfo SourceDir { get; }
110+
public IEnumerable<string> SmallNonBinary => smallNonBinary.Value;
111+
public IEnumerable<string> Sources => sources.Value;
112+
public ICollection<string> Projects => projects.Value;
113+
public ICollection<string> Solutions => solutions.Value;
114+
public IEnumerable<string> Dlls => dlls.Value;
115+
public ICollection<string> NugetConfigs => nugetConfigs.Value;
116+
public string? RootNugetConfig => rootNugetConfig.Value;
117+
public IEnumerable<string> GlobalJsons => globalJsons.Value;
118+
public ICollection<string> RazorViews => razorViews.Value;
119+
}
120+
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackages.cs renamed to csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetExeWrapper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
namespace Semmle.Extraction.CSharp.DependencyFetching
99
{
1010
/// <summary>
11-
/// Manage the downloading of NuGet packages.
11+
/// Manage the downloading of NuGet packages with nuget.exe.
1212
/// Locates packages in a source tree and downloads all of the
1313
/// referenced assemblies to a temp folder.
1414
/// </summary>
15-
internal class NugetPackages : IDisposable
15+
internal class NugetExeWrapper : IDisposable
1616
{
1717
private readonly string? nugetExe;
1818
private readonly Util.Logging.ILogger logger;
@@ -37,7 +37,7 @@ internal class NugetPackages : IDisposable
3737
/// <summary>
3838
/// Create the package manager for a specified source tree.
3939
/// </summary>
40-
public NugetPackages(string sourceDir, TemporaryDirectory packageDirectory, Util.Logging.ILogger logger)
40+
public NugetExeWrapper(string sourceDir, TemporaryDirectory packageDirectory, Util.Logging.ILogger logger)
4141
{
4242
this.packageDirectory = packageDirectory;
4343
this.logger = logger;
@@ -243,7 +243,7 @@ private void RunMonoNugetCommand(string command, out IList<string> stdout)
243243
private void AddDefaultPackageSource(string nugetConfig)
244244
{
245245
logger.LogInfo("Adding default package source...");
246-
RunMonoNugetCommand($"sources add -Name DefaultNugetOrg -Source {DependencyManager.PublicNugetFeed} -ConfigFile \"{nugetConfig}\"", out var _);
246+
RunMonoNugetCommand($"sources add -Name DefaultNugetOrg -Source {NugetPackageRestorer.PublicNugetOrgFeed} -ConfigFile \"{nugetConfig}\"", out var _);
247247
}
248248

249249
public void Dispose()

0 commit comments

Comments
 (0)