Skip to content

Commit a42fba4

Browse files
Update to offer downloading windows updates
1 parent 55b34fe commit a42fba4

File tree

4 files changed

+89
-10
lines changed

4 files changed

+89
-10
lines changed

MSUScripter/MSUScripter.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
99
<ApplicationIcon>MSUScripterIcon.ico</ApplicationIcon>
1010
<PackageIcon>MSUScripterIcon.ico</PackageIcon>
11-
<Version>5.0.0</Version>
11+
<Version>5.0.1</Version>
1212
<RuntimeFrameworkVersion>9.0.0</RuntimeFrameworkVersion>
1313
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
1414
<LangVersion>12</LangVersion>

MSUScripter/Services/ControlServices/MainWindowService.cs

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Linq;
5+
using System.Net.Http;
46
using System.Runtime.Versioning;
57
using System.Threading.Tasks;
68
using AvaloniaControls.ControlServices;
@@ -254,12 +256,10 @@ private bool CleanDirectory(string path, TimeSpan? timeout = null)
254256

255257
if (newerGitHubRelease != null)
256258
{
257-
if (OperatingSystem.IsLinux())
258-
{
259-
return (newerGitHubRelease.Url,
260-
newerGitHubRelease.Asset.FirstOrDefault(x => x.Url.ToLower().EndsWith(".appimage"))?.Url);
261-
}
262-
return (newerGitHubRelease.Url, null);
259+
var downloadUrl = OperatingSystem.IsLinux()
260+
? newerGitHubRelease.Asset.FirstOrDefault(x => x.Url.ToLower().EndsWith(".appimage"))?.Url
261+
: newerGitHubRelease.Asset.FirstOrDefault(x => x.Url.ToLower().EndsWith(".exe"))?.Url;
262+
return (newerGitHubRelease.Url, downloadUrl);
263263
}
264264

265265
return null;
@@ -296,6 +296,72 @@ public void IgnoreFutureUpdates()
296296
settingsService.SaveSettings();
297297
}
298298

299+
public async Task<string?> InstallWindowsUpdate(string url)
300+
{
301+
var filename = Path.GetFileName(new Uri(url).AbsolutePath);
302+
var localPath = Path.Combine(Path.GetTempPath(), filename);
303+
304+
logger.LogInformation("Downloading {Url} to {LocalPath}", url, localPath);
305+
306+
var response = await DownloadFileAsyncAttempt(url, localPath);
307+
308+
if (!response.Item1)
309+
{
310+
logger.LogInformation("Download failed: {Error}", response.Item2);
311+
return response.Item2;
312+
}
313+
314+
try
315+
{
316+
logger.LogInformation("Launching setup file");
317+
318+
var psi = new ProcessStartInfo
319+
{
320+
FileName = localPath,
321+
UseShellExecute = true,
322+
RedirectStandardOutput = false,
323+
RedirectStandardError = false,
324+
RedirectStandardInput = false,
325+
CreateNoWindow = true
326+
};
327+
328+
Process.Start(psi);
329+
return null;
330+
}
331+
catch (Exception e)
332+
{
333+
logger.LogError(e, "Failed to start setup file");
334+
return "Failed to start setup file";
335+
}
336+
}
337+
338+
private async Task<(bool, string?)> DownloadFileAsyncAttempt(string url, string target, int attemptNumber = 0, int totalAttempts = 3)
339+
{
340+
341+
using var httpClient = new HttpClient();
342+
343+
try
344+
{
345+
await using var downloadStream = await httpClient.GetStreamAsync(url);
346+
await using var fileStream = new FileStream(target, FileMode.Create);
347+
await downloadStream.CopyToAsync(fileStream);
348+
return (true, null);
349+
}
350+
catch (Exception ex)
351+
{
352+
logger.LogError(ex, "Download failed");
353+
if (attemptNumber < totalAttempts)
354+
{
355+
await Task.Delay(TimeSpan.FromSeconds(attemptNumber));
356+
return await DownloadFileAsyncAttempt(url, target, attemptNumber + 1, totalAttempts);
357+
}
358+
else
359+
{
360+
return (false, $"Download failed: {ex.Message}");
361+
}
362+
}
363+
}
364+
299365
private async Task CleanUpFolders()
300366
{
301367
await ITaskService.Run(() =>

MSUScripter/Services/PythonCompanionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace MSUScripter.Services;
1515
public class PythonCompanionService(ILogger<PythonCompanionService> logger, YamlService yamlService, DependencyInstallerService dependencyInstallerService)
1616
{
1717
private const string BaseCommand = "py_msu_scripter_app";
18-
private const string MinVersion = "v0.1.6";
18+
private const string MinVersion = "v0.1.9";
1919
private RunMethod _runMethod = RunMethod.Unknown;
2020
private string? _pythonExecutablePath;
2121
private string? _ffmpegPath;

MSUScripter/Views/MainWindow.axaml.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ private async Task ShowDesktopFileWindow()
126126

127127
private async Task ShowNewReleaseWindow(string releaseUrl, string? downloadUrl)
128128
{
129+
if (_service == null)
130+
{
131+
return;
132+
}
133+
129134
downloadUrl ??= "";
130135
var hasDownloadUrl = !string.IsNullOrEmpty(downloadUrl);
131136

@@ -151,7 +156,7 @@ private async Task ShowNewReleaseWindow(string releaseUrl, string? downloadUrl)
151156

152157
if (messageWindow.DialogResult.CheckedBox)
153158
{
154-
_service?.IgnoreFutureUpdates();
159+
_service.IgnoreFutureUpdates();
155160
}
156161

157162
if (messageWindow.DialogResult.PressedAcceptButton)
@@ -178,7 +183,15 @@ private async Task ShowNewReleaseWindow(string releaseUrl, string? downloadUrl)
178183
}
179184
else
180185
{
181-
throw new InvalidOperationException("Not supported on Windows");
186+
var result = await _service.InstallWindowsUpdate(downloadUrl);
187+
if (!string.IsNullOrEmpty(result))
188+
{
189+
await MessageWindow.ShowErrorDialog(result);
190+
}
191+
else
192+
{
193+
Close();;
194+
}
182195
}
183196
}
184197
}

0 commit comments

Comments
 (0)