C#: Refactor dotnet source generator execution

This commit is contained in:
Tamas Vajk
2024-04-16 10:20:23 +02:00
parent 13a71a4f6d
commit 407837afc4
5 changed files with 130 additions and 71 deletions

View File

@@ -1,15 +1,31 @@
using System.Collections.Generic;
using Semmle.Util;
using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Semmle.Util;
using Semmle.Util.Logging;
namespace Semmle.Extraction.CSharp.DependencyFetching
{
internal partial class Sdk
{
private readonly IDotNet dotNet;
private readonly ILogger logger;
private readonly Lazy<string?> cscPath;
public string? CscPath => cscPath.Value;
public Sdk(IDotNet dotNet) => this.dotNet = dotNet;
private readonly Lazy<DotNetVersion?> newestSdkVersion;
public DotNetVersion? Version => newestSdkVersion.Value;
public Sdk(IDotNet dotNet, ILogger logger)
{
this.dotNet = dotNet;
this.logger = logger;
newestSdkVersion = new Lazy<DotNetVersion?>(GetNewestSdk);
cscPath = new Lazy<string?>(GetCscPath);
}
[GeneratedRegex(@"^(\d+\.\d+\.\d+)(-([a-z]+)\.(\d+\.\d+\.\d+))?\s\[(.+)\]$")]
private static partial Regex SdkRegex();
@@ -30,11 +46,31 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
return sdks;
}
public DotNetVersion? GetNewestSdk()
private DotNetVersion? GetNewestSdk()
{
var listed = dotNet.GetListedSdks();
var sdks = ParseSdks(listed);
return sdks.Max();
}
private string? GetCscPath()
{
var sdk = GetNewestSdk();
if (sdk is null)
{
logger.LogWarning("No dotnet SDK found.");
return null;
}
var path = Path.Combine(sdk.FullPath, "Roslyn", "bincore", "csc.dll");
logger.LogInfo($"Source generator CSC: '{path}'");
if (!File.Exists(path))
{
logger.LogInfo($"csc.dll not found at '{path}'.");
return null;
}
return path;
}
}
}