Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dotnet --list-runtimes to identify installed sdks #1288

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ branches:
- /^azure-/

build_script:
- ps: dotnet --info
- ps: .\build.ps1 --target=Appveyor --configuration=Release

# disable built-in tests.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

#if NETFRAMEWORK
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace NUnit.Engine.Services.RuntimeLocators
Expand All @@ -11,66 +13,114 @@ public static class NetCoreRuntimeLocator
{
public static IEnumerable<RuntimeFramework> FindRuntimes()
{
const string WINDOWS_INSTALL_DIR = "C:\\Program Files\\dotnet\\";
const string LINUX_INSTALL_DIR = "/usr/shared/dotnet/";
string INSTALL_DIR = Path.DirectorySeparatorChar == '\\'
? WINDOWS_INSTALL_DIR
: LINUX_INSTALL_DIR;
string runtimeDir = Path.Combine(INSTALL_DIR, Path.Combine("shared", "Microsoft.NETCore.App"));
List<Version> alreadyFound = new List<Version>();

if (Directory.Exists(INSTALL_DIR) &&
File.Exists(Path.Combine(INSTALL_DIR, "dotnet.exe")) &&
Directory.Exists(runtimeDir))
foreach (string dirName in GetRuntimeDirectories())
{
var dirList = new DirectoryInfo(runtimeDir).GetDirectories();
var dirNames = new List<string>();
foreach (var dir in dirList)
dirNames.Add(dir.Name);
Version newVersion;
if (TryGetVersionFromString(dirName, out newVersion) && !alreadyFound.Contains(newVersion))
{
alreadyFound.Add(newVersion);
yield return new RuntimeFramework(Runtime.NetCore, newVersion);
}
}

foreach (var runtime in GetNetCoreRuntimesFromDirectoryNames(dirNames))
yield return runtime;
foreach (string line in GetRuntimeList())
{
Version newVersion;
if (TryGetVersionFromString(line, out newVersion) && !alreadyFound.Contains(newVersion))
{
alreadyFound.Add(newVersion);
yield return new RuntimeFramework(Runtime.NetCore, newVersion);
}
}
}

// Deal with oddly named directories, which may sometimes appear when previews are installed
internal static IList<RuntimeFramework> GetNetCoreRuntimesFromDirectoryNames(IEnumerable<string> dirNames)
private static IEnumerable<string> GetRuntimeDirectories()
{
const string VERSION_CHARS = ".0123456789";
var runtimes = new List<RuntimeFramework>();
string installDir = GetDotNetInstallDirectory();

if (installDir != null && Directory.Exists(installDir) &&
File.Exists(Path.Combine(installDir, "dotnet.exe")))
{
string runtimeDir = Path.Combine(installDir, Path.Combine("shared", "Microsoft.NETCore.App"));
if (Directory.Exists(runtimeDir))
foreach (var dir in new DirectoryInfo(runtimeDir).GetDirectories())
yield return dir.Name;
}
}

foreach (string dirName in dirNames)
private static IEnumerable<string> GetRuntimeList()
{
var process = new Process
{
int len = 0;
foreach (char c in dirName)
StartInfo = new ProcessStartInfo
{
if (VERSION_CHARS.IndexOf(c) >= 0)
len++;
else
break;
FileName = "dotnet",
Arguments = "--list-runtimes",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};

if (len == 0)
continue;
try
{
process.Start();
}
catch (Exception)
{
// Failed to start dotnet command. Assume no versions are installed and just r eturn just return
yield break;
}

Version fullVersion = null;
try
{
fullVersion = new Version(dirName.Substring(0, len));
}
catch
{
continue;
}
const string PREFIX = "Microsoft.NETCore.App ";
const int VERSION_START = 22;

while (!process.StandardOutput.EndOfStream)
{
var line = process.StandardOutput.ReadLine();
if (line.StartsWith(PREFIX))
yield return line.Substring(VERSION_START);
}
}

private static bool TryGetVersionFromString(string text, out Version newVersion)
{
const string VERSION_CHARS = ".0123456789";

var newVersion = new Version(fullVersion.Major, fullVersion.Minor);
int count = runtimes.Count;
if (count > 0 && runtimes[count - 1].FrameworkVersion == newVersion)
continue;
int len = 0;
foreach (char c in text)
{
if (VERSION_CHARS.IndexOf(c) >= 0)
len++;
else
break;
}

runtimes.Add(new RuntimeFramework(Runtime.NetCore, newVersion));
try
{
var fullVersion = new Version(text.Substring(0, len));
newVersion = new Version(fullVersion.Major, fullVersion.Minor);
return true;
}
catch
{
newVersion = new Version();
return false;
}
}

return runtimes;
internal static string GetDotNetInstallDirectory()
{
if (Path.DirectorySeparatorChar == '\\')
{
// Running on Windows so use registry
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\dotnet\SetUp\InstalledVersions\x64\sharedHost\");
return (string)key?.GetValue("Path");
}
else
return "/usr/shared/dotnet/";
}
}
}
Expand Down