using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Semmle.Extraction.CSharp.DependencyFetching { internal interface IDotNet { RestoreResult Restore(RestoreSettings restoreSettings); bool New(string folder); bool AddPackage(string folder, string package); IList GetListedRuntimes(); IList GetListedSdks(); bool Exec(string execArgs); } internal record class RestoreSettings(string File, string PackageDirectory, bool ForceDotnetRefAssemblyFetching, string? PathToNugetConfig = null, bool ForceReevaluation = false); internal partial record class RestoreResult(bool Success, IList Output) { private readonly Lazy> assetsFilePaths = new(() => GetFirstGroupOnMatch(AssetsFileRegex(), Output)); public IEnumerable AssetsFilePaths => Success ? assetsFilePaths.Value : Array.Empty(); private readonly Lazy> restoredProjects = new(() => GetFirstGroupOnMatch(RestoredProjectRegex(), Output)); public IEnumerable RestoredProjects => Success ? restoredProjects.Value : Array.Empty(); private readonly Lazy hasNugetPackageSourceError = new(() => Output.Any(s => s.Contains("NU1301"))); public bool HasNugetPackageSourceError => hasNugetPackageSourceError.Value; private static IEnumerable GetFirstGroupOnMatch(Regex regex, IEnumerable lines) => lines .Select(line => regex.Match(line)) .Where(match => match.Success) .Select(match => match.Groups[1].Value); [GeneratedRegex("Restored\\s+(.+\\.csproj)", RegexOptions.Compiled)] private static partial Regex RestoredProjectRegex(); [GeneratedRegex("[Assets\\sfile\\shas\\snot\\schanged.\\sSkipping\\sassets\\sfile\\swriting.|Writing\\sassets\\sfile\\sto\\sdisk.]\\sPath:\\s(.+)", RegexOptions.Compiled)] private static partial Regex AssetsFileRegex(); } }