fix: keep the portable launcher source and script parameter list valid #27

Merged
cflmy merged 1 commits from codex/portable-fix into main 2026-12-03 09:52:20 +08:00
2 changed files with 46 additions and 44 deletions

No files matched your search

+29 -27
View File
@@ -6,14 +6,14 @@ using System.Threading;
using System.Windows.Forms;
/// <summary>
/// MQDH desktop launcher: installs the bundled runtime on first start, serves
/// the Marqdo workbench on loopback, then opens it in an app window. Runs
/// without a console window; <c>--stop</c> terminates the background server.
/// MQDH desktop launcher: installs the bundled offline runtime on first start,
/// serves the inherited DSH Web UI on loopback, and opens it in an app window.
/// Runs without a console window; <c>--stop</c> terminates the background server.
/// </summary>
internal static class MqdhLauncher
{
private const string Title = "MQDH";
private const int WaitForServerMs = 90000;
private const int WaitForServerMs = 120000;
private static readonly Regex WebUrl = new Regex(@"http://127\.0\.0\.1:(\d+)/\?token=([A-Za-z0-9_\-]+)", RegexOptions.Compiled);
private static string openedUrl = null;
@@ -57,12 +57,13 @@ internal static class MqdhLauncher
Directory.CreateDirectory(HomeDir);
Log(DateTime.Now.ToString("o") + " launcher failed: " + error);
MessageBox.Show(
"MQDH 鍚姩澶辫触锛? + error.Message + Environment.NewLine + Environment.NewLine + "鏃ュ織锛? + LogFile,
"MQDH failed to start." + Environment.NewLine + Environment.NewLine + error.Message +
Environment.NewLine + Environment.NewLine + "Log: " + LogFile,
Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch
{
// Nothing else can be surfaced without a window.
// No window can be shown if even the log cannot be written.
}
}
}
@@ -96,8 +97,8 @@ internal static class MqdhLauncher
string node = Path.Combine(Root, "runtime", "node.exe");
string script = Path.Combine(Root, "scripts", "mqdh.mjs");
if (!File.Exists(node)) throw new FileNotFoundException("鍐呯疆 Node 杩愯鏃剁己澶?, node);
if (!File.Exists(script)) throw new FileNotFoundException("MQDH 鍚姩鑴氭湰缂哄け", script);
if (!File.Exists(node)) throw new FileNotFoundException("bundled Node runtime is missing", node);
if (!File.Exists(script)) throw new FileNotFoundException("MQDH launcher script is missing", script);
string runtimeDeps = Path.Combine(Root, "runtime-deps");
if (Directory.Exists(runtimeDeps) && string.IsNullOrEmpty(Environment.GetEnvironmentVariable("MQDH_BOOTSTRAP_OFFLINE")))
@@ -111,7 +112,11 @@ internal static class MqdhLauncher
}
Bootstrap(node, script);
StartServer(node, script);
}
private static void StartServer(string node, string script)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = node;
start.Arguments = "\"" + script + "\" --no-open --port 0";
@@ -123,7 +128,7 @@ internal static class MqdhLauncher
start.RedirectStandardError = true;
Process child = Process.Start(start);
if (child == null) throw new InvalidOperationException("鏃犳硶鍚姩鍐呯疆杩愯鏃?);
if (child == null) throw new InvalidOperationException("could not start the bundled runtime");
File.WriteAllText(PidFile, child.Id.ToString());
child.OutputDataReceived += delegate(object sender, DataReceivedEventArgs data)
@@ -138,13 +143,10 @@ internal static class MqdhLauncher
DateTime deadline = DateTime.UtcNow.AddMilliseconds(WaitForServerMs);
while (openedUrl == null && DateTime.UtcNow < deadline)
{
if (child.HasExited) throw new InvalidOperationException("MQDH 鏈嶅姟鎻愬墠閫€鍑猴紝閫€鍑虹爜 " + child.ExitCode + "銆傝鏌ョ湅鏃ュ織锛? + LogFile);
if (child.HasExited) throw new InvalidOperationException("MQDH exited before the workbench was ready (exit code " + child.ExitCode + "). Log: " + LogFile);
Thread.Sleep(150);
}
if (openedUrl == null)
{
throw new TimeoutException("绛夊緟 MQDH 宸ヤ綔鍙板氨缁秴鏃躲€傝鏌ョ湅鏃ュ織锛? + LogFile);
}
if (openedUrl == null) throw new TimeoutException("timed out waiting for the MQDH workbench. Log: " + LogFile);
}
private static void TryCaptured(string line)
@@ -152,10 +154,9 @@ internal static class MqdhLauncher
if (openedUrl != null || line == null) return;
Match match = WebUrl.Match(line);
if (!match.Success) return;
string url = match.Value;
openedUrl = url;
try { File.WriteAllText(UrlFile, url); } catch { }
OpenWorkbench(url);
openedUrl = match.Value;
try { File.WriteAllText(UrlFile, openedUrl); } catch { }
OpenWorkbench(openedUrl);
}
private static string ReadSavedUrl()
@@ -170,7 +171,7 @@ internal static class MqdhLauncher
}
catch
{
// A missing or stale URL falls through to a restart.
// A missing or stale URL falls through to a server restart.
}
return null;
}
@@ -179,7 +180,7 @@ internal static class MqdhLauncher
{
if (string.IsNullOrEmpty(url))
{
throw new InvalidOperationException("MQDH 宸ヤ綔鍙板湴鍧€涓嶅彲鐢紝璇疯繍琛?MQDH.exe --stop 鍚庨噸璇曘€?);
throw new InvalidOperationException("the MQDH workbench URL is unavailable; run MQDH.exe --stop and retry");
}
string edge = FindEdge();
if (edge != null)
@@ -197,11 +198,12 @@ internal static class MqdhLauncher
private static string FindEdge()
{
foreach (string baseDir in new string[]
string[] bases = new string[]
{
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
})
};
foreach (string baseDir in bases)
{
string candidate = Path.Combine(baseDir, "Microsoft", "Edge", "Application", "msedge.exe");
if (File.Exists(candidate)) return candidate;
@@ -222,20 +224,20 @@ internal static class MqdhLauncher
start.RedirectStandardError = true;
Process child = Process.Start(start);
if (child == null) throw new InvalidOperationException("鏃犳硶鍒濆鍖?MQDH 杩愯鏃?);
if (child == null) throw new InvalidOperationException("could not start the MQDH runtime installer");
string output = child.StandardOutput.ReadToEnd();
string error = child.StandardError.ReadToEnd();
if (!child.WaitForExit(300000))
{
try { child.Kill(); } catch { }
throw new TimeoutException("MQDH 杩愯鏃跺垵濮嬪寲瓒呮椂");
throw new TimeoutException("timed out installing the MQDH runtime");
}
Log(DateTime.Now.ToString("o") + " bootstrap exit=" + child.ExitCode);
if (!string.IsNullOrEmpty(output)) Log(output);
if (!string.IsNullOrEmpty(error)) Log(error);
if (child.ExitCode != 0)
{
throw new InvalidOperationException("MQDH 杩愯鏃跺垵濮嬪寲澶辫触锛岄€€鍑虹爜 " + child.ExitCode + "銆傝鏌ョ湅鏃ュ織锛? + LogFile);
throw new InvalidOperationException("the MQDH runtime installer failed with exit code " + child.ExitCode + ". Log: " + LogFile);
}
}
@@ -250,7 +252,7 @@ internal static class MqdhLauncher
int pid;
if (!Running(out pid))
{
MessageBox.Show("MQDH 褰撳墠娌℃湁杩愯銆?, Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("MQDH is not running.", Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
@@ -267,6 +269,6 @@ internal static class MqdhLauncher
try { File.Delete(PidFile); } catch { }
try { File.Delete(UrlFile); } catch { }
}
MessageBox.Show("MQDH 宸查€€鍑恒€?, Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("MQDH stopped.", Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
+17 -17
View File
@@ -12,23 +12,12 @@ param(
[string]$AppArchive,
[string]$OutDir = (Join-Path $PSScriptRoot '..\.ci'),
[string]$SourceSha = '',
[string]$LauncherExe = '',
[string]$ZipTool = ''
[string]$LauncherExe = ''
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
function Find-7z {
param([string]$Configured)
if ($Configured -ne '' -and (Test-Path -LiteralPath $Configured)) { return (Resolve-Path -LiteralPath $Configured).Path }
$command = Get-Command '7z.exe' -ErrorAction SilentlyContinue
if ($command) { return $command.Source }
foreach ($candidate in @('C:\Program Files\7-Zip\7z.exe', 'C:\Program Files (x86)\7-Zip\7z.exe')) {
if (Test-Path -LiteralPath $candidate) { return $candidate }
}
throw '7z.exe not found; install 7-Zip or pass -ZipTool'
}
function Read-JsonFile {
param([string]$Path)
@@ -79,10 +68,22 @@ try {
$zipName = "$folder.zip"
$zip = Join-Path $OutDir $zipName
Remove-Item -LiteralPath $zip -Force -ErrorAction SilentlyContinue
$sevenZip = Find-7z $ZipTool
Write-Host "creating $zipName"
& $sevenZip a -tzip -mx=5 $zip $folder
if ($LASTEXITCODE -ne 0) { throw "zip creation failed: $LASTEXITCODE" }
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zipStream = [IO.File]::Create($zip)
try {
$archive = New-Object System.IO.Compression.ZipArchive($zipStream, [System.IO.Compression.ZipArchiveMode]::Create, $false)
try {
foreach ($file in Get-ChildItem -LiteralPath $staged -Recurse -File -Force) {
$relative = $file.FullName.Substring($staged.Length + 1).Replace('\', '/')
$entry = $archive.CreateEntry("$folder/$relative", [System.IO.Compression.CompressionLevel]::Fastest)
$entryStream = $entry.Open()
$input = [IO.File]::OpenRead($file.FullName)
try { $input.CopyTo($entryStream) } finally { $input.Dispose(); $entryStream.Dispose() }
}
} finally { $archive.Dispose() }
} finally { $zipStream.Dispose() }
$zipHash = (Get-FileHash -LiteralPath $zip -Algorithm SHA256).Hash.ToLower()
$sidecar = "$zip.sha256"
[IO.File]::WriteAllText($sidecar, "$zipHash $zipName`n", (New-Object Text.UTF8Encoding($false)))
@@ -97,8 +98,7 @@ try {
# workbench answers. This exercises the actual portable user path.
$verify = Join-Path $work 'verify'
New-Item -ItemType Directory -Force -Path $verify | Out-Null
& $sevenZip x $zip -o"$verify" -y | Out-Null
if ($LASTEXITCODE -ne 0) { throw "zip extraction failed: $LASTEXITCODE" }
[System.IO.Compression.ZipFile]::ExtractToDirectory($zip, $verify)
$verifyTree = Join-Path $verify $folder
$home = Join-Path $verify 'home'
New-Item -ItemType Directory -Force -Path $home | Out-Null