Use PATH if possible (#5)

This commit is contained in:
Patrick Stevens
2024-06-16 23:23:30 +01:00
committed by GitHub
parent 9850cff870
commit 94a9c53912
2 changed files with 23 additions and 9 deletions

View File

@@ -8,12 +8,12 @@ module Program =
let main argv =
let info = DotnetEnvironmentInfo.Get ()
Console.WriteLine info
Console.WriteLine ("SDKs:")
Console.WriteLine "SDKs:"
for sdk in info.Sdks do
Console.WriteLine $"SDK: %O{sdk}"
Console.WriteLine ("Frameworks:")
Console.WriteLine "Frameworks:"
for f in info.Frameworks do
Console.WriteLine $"Framework: %O{f}"

View File

@@ -164,6 +164,25 @@ public record DotnetEnvironmentInfo(
}
}
private static FileInfo? LocateDotnetExe()
{
var path = Environment.GetEnvironmentVariable("PATH");
if (path != null)
{
foreach (var component in path.Split(':'))
{
var dotnet = Path.Combine(component, "dotnet");
if (File.Exists(dotnet))
{
return new FileInfo(dotnet);
}
}
}
var renv = RuntimeEnvironment.GetRuntimeDirectory();
return FindDotnetAbove(new DirectoryInfo(renv));
}
/// <summary>
/// Get the environment information that is available to some arbitrary `dotnet` executable we were able to find.
/// </summary>
@@ -171,14 +190,9 @@ public record DotnetEnvironmentInfo(
/// <exception cref="Exception">Throws on any failure; handles nothing gracefully.</exception>
public static DotnetEnvironmentInfo Get()
{
var dotnetExe = FindDotnetAbove(new DirectoryInfo(RuntimeEnvironment.GetRuntimeDirectory()));
if (ReferenceEquals(dotnetExe, null))
{
// This can happen! Maybe we're self-contained.
return GetSpecific(null);
}
var dotnetExe = LocateDotnetExe();
// `null` can happen! Maybe we're self-contained.
return GetSpecific(dotnetExe);
}