-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDotNet.cs
177 lines (157 loc) · 6.71 KB
/
DotNet.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
namespace Turkey
{
public class DotNet
{
private string _dotnetPath;
public DotNet()
{
_dotnetPath = FindProgramInPath("dotnet");
if (_dotnetPath is not null)
{
// resolve link target.
_dotnetPath = new FileInfo(_dotnetPath).ResolveLinkTarget(returnFinalTarget: true)?.FullName ?? _dotnetPath;
}
}
private string DotnetFileName => _dotnetPath ?? throw new FileNotFoundException("dotnet");
private string DotnetRoot => Path.GetDirectoryName(DotnetFileName);
public List<Version> RuntimeVersions
{
get
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = DotnetFileName,
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = "--list-runtimes",
};
using (Process p = Process.Start(startInfo))
{
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
var list = output
.Split("\n", StringSplitOptions.RemoveEmptyEntries)
.Where(line => line.StartsWith("Microsoft.NETCore.App", StringComparison.Ordinal))
.Select(line => line.Split(" ")[1])
.Select(versionString => Version.Parse(versionString))
.OrderBy(x => x)
.ToList();
return list;
}
}
}
public Version LatestRuntimeVersion
{
get
{
return RuntimeVersions.Last();
}
}
public bool IsCoreClrRuntime(Version runtimeVersion)
=> IsCoreClrRuntime(DotnetRoot, runtimeVersion);
public bool IsMonoRuntime(Version runtimeVersion)
=> !IsCoreClrRuntime(runtimeVersion);
public List<Version> SdkVersions
{
get
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = DotnetFileName,
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = "--list-sdks",
};
using (Process p = Process.Start(startInfo))
{
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
var list = output
.Split("\n", StringSplitOptions.RemoveEmptyEntries)
.Select(line => line.Split(" ")[0])
.Select(versionString => Version.Parse(versionString))
.OrderBy(x => x)
.ToList();
return list;
}
}
}
public Version LatestSdkVersion
{
get
{
return SdkVersions.LastOrDefault();
}
}
public Task<int> BuildAsync(DirectoryInfo workingDirectory, IReadOnlyDictionary<string, string> environment, Action<string> logger, CancellationToken token)
{
var arguments = new string[]
{
"build",
"-p:UseRazorBuildServer=false",
"-p:UseSharedCompilation=false",
"-m:1",
};
return RunDotNetCommandAsync(workingDirectory, arguments, environment, logger, token);
}
public Task<int> RunAsync(DirectoryInfo workingDirectory, IReadOnlyDictionary<string, string> environment, Action<string> logger, CancellationToken token)
=> RunDotNetCommandAsync(workingDirectory, new string[] { "run", "--no-restore", "--no-build"} , environment, logger, token);
public Task<int> TestAsync(DirectoryInfo workingDirectory, IReadOnlyDictionary<string, string> environment, Action<string> logger, CancellationToken token)
=> RunDotNetCommandAsync(workingDirectory, new string[] { "test", "--no-restore", "--no-build"} , environment, logger, token);
private async Task<int> RunDotNetCommandAsync(DirectoryInfo workingDirectory, string[] commands, IReadOnlyDictionary<string, string> environment, Action<string> logger, CancellationToken token)
{
var arguments = string.Join(" ", commands);
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = DotnetFileName,
Arguments = arguments,
WorkingDirectory = workingDirectory.FullName,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
startInfo.EnvironmentVariables.Clear();
foreach (var (key, value) in environment)
{
startInfo.EnvironmentVariables.Add(key, value);
}
return await ProcessRunner.RunAsync(startInfo, logger, token).ConfigureAwait(false);
}
private static bool IsCoreClrRuntime(string dotnetRoot, Version version)
{
string[] runtimeDirectories = Directory.GetDirectories(Path.Combine(dotnetRoot, "shared", "Microsoft.NETCore.App"))
.Where(dir => Version.Parse(Path.GetFileName(dir)) == version)
.ToArray();
if (runtimeDirectories.Length == 0)
{
throw new DirectoryNotFoundException($"No runtime directory for {version} found in {dotnetRoot}.");
}
if (runtimeDirectories.Length > 1)
{
throw new DirectoryNotFoundException($"Multiple runtime directories found for {version} in {dotnetRoot}.");
}
string runtimeDir = runtimeDirectories[0];
return File.Exists(Path.Combine(runtimeDir, "libcoreclrtraceptprovider.so"));
}
#nullable enable
private static string? FindProgramInPath(string program)
#nullable disable
{
string[] paths = Environment.GetEnvironmentVariable("PATH")?.Split(':', StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
foreach (string p in paths)
{
if (Path.Combine(p, program) is var filename && File.Exists(filename))
{
return filename;
}
}
return null;
}
}
}