3 Commits

Author SHA1 Message Date
qhy040404
98ae857c60 revert to registry 2023-12-28 20:40:37 +08:00
qhy040404
738fe060bc code style 2023-12-28 20:34:23 +08:00
qhy040404
d3f8500ebe webview2 2023-12-28 20:06:36 +08:00
4 changed files with 103 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace Snap.Hutao.Deployment;
internal static partial class EdgeWebView2Dependency
{
private const string EdgeWebView2DownloadUrl = "https://go.microsoft.com/fwlink/p/?LinkId=2124703";
private const string EdgeWebView2PerUserPath = @"HKEY_CURRENT_USER\Software\Microsoft\EdgeUpdate\Clients";
private const string EdgeWebView2PerMachinePath = @"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients";
private const string EdgeWebView2GuidKey = "{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}";
public static async Task EnsureAsync(bool installWebView2, bool isUpdateMode)
{
if (!installWebView2 || isUpdateMode)
{
return;
}
if (Registry.GetValue(EdgeWebView2PerUserPath, EdgeWebView2GuidKey, null) is not null || Registry.GetValue(EdgeWebView2PerMachinePath, EdgeWebView2GuidKey, null) is not null)
{
Console.WriteLine("WebView2 already installed.");
return;
}
Console.WriteLine("WebView2 not found, start downloading and installing WebView2...");
await DownloadWebView2InstallerAndInstallAsync().ConfigureAwait(false);
}
private static async Task DownloadWebView2InstallerAndInstallAsync()
{
string webView2InstallerPath = Path.Combine(Path.GetTempPath(), "MicrosoftEdgeWebview2Setup.exe");
try
{
using (HttpClient httpClient = new())
{
HttpShardCopyWorkerOptions<DownloadStatus> options = new()
{
HttpClient = httpClient,
SourceUrl = EdgeWebView2DownloadUrl,
DestinationFilePath = webView2InstallerPath,
StatusFactory = (bytesRead, totalBytes) => new DownloadStatus(bytesRead, totalBytes),
};
using (HttpShardCopyWorker<DownloadStatus> worker = await HttpShardCopyWorker<DownloadStatus>.CreateAsync(options).ConfigureAwait(false))
{
Progress<DownloadStatus> progress = new(ConsoleWriteProgress);
await worker.CopyAsync(progress).ConfigureAwait(false);
}
}
Console.WriteLine("Start installing WebView2...");
Process installerProcess = new()
{
StartInfo = new()
{
Arguments = "/silent /install",
FileName = webView2InstallerPath,
RedirectStandardOutput = true,
RedirectStandardError = true,
},
};
using (installerProcess)
{
installerProcess.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
installerProcess.ErrorDataReceived += (sender, e) => Console.WriteLine(e.Data);
installerProcess.Start();
Console.WriteLine("-----> WebView2 Output begin -----");
installerProcess.BeginOutputReadLine();
installerProcess.BeginErrorReadLine();
await installerProcess.WaitForExitAsync().ConfigureAwait(false);
Console.WriteLine("<----- WebView2 Output end -------");
}
}
finally
{
if (File.Exists(webView2InstallerPath))
{
File.Delete(webView2InstallerPath);
}
}
static void ConsoleWriteProgress(DownloadStatus status)
{
Console.Write($"\r{status.ProgressDescription}");
}
}
}

View File

@@ -15,6 +15,7 @@ internal static partial class Invocation
string? path = context.ParseResult.GetValueForOption(InvocationOptions.PackagePath);
string? name = context.ParseResult.GetValueForOption(InvocationOptions.FamilyName);
bool isUpdateMode = context.ParseResult.GetValueForOption(InvocationOptions.UpdateBehavior);
bool installWebView2 = context.ParseResult.GetValueForOption(InvocationOptions.InstallWebView2);
if (!isUpdateMode)
{
@@ -47,6 +48,7 @@ internal static partial class Invocation
await Certificate.EnsureGlobalSignCodeSigningRootR45Async().ConfigureAwait(false);
await WindowsAppSDKDependency.EnsureAsync(path).ConfigureAwait(false);
await EdgeWebView2Dependency.EnsureAsync(installWebView2, isUpdateMode).ConfigureAwait(false);
await RunDeploymentCoreAsync(path, name, isUpdateMode).ConfigureAwait(false);
await ExitAsync(isUpdateMode).ConfigureAwait(false);
}

View File

@@ -19,4 +19,9 @@ internal static class InvocationOptions
"--update-behavior",
() => false,
"Change behavior of the tool into update mode");
public static readonly Option<bool> InstallWebView2 = new(
"--install-webview2",
() => false,
"Install WebView2 if not found.");
}

View File

@@ -16,6 +16,7 @@ internal static partial class Program
root.AddOption(InvocationOptions.PackagePath);
root.AddOption(InvocationOptions.FamilyName);
root.AddOption(InvocationOptions.UpdateBehavior);
root.AddOption(InvocationOptions.InstallWebView2);
root.SetHandler(Invocation.RunDeploymentAsync);