forked from cosmo-lang/cosmo-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstallCosmo.cs
325 lines (280 loc) · 9.74 KB
/
InstallCosmo.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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Security.Principal;
using System.Text;
using Avalonia.Threading;
using MessageBox.Avalonia;
namespace CosmoInstaller;
public static class Installation
{
private static readonly float _step = (1f / 13f) * 100;
private static float _progress = 0;
private static Action<int>? _updateProgress;
private static Action<string>? _updateTitle;
private static Action? _markErrored;
private static Action? _markFinished;
private static bool _errored = false;
private static bool _finished = false;
public static void InstallCosmo(
Action<int> updateProgress,
Action<string> updateTitle,
Action markErrored,
Action markFinished,
string path,
string installerPath
) {
_updateProgress = updateProgress;
_updateTitle = updateTitle;
_markErrored = markErrored;
_markFinished = markFinished;
if (OperatingSystem.IsWindows() && !IsAdmin())
ShowErrorMessageBox($"Cannot install. You are not running with elevated privileges.\nRestart the app as an administrator and try again.");
if (_errored) return;
Log("Creating installation environment...");
if (Directory.Exists(path))
Log("Installation directory exists, skipping creation...");
else
try
{
Directory.CreateDirectory(path);
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to create directory (run as administrator?): {err.Message}");
}
Log("Changing environment directory...");
StepProgress();
try
{
Environment.CurrentDirectory = path;
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to change directory (run as administrator?): {err.Message}");
}
StepProgress();
Log("Pulling repository...");
string[] dirEntries;
try
{
dirEntries = Directory.GetFileSystemEntries(".");
if (dirEntries.Length != 0)
{
if (OperatingSystem.IsWindows())
ExecuteCommand("Failed to remove shard.lock", "powershell", "-c \"rm -Force ./shard.lock\"");
else
ExecuteCommand("Failed to remove shard.lock", "rm", "-f shard.lock");
ExecuteGitCommand("pull origin master --allow-unrelated-histories", "Failed to pull from the repository (is git installed?)");
}
else
ExecuteGitCommand("clone https://github.com/cosmo-lang/cosmo.git .", "Failed to clone the repository (is git installed?)");
}
catch (Exception err)
{
ShowErrorMessageBox($"Failed to read the directory (run as administrator?): {err.Message}");
}
StepProgress();
Log("Fetching tags...");
ExecuteGitCommand("fetch --tags", "Failed to fetch release tags");
StepProgress();
Log("Fetching latest release...");
string latestTag = ExecuteGitCommand("describe --tags --abbrev=0", "Failed to get the latest release tag");
StepProgress();
Log("Checking out latest release...");
string result = ExecuteGitCommand($"checkout {latestTag}", "Failed to checkout the latest release");
StepProgress();
Log("Checking for Crystal installation...");
ProcessResult? crystalCheckOutput = null;
try
{
crystalCheckOutput = ExecuteCommand(null, "crystal", "-v");
}
catch (Win32Exception) // shut up exceptions saying not found
{
}
if (crystalCheckOutput == null || crystalCheckOutput.ExitCode != 0)
{
Log("Installing Crystal...");
if (OperatingSystem.IsWindows())
{
Log("Checking for Scoop installation...");
ExecuteCommand("Failed to install Crystal: \nScoop is not installed, but is required to install Crystal for Windows. \nIf you don't want to use Scoop, please manually install Crystal.", "powershell.exe", "-c \"scoop\"");
Log("Found Scoop!");
Log("Adding Crystal bucket...");
ExecuteCommand("Failed to add Crystal bucket", "scoop", "bucket add crystal-preview https://github.com/neatorobito/scoop-crystal");
StepProgress();
ProcessResult crtCheckOutput = ExecuteCommand("Failed to execute 'where'", "where", "cl.exe");
if (crtCheckOutput.ExitCode != 0)
{
Log("Installing C++ build tools...");
ExecuteCommand("Failed to install C++ build tools", "scoop", "install vs_2022_cpp_build_tools");
}
StepProgress();
Log("Installing Crystal bucket...");
ExecuteCommand("Failed to install Crystal bucket", "scoop", "install crystal");
StepProgress();
}
else if (OperatingSystem.IsLinux())
{
ExecuteCommand("Failed to install Crystal: \nSnapcraft is not installed, but is required to install Crystal for Linux. \nIf you don't want to use Snapcraft, please manually install Crystal.", "snap");
StepProgress();
string scriptPath = Path.Combine(installerPath, "snapd_setup.sh");
ExecuteCommand("Failed to chmod Snapcraft setup script", "chmod", "+x", scriptPath);
StepProgress();
ExecuteCommand("Failed to setup", "pkexec", "--disable-internal-agent", scriptPath);
StepProgress();
}
else if (OperatingSystem.IsMacOS())
{
ExecuteCommand("Failed to install Crystal", "brew", "install crystal");
StepProgress();
StepProgress();
StepProgress();
}
Log("Crystal has been successfully installed.\nPlease restart the installer and try again.\nYou may need to restart your shell, or even\nyour machine.");
_finished = true;
_markFinished!();
return;
}
else
{
Log("Crystal is already installed, continuing...");
if (OperatingSystem.IsWindows())
{
Log("Updating Scoop + Crystal...");
ExecuteCommand("Failed to update Scoop", "powershell.exe", "-c \"scoop update\"");
ExecuteCommand("Failed to update Crystal bucket", "powershell.exe", "-c \"scoop update crystal\"");
// maybe dont terminate installation if it fails to update
}
}
if (_finished) return;
StepProgress();
Log("Building Cosmo...");
Log("Installing dependencies...");
ExecuteCommand("Failed to install Cosmo dependencies", "shards", "install");
StepProgress();
Log("Compiling... (this may take a while)");
ExecuteCommand("Failed to build Cosmo", "shards", "build --release");
StepProgress();
Log("Successfully built Cosmo.");
if (!OperatingSystem.IsWindows())
AddToPath(path);
StepProgress();
Log("Successfully installed Cosmo.");
}
private static bool IsAdmin()
{
bool isAdmin;
try
{
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(user);
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (UnauthorizedAccessException)
{
isAdmin = false;
}
catch (Exception)
{
isAdmin = false;
}
return isAdmin;
}
private static void AddToPath(string path)
{
if (_errored) return;
Log("Adding Cosmo to PATH...");
string binPath = Path.Combine(path, "bin");
string homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string profilePath = Path.Combine(homeDir, ".bashrc");
string pathUpdateCmd = $"export PATH=\"{binPath}:$PATH\"";
WriteProfile(profilePath, pathUpdateCmd);
Log("Successfully added Cosmo to your PATH.");
}
private static string ExecuteGitCommand(string arguments, string errorMessage)
{
ProcessResult result = ExecuteCommand(errorMessage, "git", arguments.Split(' '));
return result.StandardOutput.Trim();
}
private static ProcessResult ExecuteCommand(string? errorMessage, string command, params string[] arguments)
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = command,
Arguments = string.Join(' ', arguments),
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
Process process = new Process
{
StartInfo = startInfo
};
StringBuilder output = new StringBuilder();
StringBuilder 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}: {result.StandardError}");
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 Log(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; } = "";
}