< Summary

Information
Class: ToolCore.McpHost.DotNetRuntimeLocator
Assembly: ToolCore
File(s): /home/runner/work/InvAit/InvAit/ToolCore/McpHost/DotNetRuntimeLocator.cs
Tag: 194_44631168438
Line coverage
44%
Covered lines: 17
Uncovered lines: 21
Coverable lines: 38
Total lines: 99
Line coverage: 44.7%
Branch coverage
31%
Covered branches: 7
Total branches: 22
Branch coverage: 31.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsRuntimeInstalled()100%210%
TryGetDotNetExecutable(...)50%6677.77%
TryFindRuntimeDirectory(...)0%7280%
TryGetDotNetRoot(...)50%2266.66%
GetDotNetRootCandidates()50%66100%

File(s)

/home/runner/work/InvAit/InvAit/ToolCore/McpHost/DotNetRuntimeLocator.cs

#LineLine coverage
 1using System.IO;
 2
 3namespace ToolCore.McpHost;
 4
 5public static class DotNetRuntimeLocator
 6{
 7    public const string RequiredMajorVersion = "10";
 8
 9    public const string InstallUrl = "https://dotnet.microsoft.com/download/dotnet/10.0";
 10
 11    public const string InstallHint =
 12        "MCP host requires the .NET " + RequiredMajorVersion + " runtime, which was not found on this machine. " +
 13        "Install it from " + InstallUrl + " (any SDK or '.NET Runtime' package) and restart Visual Studio. " +
 14        "Visual Studio 2026 ships it by default; on Visual Studio 2022 it may need to be installed manually.";
 15
 16    public static bool IsRuntimeInstalled()
 17    {
 018        return TryFindRuntimeDirectory(RequiredMajorVersion, out _);
 19    }
 20
 21    public static bool TryGetDotNetExecutable(out string dotNetExe)
 22    {
 1023        dotNetExe = string.Empty;
 1024        if (!TryGetDotNetRoot(out var root))
 025            return false;
 26
 1027        var exeName = Path.DirectorySeparatorChar == '\\' ? "dotnet.exe" : "dotnet";
 1028        var candidate = Path.Combine(root, exeName);
 1029        if (File.Exists(candidate))
 30        {
 1031            dotNetExe = candidate;
 1032            return true;
 33        }
 34
 035        return false;
 36    }
 37
 38    internal static bool TryFindRuntimeDirectory(string majorVersionPrefix, out string runtimeDirectory)
 39    {
 040        runtimeDirectory = string.Empty;
 041        foreach (var root in GetDotNetRootCandidates())
 42        {
 043            var sharedDir = Path.Combine(root, "shared", "Microsoft.NETCore.App");
 044            if (!Directory.Exists(sharedDir))
 45                continue;
 46
 47            try
 48            {
 049                foreach (var dir in Directory.EnumerateDirectories(sharedDir))
 50                {
 051                    var name = Path.GetFileName(dir);
 052                    if (name.StartsWith(majorVersionPrefix + ".", StringComparison.Ordinal))
 53                    {
 054                        runtimeDirectory = dir;
 055                        return true;
 56                    }
 57                }
 058            }
 059            catch (IOException)
 60            {
 061            }
 062            catch (UnauthorizedAccessException)
 63            {
 064            }
 65        }
 66
 067        return false;
 068    }
 69
 70    public static bool TryGetDotNetRoot(out string dotNetRoot)
 71    {
 3072        foreach (var candidate in GetDotNetRootCandidates())
 73        {
 1074            dotNetRoot = candidate;
 1075            return true;
 76        }
 77
 078        dotNetRoot = string.Empty;
 079        return false;
 1080    }
 81
 82    private static IEnumerable<string> GetDotNetRootCandidates()
 83    {
 1084        var candidates = new List<string>();
 85
 86        void Add(string? path)
 87        {
 88            if (!string.IsNullOrWhiteSpace(path))
 89                candidates.Add(path.TrimEnd(Path.DirectorySeparatorChar));
 90        }
 91
 1092        Add(Environment.GetEnvironmentVariable("DOTNET_ROOT"));
 1093        Add(Environment.GetEnvironmentVariable("ProgramFiles") is var pf ? Path.Combine(pf ?? string.Empty, "dotnet") : 
 1094        Add(Environment.GetEnvironmentVariable("ProgramFiles(x86)") is var pf86 ? Path.Combine(pf86 ?? string.Empty, "do
 1095        Add(Environment.GetEnvironmentVariable("LOCALAPPDATA") is var lad ? Path.Combine(lad ?? string.Empty, "Microsoft
 96
 1097        return candidates.Distinct(StringComparer.OrdinalIgnoreCase).Where(Directory.Exists);
 98    }
 99}