forked from LykosAI/StabilityMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateViewModel.cs
More file actions
374 lines (317 loc) · 11.9 KB
/
UpdateViewModel.cs
File metadata and controls
374 lines (317 loc) · 11.9 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
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
370
371
372
373
374
using System.Text.RegularExpressions;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Injectio.Attributes;
using Microsoft.Extensions.Logging;
using Semver;
using StabilityMatrix.Avalonia.Languages;
using StabilityMatrix.Avalonia.ViewModels.Base;
using StabilityMatrix.Avalonia.Views.Dialogs;
using StabilityMatrix.Core.Attributes;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Models.Progress;
using StabilityMatrix.Core.Models.Update;
using StabilityMatrix.Core.Processes;
using StabilityMatrix.Core.Services;
using StabilityMatrix.Core.Updater;
namespace StabilityMatrix.Avalonia.ViewModels.Dialogs;
[View(typeof(UpdateDialog))]
[ManagedService]
[RegisterSingleton<UpdateViewModel>]
public partial class UpdateViewModel : ContentDialogViewModelBase
{
private readonly ILogger<UpdateViewModel> logger;
private readonly ISettingsManager settingsManager;
private readonly IHttpClientFactory httpClientFactory;
private readonly IUpdateHelper updateHelper;
private bool isLoaded;
[ObservableProperty]
private bool isUpdateAvailable;
[ObservableProperty]
private UpdateInfo? updateInfo;
[ObservableProperty]
private string? releaseNotes;
[ObservableProperty]
private string? updateText;
[ObservableProperty]
private int progressValue;
[ObservableProperty]
private bool isProgressIndeterminate;
[ObservableProperty]
private bool showProgressBar;
[ObservableProperty]
private string? currentVersionText;
[ObservableProperty]
private string? newVersionText;
[GeneratedRegex(
@"(##\s*(v[0-9]+\.[0-9]+\.[0-9]+(?:-(?:[0-9A-Za-z-.]+))?)((?:\n|.)+?))(?=(##\s*v[0-9]+\.[0-9]+\.[0-9]+)|\z)"
)]
private static partial Regex RegexChangelog();
public UpdateViewModel(
ILogger<UpdateViewModel> logger,
ISettingsManager settingsManager,
IHttpClientFactory httpClientFactory,
IUpdateHelper updateHelper
)
{
this.logger = logger;
this.settingsManager = settingsManager;
this.httpClientFactory = httpClientFactory;
this.updateHelper = updateHelper;
EventManager.Instance.UpdateAvailable += (_, info) =>
{
IsUpdateAvailable = true;
UpdateInfo = info;
};
}
public async Task Preload()
{
if (UpdateInfo is null)
return;
ReleaseNotes = await GetReleaseNotes(UpdateInfo.Changelog.ToString());
}
partial void OnUpdateInfoChanged(UpdateInfo? value)
{
CurrentVersionText = $"v{Compat.AppVersion.ToDisplayString()}";
NewVersionText = $"v{value?.Version.ToDisplayString()}";
}
public override async Task OnLoadedAsync()
{
if (!isLoaded)
{
await Preload();
}
}
/// <inheritdoc />
public override void OnUnloaded()
{
base.OnUnloaded();
isLoaded = false;
}
[RelayCommand]
private async Task InstallUpdate()
{
if (UpdateInfo == null)
{
return;
}
ShowProgressBar = true;
IsProgressIndeterminate = true;
UpdateText = string.Format(Resources.TextTemplate_UpdatingPackage, Resources.Label_StabilityMatrix);
try
{
await updateHelper.DownloadUpdate(
UpdateInfo,
new Progress<ProgressReport>(report =>
{
ProgressValue = Convert.ToInt32(report.Percentage);
IsProgressIndeterminate = report.IsIndeterminate;
})
);
}
catch (Exception e)
{
logger.LogWarning(e, "Failed to download update");
var dialog = DialogHelper.CreateMarkdownDialog(
$"{e.GetType().Name}: {e.Message}",
Resources.Label_UnexpectedErrorOccurred
);
await dialog.ShowAsync();
return;
}
// On unix, we need to set the executable bit
if (Compat.IsUnix)
{
File.SetUnixFileMode(
UpdateHelper.ExecutablePath.FullPath,
// 0755
UnixFileMode.UserRead
| UnixFileMode.UserWrite
| UnixFileMode.UserExecute
| UnixFileMode.GroupRead
| UnixFileMode.GroupExecute
| UnixFileMode.OtherRead
| UnixFileMode.OtherExecute
);
}
// Handle Linux AppImage update
if (Compat.IsLinux && Environment.GetEnvironmentVariable("APPIMAGE") is { } appImage)
{
try
{
var updateScriptPath = UpdateHelper.UpdateFolder.JoinFile("update_script.sh").FullPath;
var newAppImage = UpdateHelper.ExecutablePath.FullPath;
var scriptContent = $"""
#!/bin/bash
PID={Environment.ProcessId}
NEW_APPIMAGE="{newAppImage.Replace("\"", "\\\"")}"
OLD_APPIMAGE="{appImage.Replace("\"", "\\\"")}"
# Wait for the process to exit
while kill -0 "$PID" 2>/dev/null; do
sleep 0.5
done
# Move the new AppImage over the old one
mv -f "$NEW_APPIMAGE" "$OLD_APPIMAGE"
chmod +x "$OLD_APPIMAGE"
# Launch the new AppImage detached
"$OLD_APPIMAGE" > /dev/null 2>&1 &
disown
""";
await File.WriteAllTextAsync(updateScriptPath, scriptContent);
File.SetUnixFileMode(
updateScriptPath,
UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute
);
System.Diagnostics.Process.Start(
new System.Diagnostics.ProcessStartInfo
{
FileName = "/usr/bin/env",
Arguments = $"bash \"{updateScriptPath}\"",
UseShellExecute = false,
CreateNoWindow = true,
}
);
App.Shutdown();
return;
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to execute AppImage update script");
var dialog = DialogHelper.CreateMarkdownDialog(
"AppImage update script failed. \nCould not replace old AppImage with new version. Please check directory permissions. \nFalling back to standard update process. User intervention required: After program closes, \nplease move the new AppImage extracted in the '.StabilityMatrixUpdate' hidden directory to the old AppImage overwriting it. \n\nClose this dialog to continue with standard update process.",
Resources.Label_UnexpectedErrorOccurred
);
await dialog.ShowAsync();
}
}
// Set current version for update messages
settingsManager.Transaction(
s => s.UpdatingFromVersion = Compat.AppVersion,
ignoreMissingLibraryDir: true
);
UpdateText = "Getting a few things ready...";
await using (new MinimumDelay(500, 1000))
{
await Task.Run(() =>
{
var args = new[] { "--wait-for-exit-pid", $"{Environment.ProcessId}" };
if (Program.Args.NoSentry)
{
args = args.Append("--no-sentry").ToArray();
}
ProcessRunner.StartApp(UpdateHelper.ExecutablePath.FullPath, args);
});
}
UpdateText = "Update complete. Restarting Stability Matrix in 3 seconds...";
await Task.Delay(1000);
UpdateText = "Update complete. Restarting Stability Matrix in 2 seconds...";
await Task.Delay(1000);
UpdateText = "Update complete. Restarting Stability Matrix in 1 second...";
await Task.Delay(1000);
UpdateText = "Update complete. Restarting Stability Matrix...";
App.Shutdown();
}
internal async Task<string> GetReleaseNotes(string changelogUrl)
{
using var client = httpClientFactory.CreateClient();
try
{
var response = await client.GetAsync(changelogUrl);
if (response.IsSuccessStatusCode)
{
var changelog = await response.Content.ReadAsStringAsync();
// Formatting for new changelog format
// https://keepachangelog.com/en/1.1.0/
if (changelogUrl.EndsWith(".md", StringComparison.OrdinalIgnoreCase))
{
return FormatChangelog(
changelog,
Compat.AppVersion,
settingsManager.Settings.PreferredUpdateChannel
) ?? "## Unable to format release notes";
}
return changelog;
}
return "## Unable to load release notes";
}
catch (HttpRequestException e)
{
return $"## Unable to fetch release notes ({e.StatusCode})\n\n[{changelogUrl}]({changelogUrl})";
}
catch (TaskCanceledException) { }
return $"## Unable to fetch release notes\n\n[{changelogUrl}]({changelogUrl})";
}
/// <summary>
/// Formats changelog markdown including up to the current version
/// </summary>
/// <param name="markdown">Markdown to format</param>
/// <param name="currentVersion">Versions equal or below this are excluded</param>
/// <param name="maxChannel">Maximum channel level to include</param>
internal static string? FormatChangelog(
string markdown,
SemVersion currentVersion,
UpdateChannel maxChannel = UpdateChannel.Stable
)
{
var pattern = RegexChangelog();
var results = pattern
.Matches(markdown)
.Select(m => new
{
Block = m.Groups[1].Value.Trim(),
Version = SemVersion.TryParse(
m.Groups[2].Value.Trim(),
SemVersionStyles.AllowV,
out var version
)
? version
: null,
Content = m.Groups[3].Value.Trim(),
})
.Where(x => x.Version is not null)
.ToList();
// Join all blocks until and excluding the current version
// If we're on a pre-release, include the current release
var currentVersionBlock = results.FindIndex(x => x.Version == currentVersion.WithoutMetadata());
// For mismatching build metadata, add one
if (
currentVersionBlock != -1
&& results[currentVersionBlock].Version?.Metadata != currentVersion.Metadata
)
{
currentVersionBlock++;
}
// Support for previous pre-release without changelogs
if (currentVersionBlock == -1)
{
currentVersionBlock = results.FindIndex(x =>
x.Version == currentVersion.WithoutPrereleaseOrMetadata()
);
// Add 1 if found to include the current release
if (currentVersionBlock != -1)
{
currentVersionBlock++;
}
}
// Still not found, just include all
if (currentVersionBlock == -1)
{
currentVersionBlock = results.Count;
}
// Filter out pre-releases
var blocks = results
.Take(currentVersionBlock)
.Where(x =>
x.Version!.PrereleaseIdentifiers.Count == 0
|| x.Version.PrereleaseIdentifiers[0].Value switch
{
"pre" when maxChannel >= UpdateChannel.Preview => true,
"dev" when maxChannel >= UpdateChannel.Development => true,
_ => false,
}
)
.Select(x => x.Block);
return string.Join(Environment.NewLine + Environment.NewLine, blocks);
}
}