Skip to content

Commit

Permalink
Use which directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Asplund committed Dec 24, 2024
1 parent eccacde commit a461dc2
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Source/GenerateSharp/Opal/System/IProcessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface IProcessManager
/// <param name="arguments">The arguments.</param>
/// <param name="workingDirectory">The working directory.</param>
public abstract IProcess CreateProcess(
Path executable,
string executable,
string arguments,
Path workingDirectory);
}
2 changes: 1 addition & 1 deletion Source/GenerateSharp/Opal/System/MockProcessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Path GetCurrentProcessFileName()
/// <param name="arguments">The arguments.</param>
/// <param name="workingDirectory">The working directory.</param>
public IProcess CreateProcess(
Path executable,
string executable,
string arguments,
Path workingDirectory)
{
Expand Down
6 changes: 3 additions & 3 deletions Source/GenerateSharp/Opal/System/RuntimeProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,7 +28,7 @@ public class RuntimeProcess : IProcess
/// <param name="arguments">The arguments.</param>
/// <param name="workingDirectory">The workingDirectory.</param>
public RuntimeProcess(
Path executable,
string executable,
string arguments,
Path workingDirectory)
{
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Source/GenerateSharp/Opal/System/RuntimeProcessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Path GetCurrentProcessFileName()
/// Creates a process for the provided executable path
/// </summary>
public IProcess CreateProcess(
Path executable,
string executable,
string arguments,
Path workingDirectory)
{
Expand Down
28 changes: 0 additions & 28 deletions Source/GenerateSharp/Swhere.Core/Recipe.sml

This file was deleted.

49 changes: 32 additions & 17 deletions Source/GenerateSharp/Swhere/DotNet/DotNetSDKUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,27 @@ public static class DotNetSDKUtilities
};

// Grant access to the install folder
var sourceDirectories = new List<Path>()
var sourceDirectories = new HashSet<Path>()
{
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<IList<(string Version, Path InstallDirectory)>> FindDotNetSDKVersionsAsync(
HashSet<Path> 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))
Expand All @@ -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<IDictionary<string, IList<(string Version, Path InstallDirectory)>>> FindDotNetRuntimeVersionsAsync(
HashSet<Path> 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<string, IList<(string, Path)>>();
foreach (var runtimeValue in runtimesOutput.Split(newline).SkipLast(1))
Expand All @@ -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}");

Expand All @@ -116,13 +125,14 @@ public static class DotNetSDKUtilities
}

private static Dictionary<string, IList<(string Version, Path InstallDirectory, FrameworkFileList? FrameworkList)>> FindDotNetTargetingPacksVersions(
HashSet<Path> sourceDirectories,
IDictionary<string, IList<(string Version, Path InstallDirectory)>> dotnetRuntimes)
{
var result = new Dictionary<string, IList<(string Version, Path InstallDirectory, FrameworkFileList? FrameworkList)>>();
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);
Expand All @@ -133,21 +143,26 @@ public static class DotNetSDKUtilities
}

private static List<(string Version, Path InstallDirectory, FrameworkFileList? FrameworkList)> FindDotNetTargetingPackVersions(
HashSet<Path> sourceDirectories,
string packName,
IList<(string Version, Path InstallDirectory)> runtimeVersions)
{
var versions = new List<(string Version, Path InstallDirectory, FrameworkFileList? FrameworkList)>();
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<IFileSystem>().Exists(packPath))
Log.HighPriority("FindDotNetPack: " + packVersionPath.ToString());
if (LifetimeManager.Get<IFileSystem>().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
{
Expand Down
2 changes: 1 addition & 1 deletion Source/GenerateSharp/Swhere/ExecutableUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Soup.Build.Discover;

public static class ExecutableUtilities
{
public static async Task<string> RunExecutableAsync(Path executable, IList<string> arguments)
public static async Task<string> RunExecutableAsync(string executable, IList<string> arguments)
{
var workingDirectory = new Path("./");

Expand Down
40 changes: 40 additions & 0 deletions Source/GenerateSharp/Swhere/SourceSetUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// <copyright file="SourceSetUtilities.cs" company="Soup">
// Copyright (c) Soup. All rights reserved.
// </copyright>

using System.Collections.Generic;
using Path = Opal.Path;

namespace Soup.Build.Discover;

public static class SourceSetUtilities
{
/// <summary>
/// Add a new source path and cleanup sub folders
/// </summary>
public static void Add(HashSet<Path> 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);
}
}
}
2 changes: 1 addition & 1 deletion Source/GenerateSharp/Swhere/VSWhereUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static async Task<Path> FindVSInstallRootAsync(string requires, bool inc
}

var process = LifetimeManager.Get<IProcessManager>().CreateProcess(
executablePath,
executablePath.ToString(),
arguments,
workingDirectory);
process.Start();
Expand Down
12 changes: 6 additions & 6 deletions Source/GenerateSharp/Swhere/WhereIsUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ public static class WhereIsUtilities
{
public static async Task<Path> 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:
Expand All @@ -44,7 +44,7 @@ public static async Task<Path> 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]
Expand Down

0 comments on commit a461dc2

Please sign in to comment.