-
Notifications
You must be signed in to change notification settings - Fork 1
/
InstallRbxcs.cs
369 lines (332 loc) · 11.6 KB
/
InstallRbxcs.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using MessageBox.Avalonia;
using Avalonia.Threading;
using Installer.Views;
using Installer.ViewModels;
using Avalonia.Controls;
using System.Collections.Generic;
using LibGit2Sharp;
using System.Net;
using System.Linq;
namespace Installer;
public static class Installation
{
private const string _sourceName = "rbxcs";
private const string _sourceURL = "https://nuget.pkg.github.com/roblox-csharp/index.json";
private const uint _steps = 9;
private static readonly float _step = (1f / _steps) * 100;
private static float _progress = 0;
private static Action<int>? _updateProgress;
private static Action<string>? _updateTitle;
private static Action? _markErrored;
private static bool _errored = false;
private static string _path = "";
private static Tag? _latestTag;
public static async Task InstallRbxcs(
Action<int> updateProgress,
Action<string> updateTitle,
Action markErrored,
string path
)
{
_updateProgress = updateProgress;
_updateTitle = updateTitle;
_markErrored = markErrored;
_path = path;
if (_errored) return;
Display("Creating installation environment...");
if (Directory.Exists(path))
{
Display("Installation directory exists, skipping creation...");
}
else
{
try
{
Directory.CreateDirectory(path);
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to create directory (run as administrator?): {err.Message}");
}
}
Display("Changing environment directory...");
StepProgress();
try
{
Environment.CurrentDirectory = path;
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to change directory (run as administrator?): {err.Message}");
}
const string repoURL = "https://github.com/roblox-csharp/roblox-cs.git";
const string remoteName = "origin";
var clonePath = Path.Combine(path, ".");
var gitFolder = Path.Combine(clonePath, ".git");
Repository? repo = null;
if (Directory.Exists(gitFolder))
{
try
{
repo = new Repository(gitFolder);
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to create repository: {err.Message}\n{string.Join('\n', err.StackTrace)}");
return;
}
}
StepProgress();
Remote origin = null!;
IEnumerable<string> refspecs = null!;
Display("Pulling repository...");
try
{
var directoryEntries = Directory.GetFileSystemEntries(clonePath);
if (directoryEntries.Length == 0)
{
try
{
Repository.Clone(repoURL, clonePath);
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to clone the compiler repository: {err.Message}");
return;
}
}
if (repo == null)
{
try
{
repo = new Repository(gitFolder);
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to create repository: {err.Message}\n{string.Join('\n', err.StackTrace)}");
return;
}
}
origin = repo.Network.Remotes[remoteName];
refspecs = origin.FetchRefSpecs.Select(refspec => refspec.ToString()).OfType<string>();
GitPull(repo, origin, refspecs);
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to read the compiler repository directory (run as administrator?): {err.Message}");
return;
}
StepProgress();
Display("Fetching tags...");
if (repo == null) return;
try
{
repo.Network.Fetch(origin.Name, refspecs, new FetchOptions()
{
TagFetchMode = TagFetchMode.All
});
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to fetch release tags from repository: {err.Message}");
}
StepProgress();
Display("Fetching latest release...");
var tags = repo.Tags;
_latestTag = tags.OrderByDescending(tag => tag.FriendlyName).FirstOrDefault()!;
if (_latestTag == null)
{
ShowErrorMessageBox($"Failed to get the latest release tag, tags found: {repo.Tags.Count()}");
return;
}
StepProgress();
Display("Checking out latest release...");
try
{
var commit = repo.Commits.FirstOrDefault(commit => commit == (Commit)_latestTag.Target);
Commands.Checkout(repo, commit);
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to checkout the latest release: {err.Message}");
}
StepProgress();
Display("Building roblox-cs...");
var sourcesResult = ExecuteCommand(null, "dotnet", $"nuget list source");
var sources = sourcesResult.StandardOutput;
var sourceAlreadyAdded = sources.Contains(_sourceName) && sources.Contains(_sourceURL);
await Dispatcher.UIThread.InvokeAsync(() =>
{
new SourceCredentialsWindow(sourceAlreadyAdded)
{
DataContext = new SourceCredentialsWindowViewModel()
};
});
}
public static void OnCredentialsAcquired(SourceCredentialsWindow credentialsWindow, bool sourceAlreadyAdded)
{
if (!sourceAlreadyAdded)
{
// add the source
var usernameTextBox = credentialsWindow.FindControl<TextBox>("UsernameBox")!;
var tokenTextBox = credentialsWindow.FindControl<TextBox>("TokenBox")!;
Display("Adding NuGet source for RobloxCS packages...");
ExecuteCommand(null, "dotnet", $"nuget add source \"{_sourceURL}\" -u {usernameTextBox.Text} -p {tokenTextBox.Text} -n {_sourceName}");
StepProgress();
}
Display("Compiling...");
ExecuteCommand("Failed to compile roblox-cs", "dotnet", "build -c Release");
StepProgress();
Display("Successfully compiled roblox-cs.");
if (OperatingSystem.IsWindows())
{
UpdateEnvironmentPath(_path);
}
else
{
UpdateShellProfilePath(_path);
}
Display("Successfully added roblox-cs to your PATH.");
StepProgress();
Display($"Successfully installed roblox-cs ({_latestTag?.FriendlyName ?? "???"}).");
}
private static void GitPull(Repository repo, Remote remote, IEnumerable<string> refspecs)
{
try
{
repo.Network.Fetch(remote.Name, refspecs);
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to fetch origin/master from repository: {err.Message}");
}
try
{
var branchToMerge = repo.Branches[$"{remote.Name}/master"];
var signature = new Signature("rbxcs-installer", "[email protected]", DateTimeOffset.Now);
var mergeResult = repo.Merge(branchToMerge, signature, new MergeOptions
{
FileConflictStrategy = CheckoutFileConflictStrategy.Theirs
});
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to merge: {err.Message}");
}
}
private static void UpdateEnvironmentPath(string path)
{
if (_errored) return;
Display("Adding roblox-cs to PATH in system environment...");
var binPath = Path.GetFullPath(Path.Combine(path, "bin"));
var currentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
if (new List<string>(currentPath?.Split(';') ?? []).Contains(binPath))
{
Display("Already have bin folder in PATH variable! Skipping step...");
return;
}
var updatedPath = $"{currentPath};{binPath}";
try
{
Environment.SetEnvironmentVariable("PATH", updatedPath, EnvironmentVariableTarget.Machine);
} catch (Exception exception)
{
ShowErrorMessageBox($"Failed to set PATH variable (run as administrator?): {exception.Message}");
}
}
private static void UpdateShellProfilePath(string path)
{
if (_errored) return;
Display("Adding roblox-cs to PATH in shell profile...");
var binPath = Path.Combine(path, "bin");
var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var profilePath = Path.Combine(homeDir, ".bashrc");
var pathUpdateCmd = $"export PATH=\"{binPath}:$PATH\"";
WriteProfile(profilePath, pathUpdateCmd);
}
private static ProcessResult ExecuteCommand(string? errorMessage, string command, params string[] arguments)
{
var startInfo = new ProcessStartInfo
{
FileName = command,
Arguments = string.Join(' ', arguments),
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process
{
StartInfo = startInfo
};
var output = new StringBuilder();
var error = new StringBuilder();
process.OutputDataReceived += (s, e) => output.AppendLine(e.Data);
process.ErrorDataReceived += (s, e) => error.AppendLine(e.Data);
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
var result = new ProcessResult
{
ExitCode = process.ExitCode,
StandardOutput = output.ToString(),
StandardError = error.ToString()
};
if (result.ExitCode != 0 && errorMessage != null)
{
ShowErrorMessageBox($"{errorMessage}: {(!string.IsNullOrEmpty(result.StandardError) ? result.StandardError : result.StandardOutput)}");
}
return result;
}
private static void WriteProfile(string path, string pathUpdateCmd)
{
if (_errored) return;
try
{
using (StreamWriter file = File.AppendText(path))
file.WriteLine(pathUpdateCmd);
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to write to shell profile: {err.Message}");
}
}
private static void StepProgress()
{
if (_errored) return;
_progress += _step;
_updateProgress!((int)_progress);
}
private static void ShowErrorMessageBox(string message)
{
if (_errored) return;
_errored = true;
_markErrored!();
_updateTitle!("Error!");
Dispatcher.UIThread.InvokeAsync(() =>
{
MessageBoxManager
.GetMessageBoxStandardWindow("Error", message)
.Show()
.ContinueWith(_ => Environment.Exit(1));
});
}
private static void Display(string msg)
{
if (_errored) return;
_updateTitle!(msg);
Console.WriteLine(msg);
}
}
public class ProcessResult
{
public int ExitCode { get; set; }
public string StandardOutput { get; set; } = "";
public string StandardError { get; set; } = "";
}