Fix code review findings

This commit is contained in:
Tamas Vajk
2024-04-22 14:46:24 +02:00
parent 7b5f2c7d94
commit 05f3c64172
4 changed files with 18 additions and 46 deletions

View File

@@ -23,7 +23,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
this.dotNet = dotNet; this.dotNet = dotNet;
this.logger = logger; this.logger = logger;
newestSdkVersion = new Lazy<DotNetVersion?>(GetNewestSdk); newestSdkVersion = new Lazy<DotNetVersion?>(GetNewestSdkVersion);
cscPath = new Lazy<string?>(GetCscPath); cscPath = new Lazy<string?>(GetCscPath);
} }
@@ -46,7 +46,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
return sdks; return sdks;
} }
private DotNetVersion? GetNewestSdk() private DotNetVersion? GetNewestSdkVersion()
{ {
var listed = dotNet.GetListedSdks(); var listed = dotNet.GetListedSdks();
var sdks = ParseSdks(listed); var sdks = ParseSdks(listed);
@@ -55,14 +55,14 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
private string? GetCscPath() private string? GetCscPath()
{ {
var sdk = GetNewestSdk(); var newestSdkVersion = GetNewestSdkVersion();
if (sdk is null) if (newestSdkVersion is null)
{ {
logger.LogWarning("No dotnet SDK found."); logger.LogWarning("No dotnet SDK found.");
return null; return null;
} }
var path = Path.Combine(sdk.FullPath, "Roslyn", "bincore", "csc.dll"); var path = Path.Combine(newestSdkVersion.FullPath, "Roslyn", "bincore", "csc.dll");
logger.LogDebug($"Source generator CSC: '{path}'"); logger.LogDebug($"Source generator CSC: '{path}'");
if (!File.Exists(path)) if (!File.Exists(path))
{ {

View File

@@ -73,13 +73,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
continue; continue;
} }
if (!groupedFiles.TryGetValue(project.File, out var files)) groupedFiles.AddAnother(project.File, additionalFile);
{
files = [];
groupedFiles[project.File] = files;
}
files.Add(additionalFile);
} }
try try

View File

@@ -35,23 +35,22 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
public IEnumerable<string> RunSourceGenerator(IEnumerable<string> additionalFiles, string csprojFile, IEnumerable<string> references, string targetDir) public IEnumerable<string> RunSourceGenerator(IEnumerable<string> additionalFiles, string csprojFile, IEnumerable<string> references, string targetDir)
{ {
var name = Guid.NewGuid().ToString("N").ToUpper();
var tempPath = FileUtils.GetTemporaryWorkingDirectory(out var shouldCleanUp);
var analyzerConfig = Path.Combine(tempPath, $"{name}.txt");
var dllPath = Path.Combine(tempPath, $"{name}.dll");
var cscArgsPath = Path.Combine(tempPath, $"{name}.rsp");
var outputFolder = Path.Combine(targetDir, name);
Directory.CreateDirectory(outputFolder);
try try
{ {
var name = Guid.NewGuid().ToString("N").ToUpper();
using var tempDir = new TemporaryDirectory(Path.Join(FileUtils.GetTemporaryWorkingDirectory(out _), "source-generator"), "source generator temporary", logger);
var analyzerConfigPath = Path.Combine(tempDir.DirInfo.FullName, $"{name}.txt");
var dllPath = Path.Combine(tempDir.DirInfo.FullName, $"{name}.dll");
var cscArgsPath = Path.Combine(tempDir.DirInfo.FullName, $"{name}.rsp");
var outputFolder = Path.Combine(targetDir, name);
Directory.CreateDirectory(outputFolder);
logger.LogInfo("Producing analyzer config content."); logger.LogInfo("Producing analyzer config content.");
GenerateAnalyzerConfig(additionalFiles, csprojFile, analyzerConfig); GenerateAnalyzerConfig(additionalFiles, csprojFile, analyzerConfigPath);
logger.LogDebug($"Analyzer config content: {File.ReadAllText(analyzerConfig)}"); logger.LogDebug($"Analyzer config content: {File.ReadAllText(analyzerConfigPath)}");
var args = new StringBuilder(); var args = new StringBuilder();
args.Append($"/target:exe /generatedfilesout:\"{outputFolder}\" /out:\"{dllPath}\" /analyzerconfig:\"{analyzerConfig}\" "); args.Append($"/target:exe /generatedfilesout:\"{outputFolder}\" /out:\"{dllPath}\" /analyzerconfig:\"{analyzerConfigPath}\" ");
foreach (var f in Directory.GetFiles(SourceGeneratorFolder, "*.dll", new EnumerationOptions { RecurseSubdirectories = false, MatchCasing = MatchCasing.CaseInsensitive })) foreach (var f in Directory.GetFiles(SourceGeneratorFolder, "*.dll", new EnumerationOptions { RecurseSubdirectories = false, MatchCasing = MatchCasing.CaseInsensitive }))
{ {
@@ -91,27 +90,6 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
logger.LogInfo($"Failed to generate source files from {FileType} files: {ex.Message}"); logger.LogInfo($"Failed to generate source files from {FileType} files: {ex.Message}");
return []; return [];
} }
finally
{
if (shouldCleanUp)
{
DeleteFile(analyzerConfig);
DeleteFile(dllPath);
DeleteFile(cscArgsPath);
}
}
}
private void DeleteFile(string path)
{
try
{
File.Delete(path);
}
catch (Exception exc)
{
logger.LogWarning($"Failed to delete file {path}: {exc}");
}
} }
} }
} }

View File

@@ -15,9 +15,9 @@ namespace Semmle.Util
public DirectoryInfo DirInfo { get; } public DirectoryInfo DirInfo { get; }
public TemporaryDirectory(string name, string userReportedDirectoryPurpose, ILogger logger) public TemporaryDirectory(string path, string userReportedDirectoryPurpose, ILogger logger)
{ {
DirInfo = new DirectoryInfo(name); DirInfo = new DirectoryInfo(path);
DirInfo.Create(); DirInfo.Create();
this.userReportedDirectoryPurpose = userReportedDirectoryPurpose; this.userReportedDirectoryPurpose = userReportedDirectoryPurpose;
this.logger = logger; this.logger = logger;