Repository navigation
feat: install ControlFS from the session menu when it is missing #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4cf7bd1
feat: install ControlFS from the session menu when it is missing
lippdev ce5b895
docs: link ControlFS install to PR 136
lippdev 0efd3f0
chore: prepare 1.6.0-beta.5
lippdev a480291
fix: stop the ControlFS installer when it times out
lippdev 4d29176
fix: never start a ControlFS install while an earlier setup is still …
lippdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| using System.Diagnostics; | ||
|
|
||
| namespace ConsoleMode.Services; | ||
|
|
||
| /// <summary> | ||
| /// Installs ControlFS from the session menu, when the user asks for it there: downloads the setup of its newest | ||
| /// release, checks it against GitHub's SHA-256 and runs it silently. The setup is per user (no admin prompt) and | ||
| /// does not start ControlFS when silent. Updating it afterwards is ControlFS's own business. | ||
| /// </summary> | ||
| public static class ControlFsInstaller | ||
| { | ||
| private static readonly TimeSpan SetupTimeout = TimeSpan.FromMinutes(5); | ||
|
|
||
| /// <summary> | ||
| /// Returns the installed exe. Throws with the reason when it can't be installed. <paramref name="progress"/> gets | ||
| /// the download from 0 to 1, then <see cref="double.PositiveInfinity"/> once the setup is running. | ||
| /// </summary> | ||
| public static async Task<string> InstallAsync(IProgress<double>? progress, CancellationToken ct = default) | ||
| { | ||
| if (!Environment.Is64BitOperatingSystem) throw new PlatformNotSupportedException("ControlFS só tem instalador x64."); | ||
| // A setup from an earlier attempt (or its Inno child, "ControlFS-Setup-x64.tmp") may still be at work: | ||
| // never start a second one next to it. | ||
| if (IsSetupRunning()) throw new InvalidOperationException("outro instalador do ControlFS ainda está rodando"); | ||
|
|
||
| var setup = await UpdateService.FindControlFsSetupAsync(ct) | ||
| ?? throw new InvalidOperationException("nenhuma release com instalador verificável"); | ||
| var dir = Path.Combine(Path.GetTempPath(), "ConsoleModeUpdate"); | ||
| Directory.CreateDirectory(dir); | ||
| var file = Path.Combine(dir, setup.Name); | ||
| try | ||
| { | ||
| await UpdateService.DownloadAsync(setup.Url, file, progress, ct); | ||
| if (!await UpdateIntegrity.VerifyFileAsync(file, setup.Digest, ct)) | ||
| throw new InvalidDataException("o SHA-256 do instalador não confere"); | ||
| AppLog.Write($"ControlFS: instalador {setup.Version} baixado e verificado"); | ||
|
|
||
| progress?.Report(double.PositiveInfinity); | ||
| using var process = Process.Start(new ProcessStartInfo | ||
| { | ||
| FileName = file, | ||
| Arguments = "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART", | ||
| UseShellExecute = false | ||
| }) ?? throw new InvalidOperationException("o instalador não iniciou"); | ||
| using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct); | ||
| timeout.CancelAfter(SetupTimeout); | ||
| try { await process.WaitForExitAsync(timeout.Token); } | ||
| catch (OperationCanceledException) | ||
| { | ||
| // Given up on: stop it (Inno runs a second, child process), or it could finish later and a new | ||
| // attempt from the menu would start a second installer next to it. | ||
| try { process.Kill(entireProcessTree: true); } catch { /* already gone */ } | ||
| await process.WaitForExitAsync(CancellationToken.None); | ||
| throw new TimeoutException($"o instalador não terminou em {SetupTimeout.TotalMinutes:0} min e foi encerrado"); | ||
| } | ||
| if (process.ExitCode != 0) throw new InvalidOperationException($"o instalador saiu com o código {process.ExitCode}"); | ||
| } | ||
| finally | ||
| { | ||
| try { File.Delete(file); } catch { /* the temp folder is cleaned by Windows */ } | ||
| } | ||
|
|
||
| return ControlFsService.FindExe() ?? throw new InvalidOperationException("instalado, mas o ControlFS.exe não foi encontrado"); | ||
| } | ||
|
|
||
| private static bool IsSetupRunning() | ||
| { | ||
| var running = false; | ||
| foreach (var process in Process.GetProcesses()) | ||
| { | ||
| using (process) | ||
| { | ||
| try { running |= process.ProcessName.StartsWith("ControlFS-Setup", StringComparison.OrdinalIgnoreCase); } | ||
| catch { /* exited while listing */ } | ||
| } | ||
| } | ||
| return running; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using System.Text.Json; | ||
|
|
||
| namespace ConsoleMode.Services; | ||
|
|
||
| /// <summary>The ControlFS installer of one release: what to download and the SHA-256 it must have.</summary> | ||
| public sealed record ControlFsSetup(string Version, string Url, string Name, string Digest, long Size); | ||
|
|
||
| /// <summary> | ||
| /// Picks the ControlFS installer out of GitHub's list of its releases. Pure, so the rules are tested: only a | ||
| /// published release, only the x64 setup, only with GitHub's SHA-256 digest and only from that repository's | ||
| /// own downloads. Anything else is not installed from the menu (the download page opens instead). | ||
| /// </summary> | ||
| public static class ControlFsRelease | ||
| { | ||
| public const string Repository = "nextestudios/ControlFS"; | ||
| public const string SetupAssetName = "ControlFS-Setup-x64.exe"; | ||
| public const string DownloadPrefix = "https://github.com/" + Repository + "/releases/download/"; | ||
|
|
||
| /// <summary>The setup of the newest published release (GitHub lists them newest first), or null.</summary> | ||
| public static ControlFsSetup? Pick(JsonElement releases) | ||
| { | ||
| if (releases.ValueKind != JsonValueKind.Array) return null; | ||
| foreach (var release in releases.EnumerateArray()) | ||
| { | ||
| if (release.ValueKind != JsonValueKind.Object) continue; | ||
| if (release.TryGetProperty("draft", out var draft) && draft.ValueKind == JsonValueKind.True) continue; | ||
| if (!release.TryGetProperty("assets", out var assets) || assets.ValueKind != JsonValueKind.Array) continue; | ||
| foreach (var asset in assets.EnumerateArray()) | ||
| { | ||
| if (!string.Equals(Text(asset, "name"), SetupAssetName, StringComparison.OrdinalIgnoreCase)) continue; | ||
| var url = Text(asset, "browser_download_url"); | ||
| var digest = Text(asset, "digest"); | ||
| // The newest release decides: without a digest or from somewhere else, nothing is run. | ||
| if (!IsTrustedUrl(url) || !UpdateIntegrity.IsValidSha256Digest(digest)) return null; | ||
| var size = asset.TryGetProperty("size", out var s) && s.TryGetInt64(out var bytes) ? bytes : 0; | ||
| return new ControlFsSetup(Text(release, "tag_name").TrimStart('v', 'V'), url, SetupAssetName, digest, size); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public static bool IsTrustedUrl(string? url) => | ||
| url is not null && url.StartsWith(DownloadPrefix, StringComparison.Ordinal); | ||
|
|
||
| private static string Text(JsonElement element, string name) => | ||
| element.ValueKind == JsonValueKind.Object && element.TryGetProperty(name, out var value) && value.ValueKind == JsonValueKind.String | ||
| ? value.GetString() ?? "" : ""; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.