$ErrorActionPreference = 'Stop' $root = [IO.Path]::GetFullPath('D:\web\dj') $live = [IO.Path]::GetFullPath((Join-Path $root 'HuobaoDrama')) $stage = [IO.Path]::GetFullPath((Join-Path $root 'integration-work\stage-4.0.11\app')) $previous = [IO.Path]::GetFullPath((Join-Path $root ('integration-work\previous-app-' + (Get-Date -Format 'yyyyMMdd-HHmmss')))) foreach ($target in @($live,$stage,$previous)) { if (-not $target.StartsWith($root + '\', [StringComparison]::OrdinalIgnoreCase)) { throw "Target outside workspace: $target" } } if (-not (Test-Path -LiteralPath (Join-Path $stage 'resources\huobao-local-build.json'))) { throw 'Staged build manifest missing' } $manifest = Get-Content -LiteralPath (Join-Path $stage 'resources\huobao-local-build.json') -Raw | ConvertFrom-Json if ($manifest.upstream_version -ne '4.0.11') { throw 'Unexpected staged version' } $hash = (Get-FileHash -LiteralPath (Join-Path $stage 'resources\app.asar') -Algorithm SHA256).Hash.ToLowerInvariant() if ($hash -ne $manifest.archive_sha256) { throw 'Staged archive hash mismatch' } $processes = @(Get-Process -Name HuobaoDrama -ErrorAction SilentlyContinue | Where-Object { $_.Path -eq (Join-Path $live 'HuobaoDrama.exe') }) foreach ($process in $processes) { if ($process.MainWindowHandle -ne 0) { $null = $process.CloseMainWindow() } } $deadline = (Get-Date).AddSeconds(12) do { $remaining = @(Get-Process -Name HuobaoDrama -ErrorAction SilentlyContinue | Where-Object { $_.Path -eq (Join-Path $live 'HuobaoDrama.exe') }) if (-not $remaining.Count) { break } Start-Sleep -Milliseconds 300 } while ((Get-Date) -lt $deadline) if ($remaining.Count) { throw 'Application has not closed cleanly. No files replaced.' } $python = 'C:\Users\pc\AppData\Local\Programs\Python\Python311\python.exe' & $python (Join-Path $root 'model-bridge\audit_data.py') 'before-switch-final.json' if ($LASTEXITCODE -ne 0) { throw 'Pre-switch data check failed' } & $python (Join-Path $root 'model-bridge\backup_upgrade.py') if ($LASTEXITCODE -ne 0) { throw 'Backup failed; original app retained' } Move-Item -LiteralPath $live -Destination $previous try { Copy-Item -LiteralPath $stage -Destination $live -Recurse } catch { # Leave any partial new copy for inspection and put the known working app back. $failed = Join-Path $root ('integration-work\failed-copy-' + (Get-Date -Format 'yyyyMMdd-HHmmss')) if (Test-Path -LiteralPath $live) { Move-Item -LiteralPath $live -Destination $failed } Move-Item -LiteralPath $previous -Destination $live throw } Write-Output "Previous program retained at $previous" & $python (Join-Path $root 'model-bridge\start_huobao.py') if ($LASTEXITCODE -ne 0) { throw 'New application failed health check; backups retained' } & $python (Join-Path $root 'model-bridge\audit_data.py') 'after-switch.json'