-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
DotNetExe.cs
71 lines (60 loc) · 2.6 KB
/
DotNetExe.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// This file has been modified from the original form. See Notice.txt in the project root for more information.
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
namespace McMaster.Extensions.CommandLineUtils
{
/// <summary>
/// Utilities for finding the "dotnet.exe" file from the currently running .NET Core application.
/// </summary>
public static class DotNetExe
{
private const string FileName = "dotnet";
static DotNetExe()
{
FullPath = TryFindDotNetExePath();
}
/// <summary>
/// The full filepath to the .NET Core CLI executable.
/// <para>
/// May be <c>null</c> if the CLI cannot be found.
/// </para>
/// </summary>
/// <returns>The path or null</returns>
/// <seealso cref="FullPathOrDefault" />
public static string? FullPath { get; }
/// <summary>
/// Finds the full filepath to the .NET Core CLI executable,
/// or returns a string containing the default name of the .NET Core muxer ('dotnet').
/// <returns>The path or a string named 'dotnet'</returns>
/// </summary>
public static string FullPathOrDefault()
=> FullPath ?? FileName;
private static string? TryFindDotNetExePath()
{
var fileName = FileName;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
fileName += ".exe";
}
var mainModule = Process.GetCurrentProcess().MainModule;
if (!string.IsNullOrEmpty(mainModule?.FileName)
&& Path.GetFileName(mainModule.FileName).Equals(fileName, StringComparison.OrdinalIgnoreCase))
{
return mainModule.FileName;
}
// DOTNET_ROOT specifies the location of the .NET runtimes, if they are not installed in the default location.
var dotnetRoot = Environment.GetEnvironmentVariable("DOTNET_ROOT");
if (string.IsNullOrEmpty(dotnetRoot))
{
// fall back to default location
// https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables#dotnet_root-dotnet_rootx86
dotnetRoot = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "C:\\Program Files\\dotnet" : "/usr/local/share/dotnet";
}
return Path.Combine(dotnetRoot, fileName);
}
}
}