forked from LykosAI/StabilityMatrix
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIPrerequisiteHelper.cs
More file actions
303 lines (268 loc) · 11 KB
/
IPrerequisiteHelper.cs
File metadata and controls
303 lines (268 loc) · 11 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System.Diagnostics;
using System.Runtime.Versioning;
using StabilityMatrix.Core.Exceptions;
using StabilityMatrix.Core.Helper.HardwareInfo;
using StabilityMatrix.Core.Models;
using StabilityMatrix.Core.Models.FileInterfaces;
using StabilityMatrix.Core.Models.Packages;
using StabilityMatrix.Core.Models.Progress;
using StabilityMatrix.Core.Processes;
using StabilityMatrix.Core.Python;
namespace StabilityMatrix.Core.Helper;
public interface IPrerequisiteHelper
{
string GitBinPath { get; }
bool IsPythonInstalled { get; }
bool IsVcBuildToolsInstalled { get; }
bool IsHipSdkInstalled { get; }
Task InstallAllIfNecessary(IProgress<ProgressReport>? progress = null);
Task InstallUvIfNecessary(IProgress<ProgressReport>? progress = null);
string UvExePath { get; }
bool IsUvInstalled { get; }
DirectoryPath DotnetDir { get; }
Task UnpackResourcesIfNecessary(IProgress<ProgressReport>? progress = null);
Task InstallGitIfNecessary(IProgress<ProgressReport>? progress = null);
Task InstallPythonIfNecessary(IProgress<ProgressReport>? progress = null);
[SupportedOSPlatform("Windows")]
Task InstallVcRedistIfNecessary(IProgress<ProgressReport>? progress = null);
/// <summary>
/// Run embedded git with the given arguments.
/// </summary>
Task RunGit(
ProcessArgs args,
Action<ProcessOutput>? onProcessOutput = null,
string? workingDirectory = null
);
Task<ProcessResult> GetGitOutput(ProcessArgs args, string? workingDirectory = null);
async Task<bool> CheckIsGitRepository(string repositoryPath)
{
var result = await GetGitOutput(["rev-parse", "--is-inside-work-tree"], repositoryPath)
.ConfigureAwait(false);
return result.ExitCode == 0 && result.StandardOutput?.Trim().ToLowerInvariant() == "true";
}
async Task<GitVersion> GetGitRepositoryVersion(string repositoryPath)
{
var version = new GitVersion();
// Get tag
if (
await GetGitOutput(["describe", "--tags", "--abbrev=0"], repositoryPath).ConfigureAwait(false) is
{ IsSuccessExitCode: true } tagResult
)
{
version = version with { Tag = tagResult.StandardOutput?.Trim() };
}
// Get branch
if (
await GetGitOutput(["rev-parse", "--abbrev-ref", "HEAD"], repositoryPath).ConfigureAwait(false) is
{ IsSuccessExitCode: true } branchResult
)
{
version = version with { Branch = branchResult.StandardOutput?.Trim() };
}
// Get commit sha
if (
await GetGitOutput(["rev-parse", "HEAD"], repositoryPath).ConfigureAwait(false) is
{ IsSuccessExitCode: true } shaResult
)
{
version = version with { CommitSha = shaResult.StandardOutput?.Trim() };
}
return version;
}
async Task CloneGitRepository(
string rootDir,
string repositoryUrl,
GitVersion? version = null,
Action<ProcessOutput>? onProcessOutput = null
)
{
// Decide shallow clone only when not pinning to arbitrary commit post-clone
var isShallowOk = version is null || version.Tag is not null;
var cloneArgs = new ProcessArgsBuilder("clone");
if (isShallowOk)
{
cloneArgs = cloneArgs.AddArgs("--depth", "1", "--single-branch");
}
if (!string.IsNullOrWhiteSpace(version?.Tag))
{
cloneArgs = cloneArgs.AddArgs("--branch", version.Tag!);
}
else if (!string.IsNullOrWhiteSpace(version?.Branch))
{
cloneArgs = cloneArgs.AddArgs("--branch", version.Branch!);
}
cloneArgs = cloneArgs.AddArgs(repositoryUrl);
await RunGit(cloneArgs.ToProcessArgs(), onProcessOutput, rootDir).ConfigureAwait(false);
// If pinning to a specific commit, we need a destination directory to continue
if (!string.IsNullOrWhiteSpace(version?.CommitSha))
{
await RunGit(["fetch", "--depth", "1", "origin", version.CommitSha!], onProcessOutput, rootDir)
.ConfigureAwait(false);
await RunGit(["checkout", "--force", version.CommitSha!], onProcessOutput, rootDir)
.ConfigureAwait(false);
await RunGit(
["submodule", "update", "--init", "--recursive", "--depth", "1"],
onProcessOutput,
rootDir
)
.ConfigureAwait(false);
}
}
async Task UpdateGitRepository(
string repositoryDir,
string repositoryUrl,
GitVersion version,
Action<ProcessOutput>? onProcessOutput = null,
bool usePrune = false,
bool allowRebaseFallback = true,
bool allowResetHardFallback = false
)
{
if (!Directory.Exists(Path.Combine(repositoryDir, ".git")))
{
await RunGit(["init"], onProcessOutput, repositoryDir).ConfigureAwait(false);
await RunGit(["remote", "add", "origin", repositoryUrl], onProcessOutput, repositoryDir)
.ConfigureAwait(false);
}
// Ensure origin url matches the expected one
await RunGit(["remote", "set-url", "origin", repositoryUrl], onProcessOutput, repositoryDir)
.ConfigureAwait(false);
// Specify Tag
if (version.Tag is not null)
{
await RunGit(["fetch", "--tags", "--force"], onProcessOutput, repositoryDir)
.ConfigureAwait(false);
await RunGit(["checkout", version.Tag, "--force"], onProcessOutput, repositoryDir)
.ConfigureAwait(false);
// Update submodules
await RunGit(["submodule", "update", "--init", "--recursive"], onProcessOutput, repositoryDir)
.ConfigureAwait(false);
}
// Specify Branch + CommitSha
else if (version.Branch is not null && version.CommitSha is not null)
{
await RunGit(["fetch", "--force", "origin", version.CommitSha], onProcessOutput, repositoryDir)
.ConfigureAwait(false);
await RunGit(["checkout", "--force", version.CommitSha], onProcessOutput, repositoryDir)
.ConfigureAwait(false);
// Update submodules
await RunGit(
["submodule", "update", "--init", "--recursive", "--depth", "1"],
onProcessOutput,
repositoryDir
)
.ConfigureAwait(false);
}
// Specify Branch (Use latest commit)
else if (version.Branch is not null)
{
// Fetch (optional prune)
var fetchArgs = new ProcessArgsBuilder("fetch", "--force");
if (usePrune)
fetchArgs = fetchArgs.AddArg("--prune");
fetchArgs = fetchArgs.AddArg("origin");
await RunGit(fetchArgs.ToProcessArgs(), onProcessOutput, repositoryDir).ConfigureAwait(false);
// Checkout
await RunGit(["checkout", "--force", version.Branch], onProcessOutput, repositoryDir)
.ConfigureAwait(false);
// Try ff-only first
var ffOnlyResult = await GetGitOutput(
["pull", "--ff-only", "--autostash", "origin", version.Branch],
repositoryDir
)
.ConfigureAwait(false);
if (ffOnlyResult.ExitCode != 0)
{
if (allowRebaseFallback)
{
var rebaseResult = await GetGitOutput(
["pull", "--rebase", "--autostash", "origin", version.Branch],
repositoryDir
)
.ConfigureAwait(false);
rebaseResult.EnsureSuccessExitCode();
}
else if (allowResetHardFallback)
{
await RunGit(
["fetch", "--force", "origin", version.Branch],
onProcessOutput,
repositoryDir
)
.ConfigureAwait(false);
await RunGit(
["reset", "--hard", $"origin/{version.Branch}"],
onProcessOutput,
repositoryDir
)
.ConfigureAwait(false);
}
else
{
ffOnlyResult.EnsureSuccessExitCode();
}
}
// Update submodules
await RunGit(
["submodule", "update", "--init", "--recursive", "--depth", "1"],
onProcessOutput,
repositoryDir
)
.ConfigureAwait(false);
}
// Not specified
else
{
throw new ArgumentException(
"Version must have a tag, branch + commit sha, or branch only.",
nameof(version)
);
}
}
Task<ProcessResult> GetGitRepositoryRemoteOriginUrl(string repositoryPath)
{
return GetGitOutput(["config", "--get", "remote.origin.url"], repositoryPath);
}
Task InstallTkinterIfNecessary(IProgress<ProgressReport>? progress = null);
Task RunNpm(
ProcessArgs args,
string? workingDirectory = null,
Action<ProcessOutput>? onProcessOutput = null,
IReadOnlyDictionary<string, string>? envVars = null
);
AnsiProcess RunNpmDetached(
ProcessArgs args,
string? workingDirectory = null,
Action<ProcessOutput>? onProcessOutput = null,
IReadOnlyDictionary<string, string>? envVars = null
);
Task InstallNodeIfNecessary(IProgress<ProgressReport>? progress = null);
Task InstallPackageRequirements(
BasePackage package,
PyVersion? pyVersion = null,
IProgress<ProgressReport>? progress = null
);
Task InstallPackageRequirements(
List<PackagePrerequisite> prerequisites,
PyVersion? pyVersion = null,
IProgress<ProgressReport>? progress = null
);
Task InstallDotnetIfNecessary(IProgress<ProgressReport>? progress = null);
Task<Process> RunDotnet(
ProcessArgs args,
string? workingDirectory = null,
Action<ProcessOutput>? onProcessOutput = null,
IReadOnlyDictionary<string, string>? envVars = null,
bool waitForExit = true
);
Task<bool> FixGitLongPaths();
Task AddMissingLibsToVenv(
DirectoryPath installedPackagePath,
PyBaseInstall baseInstall,
IProgress<ProgressReport>? progress = null
);
Task InstallPythonIfNecessary(PyVersion version, IProgress<ProgressReport>? progress = null);
Task InstallVirtualenvIfNecessary(PyVersion version, IProgress<ProgressReport>? progress = null);
Task InstallTkinterIfNecessary(PyVersion version, IProgress<ProgressReport>? progress = null);
string? GetGfxArchFromAmdGpuName(GpuInfo? gpu = null);
}