From a461dc20c5acc481ce3713f889c95cdfe71ac6af Mon Sep 17 00:00:00 2001 From: Matthew Asplund Date: Mon, 23 Dec 2024 21:10:05 -0600 Subject: [PATCH] Use which directly --- .vscode/launch.json | 2 +- .../Opal/System/IProcessManager.cs | 2 +- .../Opal/System/MockProcessManager.cs | 2 +- .../Opal/System/RuntimeProcess.cs | 6 +-- .../Opal/System/RuntimeProcessManager.cs | 2 +- Source/GenerateSharp/Swhere.Core/Recipe.sml | 28 ----------- .../Swhere/DotNet/DotNetSDKUtilities.cs | 49 ++++++++++++------- .../Swhere/ExecutableUtilities.cs | 2 +- .../Swhere/SourceSetUtilities.cs | 40 +++++++++++++++ .../GenerateSharp/Swhere/VSWhereUtilities.cs | 2 +- .../GenerateSharp/Swhere/WhereIsUtilities.cs | 12 ++--- 11 files changed, 87 insertions(+), 60 deletions(-) delete mode 100644 Source/GenerateSharp/Swhere.Core/Recipe.sml create mode 100644 Source/GenerateSharp/Swhere/SourceSetUtilities.cs diff --git a/.vscode/launch.json b/.vscode/launch.json index 0942b106..ba445d13 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,7 +6,7 @@ "type": "coreclr", "request": "launch", "preLaunchTask": "", - "program": "${workspaceFolder}/Source/out/msbuild/bin/Swhere/Debug/net9.0/Swhere.dll", + "program": "${workspaceFolder}/out/msbuild/bin/Swhere/Debug/net9.0/linux-x64/Swhere.dll", "args": [], "cwd": "${workspaceFolder}", "stopAtEntry": false, diff --git a/Source/GenerateSharp/Opal/System/IProcessManager.cs b/Source/GenerateSharp/Opal/System/IProcessManager.cs index 7910ec63..5e0cfe73 100644 --- a/Source/GenerateSharp/Opal/System/IProcessManager.cs +++ b/Source/GenerateSharp/Opal/System/IProcessManager.cs @@ -22,7 +22,7 @@ public interface IProcessManager /// The arguments. /// The working directory. public abstract IProcess CreateProcess( - Path executable, + string executable, string arguments, Path workingDirectory); } diff --git a/Source/GenerateSharp/Opal/System/MockProcessManager.cs b/Source/GenerateSharp/Opal/System/MockProcessManager.cs index e97736fa..9e257d15 100644 --- a/Source/GenerateSharp/Opal/System/MockProcessManager.cs +++ b/Source/GenerateSharp/Opal/System/MockProcessManager.cs @@ -71,7 +71,7 @@ public Path GetCurrentProcessFileName() /// The arguments. /// The working directory. public IProcess CreateProcess( - Path executable, + string executable, string arguments, Path workingDirectory) { diff --git a/Source/GenerateSharp/Opal/System/RuntimeProcess.cs b/Source/GenerateSharp/Opal/System/RuntimeProcess.cs index 98784496..b96f7635 100644 --- a/Source/GenerateSharp/Opal/System/RuntimeProcess.cs +++ b/Source/GenerateSharp/Opal/System/RuntimeProcess.cs @@ -14,7 +14,7 @@ namespace Opal.System; public class RuntimeProcess : IProcess { // Input - private readonly Path executable; + private readonly string executable; private readonly string arguments; private readonly Path workingDirectory; @@ -28,7 +28,7 @@ public class RuntimeProcess : IProcess /// The arguments. /// The workingDirectory. public RuntimeProcess( - Path executable, + string executable, string arguments, Path workingDirectory) { @@ -44,7 +44,7 @@ public void Start() { var processInfo = new ProcessStartInfo() { - FileName = this.executable.ToString(), + FileName = this.executable, Arguments = this.arguments, WorkingDirectory = this.workingDirectory.ToString(), RedirectStandardOutput = true, diff --git a/Source/GenerateSharp/Opal/System/RuntimeProcessManager.cs b/Source/GenerateSharp/Opal/System/RuntimeProcessManager.cs index 9260d0d5..9f3371ac 100644 --- a/Source/GenerateSharp/Opal/System/RuntimeProcessManager.cs +++ b/Source/GenerateSharp/Opal/System/RuntimeProcessManager.cs @@ -30,7 +30,7 @@ public Path GetCurrentProcessFileName() /// Creates a process for the provided executable path /// public IProcess CreateProcess( - Path executable, + string executable, string arguments, Path workingDirectory) { diff --git a/Source/GenerateSharp/Swhere.Core/Recipe.sml b/Source/GenerateSharp/Swhere.Core/Recipe.sml deleted file mode 100644 index ed52e6a7..00000000 --- a/Source/GenerateSharp/Swhere.Core/Recipe.sml +++ /dev/null @@ -1,28 +0,0 @@ -Name: 'Swhere.Core' -Language: 'C#|0' -Version: 1.0.0 -Source: [ - 'DotNet/DotNetSDKUtilities.cs' - 'Nuget/NugetPackage.cs' - 'Nuget/NugetPackageDependency.cs' - 'Nuget/NugetPackageTargetFramework.cs' - 'Nuget/NugetPackageVersion.cs' - 'Nuget/NugetSDKUtilities.cs' - 'Nuget/NuspecDependency.cs' - 'Nuget/NuspecDependencyBase.cs' - 'Nuget/NuspecDependencyGroup.cs' - 'Nuget/NuspecMetadata.cs' - 'Nuget/NuspecPackage.cs' - 'ExecutableUtilities.cs' - 'SwhereManager.cs' - 'VSWhereUtilities.cs' - 'WhereIsUtilities.cs' - 'WindowsSDKUtilities.cs' -] - -Dependencies: { - Runtime: [ - '../Utilities/' - '../Opal/' - ] -} diff --git a/Source/GenerateSharp/Swhere/DotNet/DotNetSDKUtilities.cs b/Source/GenerateSharp/Swhere/DotNet/DotNetSDKUtilities.cs index 974becb1..bdd8ca05 100644 --- a/Source/GenerateSharp/Swhere/DotNet/DotNetSDKUtilities.cs +++ b/Source/GenerateSharp/Swhere/DotNet/DotNetSDKUtilities.cs @@ -33,26 +33,27 @@ public static class DotNetSDKUtilities }; // Grant access to the install folder - var sourceDirectories = new List() + var sourceDirectories = new HashSet() { dotnetInstallPath, }; - var dotnetSDKs = await FindDotNetSDKVersionsAsync(dotnetExecutablePath, newline); - var dotnetRuntimes = await FindDotNetRuntimeVersionsAsync(dotnetExecutablePath, newline); - var dotnetTargetingPacks = FindDotNetTargetingPacksVersions(dotnetRuntimes); + var dotnetSDKs = await FindDotNetSDKVersionsAsync(sourceDirectories, dotnetExecutablePath, newline); + var dotnetRuntimes = await FindDotNetRuntimeVersionsAsync(sourceDirectories, dotnetExecutablePath, newline); + var dotnetTargetingPacks = FindDotNetTargetingPacksVersions(sourceDirectories, dotnetRuntimes); - return (dotnetExecutablePath, dotnetSDKs, dotnetRuntimes, dotnetTargetingPacks, sourceDirectories); + return (dotnetExecutablePath, dotnetSDKs, dotnetRuntimes, dotnetTargetingPacks, sourceDirectories.ToList()); } private static async Task> FindDotNetSDKVersionsAsync( + HashSet sourceDirectories, Path dotnetExecutablePath, string newline) { // Check the default tools version Log.HighPriority("Find DotNet SDK Versions"); var sdksOutput = await ExecutableUtilities.RunExecutableAsync( - dotnetExecutablePath, + dotnetExecutablePath.ToString(), ["--list-sdks"]); var sdks = new List<(string, Path)>(); foreach (var sdkValue in sdksOutput.Split(newline).SkipLast(1)) @@ -66,23 +67,28 @@ public static class DotNetSDKUtilities var version = sdkValue[..splitIndex]; var installationValue = sdkValue.Substring(splitIndex + 2, sdkValue.Length - splitIndex - 3); - var installationPath = Path.Parse($"{installationValue}\\{version}\\"); + var installationPath = Path.Parse(installationValue); + var installationVersionPath = new Path($"./{version}/"); - Log.Info($"Found SDK: {version} {installationPath}"); - sdks.Add((version, installationPath)); + // Ensure we have read access to the sdk + SourceSetUtilities.Add(sourceDirectories, installationPath); + + Log.Info($"Found SDK: {version} {installationVersionPath}"); + sdks.Add((version, installationVersionPath)); } return sdks; } private static async Task>> FindDotNetRuntimeVersionsAsync( + HashSet sourceDirectories, Path dotnetExecutablePath, string newline) { // Check the default tools version Log.HighPriority("Find DotNet Runtime Versions"); var runtimesOutput = await ExecutableUtilities.RunExecutableAsync( - dotnetExecutablePath, + dotnetExecutablePath.ToString(), ["--list-runtimes"]); var runtimes = new Dictionary>(); foreach (var runtimeValue in runtimesOutput.Split(newline).SkipLast(1)) @@ -98,7 +104,10 @@ public static class DotNetSDKUtilities var name = runtimeValue[..split1Index]; var version = runtimeValue.Substring(split1Index + 1, split2Index - split1Index - 1); var installationValue = runtimeValue.Substring(split2Index + 2, runtimeValue.Length - split2Index - 3); - var installationPath = Path.Parse($"{installationValue}\\"); + var installationPath = Path.Parse($"{installationValue}/"); + + // Ensure we have read access to the runtime + SourceSetUtilities.Add(sourceDirectories, installationPath); Log.Info($"Found Runtime: {name} {version} {installationPath}"); @@ -116,13 +125,14 @@ public static class DotNetSDKUtilities } private static Dictionary> FindDotNetTargetingPacksVersions( + HashSet sourceDirectories, IDictionary> dotnetRuntimes) { var result = new Dictionary>(); foreach (var (runtime, versions) in dotnetRuntimes) { var packName = $"{runtime}.Ref"; - var packageVersions = FindDotNetTargetingPackVersions(packName, versions); + var packageVersions = FindDotNetTargetingPackVersions(sourceDirectories, packName, versions); if (packageVersions.Count > 0) { result.Add(packName, packageVersions); @@ -133,6 +143,7 @@ public static class DotNetSDKUtilities } private static List<(string Version, Path InstallDirectory, FrameworkFileList? FrameworkList)> FindDotNetTargetingPackVersions( + HashSet sourceDirectories, string packName, IList<(string Version, Path InstallDirectory)> runtimeVersions) { @@ -140,14 +151,18 @@ public static class DotNetSDKUtilities foreach (var (version, installDirectory) in runtimeVersions) { var rootDirectory = installDirectory.GetParent().GetParent(); - var packPath = rootDirectory + new Path($"./packs/{packName}/{version}/"); + var packPath = rootDirectory + new Path($"./packs/"); + var packVersionPath = packPath + new Path($"./{packName}/{version}/"); // Check the default tools version - Log.HighPriority("FindDotNetPack: " + packPath.ToString()); - if (LifetimeManager.Get().Exists(packPath)) + Log.HighPriority("FindDotNetPack: " + packVersionPath.ToString()); + if (LifetimeManager.Get().Exists(packVersionPath)) { - var frameworkList = LoadFrameworkList(packPath); - versions.Add((version, packPath, frameworkList)); + // Ensure we have read access to the pack + SourceSetUtilities.Add(sourceDirectories, packPath); + + var frameworkList = LoadFrameworkList(packVersionPath); + versions.Add((version, packVersionPath, frameworkList)); } else { diff --git a/Source/GenerateSharp/Swhere/ExecutableUtilities.cs b/Source/GenerateSharp/Swhere/ExecutableUtilities.cs index 4ad24045..b9c3cb4f 100644 --- a/Source/GenerateSharp/Swhere/ExecutableUtilities.cs +++ b/Source/GenerateSharp/Swhere/ExecutableUtilities.cs @@ -13,7 +13,7 @@ namespace Soup.Build.Discover; public static class ExecutableUtilities { - public static async Task RunExecutableAsync(Path executable, IList arguments) + public static async Task RunExecutableAsync(string executable, IList arguments) { var workingDirectory = new Path("./"); diff --git a/Source/GenerateSharp/Swhere/SourceSetUtilities.cs b/Source/GenerateSharp/Swhere/SourceSetUtilities.cs new file mode 100644 index 00000000..39599c22 --- /dev/null +++ b/Source/GenerateSharp/Swhere/SourceSetUtilities.cs @@ -0,0 +1,40 @@ +// +// Copyright (c) Soup. All rights reserved. +// + +using System.Collections.Generic; +using Path = Opal.Path; + +namespace Soup.Build.Discover; + +public static class SourceSetUtilities +{ + /// + /// Add a new source path and cleanup sub folders + /// + public static void Add(HashSet sourceSet, Path value) + { + bool isIncluded = false; + var current = value; + while (true) + { + if (sourceSet.Contains(current)) + { + isIncluded = true; + break; + } + + var next = current.GetParent(); + if (current == next) + break; + + current = next; + } + + if (!isIncluded) + { + // TODO: Remove already added values that this folder contains + _ = sourceSet.Add(value); + } + } +} \ No newline at end of file diff --git a/Source/GenerateSharp/Swhere/VSWhereUtilities.cs b/Source/GenerateSharp/Swhere/VSWhereUtilities.cs index b638c839..3ff308de 100644 --- a/Source/GenerateSharp/Swhere/VSWhereUtilities.cs +++ b/Source/GenerateSharp/Swhere/VSWhereUtilities.cs @@ -67,7 +67,7 @@ private static async Task FindVSInstallRootAsync(string requires, bool inc } var process = LifetimeManager.Get().CreateProcess( - executablePath, + executablePath.ToString(), arguments, workingDirectory); process.Start(); diff --git a/Source/GenerateSharp/Swhere/WhereIsUtilities.cs b/Source/GenerateSharp/Swhere/WhereIsUtilities.cs index 648b0f3a..b8bfd837 100644 --- a/Source/GenerateSharp/Swhere/WhereIsUtilities.cs +++ b/Source/GenerateSharp/Swhere/WhereIsUtilities.cs @@ -15,24 +15,24 @@ public static class WhereIsUtilities { public static async Task FindExecutableAsync(OSPlatform platform, string name) { - Path executablePath; + string executable; string separator; int skipCount; int newLineLength; switch (platform) { case OSPlatform.Windows: - executablePath = new Path("C:/Windows/System32/where.exe"); + executable = "C:/Windows/System32/where.exe"; separator = "\r\n"; skipCount = 0; newLineLength = 2; break; case OSPlatform.Linux: - executablePath = new Path("/usr/bin/whereis"); + executable = "which"; separator = " "; - // Whereis sets the name as the first entry - skipCount = 1; + // which sets the name as the first entry + skipCount = 0; newLineLength = 1; break; default: @@ -44,7 +44,7 @@ public static async Task FindExecutableAsync(OSPlatform platform, string n name, }; - var stdOut = await ExecutableUtilities.RunExecutableAsync(executablePath, arguments); + var stdOut = await ExecutableUtilities.RunExecutableAsync(executable, arguments); // The first line is the path var values = stdOut[..^newLineLength]