-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMsbuildLocation.cs
36 lines (32 loc) · 1.51 KB
/
MsbuildLocation.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
using System.Runtime.InteropServices;
namespace Msbuild.Clean;
internal record class MsbuildLocation(string FullPath, MsbuildExecType ExecType, MsbuildLocationType MsbuildLocationType) {
public string ExecName => ExecType switch {
MsbuildExecType.ExecTypeDllDotnet => RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "dotnet" : $"dotnet.exe",
MsbuildExecType.ExecTypeExe => FullPath,
_ => throw new InvalidOperationException()
};
internal static MsbuildLocation? From(string fullPath) {
static MsbuildLocation ResolveFromFile(string path) {
if (0 == string.Compare(Path.GetExtension(path), ".exe", StringComparison.OrdinalIgnoreCase)) {
return new MsbuildLocation(path, MsbuildExecType.ExecTypeExe, MsbuildLocationType.Manual);
}
return new MsbuildLocation(path, MsbuildExecType.ExecTypeDllDotnet, MsbuildLocationType.Manual);
}
if (File.Exists(fullPath)) {
return ResolveFromFile(fullPath);
}
else {
if (Directory.Exists(fullPath)) {
var path = Directory.GetFiles(fullPath).FirstOrDefault(file => {
var fileName = Path.GetFileName(file);
return fileName.ToLowerInvariant() == "msbuild.exe" || fileName.ToLowerInvariant() == "msbuild.dll";
});
if (path is not null) {
return ResolveFromFile(path);
}
}
}
return default;
}
}