param( [string]$Version = "", [string]$DistDir = "", [string]$Installer = "", [string]$WorkRoot = "", [int]$WebPort = 18080, [int]$WebStartupTimeoutSeconds = 45, [int]$DashboardStartupSeconds = 8 ) $ErrorActionPreference = "Stop" function Resolve-FullPath([string]$PathText) { if ([System.IO.Path]::IsPathRooted($PathText)) { return [System.IO.Path]::GetFullPath($PathText) } return [System.IO.Path]::GetFullPath((Join-Path $Root $PathText)) } function Assert-UnderDirectory([string]$Child, [string]$Parent, [string]$Label) { $childFull = [System.IO.Path]::GetFullPath($Child).TrimEnd('\', '/') $parentFull = [System.IO.Path]::GetFullPath($Parent).TrimEnd('\', '/') if (-not $childFull.StartsWith($parentFull, [System.StringComparison]::OrdinalIgnoreCase)) { throw "$Label must be under $parentFull, got $childFull" } } function Assert-FileExists([string]$PathText, [string]$Label) { if (-not (Test-Path -LiteralPath $PathText -PathType Leaf)) { throw "Missing $Label`: $PathText" } } function Assert-DirExists([string]$PathText, [string]$Label) { if (-not (Test-Path -LiteralPath $PathText -PathType Container)) { throw "Missing $Label`: $PathText" } } function ConvertTo-ProcessArgument([string]$Arg) { if ($Arg -notmatch '[\s"]') { return $Arg } return '"' + ($Arg -replace '\\(?=\\*")', '$0$0' -replace '"', '\"') + '"' } function Invoke-CheckedProcess([string]$FilePath, [string[]]$Arguments, [string]$Label, [string]$WorkingDirectory = "") { $psi = New-Object System.Diagnostics.ProcessStartInfo $psi.FileName = $FilePath $psi.Arguments = (($Arguments | ForEach-Object { ConvertTo-ProcessArgument $_ }) -join " ") if ($WorkingDirectory) { $psi.WorkingDirectory = $WorkingDirectory } $psi.UseShellExecute = $false $psi.RedirectStandardOutput = $true $psi.RedirectStandardError = $true $proc = [System.Diagnostics.Process]::Start($psi) $stdout = $proc.StandardOutput.ReadToEnd() $stderr = $proc.StandardError.ReadToEnd() $proc.WaitForExit() if ($proc.ExitCode -ne 0) { $detail = (($stderr + " " + $stdout) -replace "\s+", " ").Trim() throw "$Label failed with exit code $($proc.ExitCode): $detail" } return $stdout } function Invoke-ProcessCapture([string]$FilePath, [string[]]$Arguments, [string]$WorkingDirectory = "") { $psi = New-Object System.Diagnostics.ProcessStartInfo $psi.FileName = $FilePath $psi.Arguments = (($Arguments | ForEach-Object { ConvertTo-ProcessArgument $_ }) -join " ") if ($WorkingDirectory) { $psi.WorkingDirectory = $WorkingDirectory } $psi.UseShellExecute = $false $psi.RedirectStandardOutput = $true $psi.RedirectStandardError = $true $proc = [System.Diagnostics.Process]::Start($psi) $stdout = $proc.StandardOutput.ReadToEnd() $stderr = $proc.StandardError.ReadToEnd() $proc.WaitForExit() return [pscustomobject]@{ ExitCode = $proc.ExitCode Stdout = $stdout Stderr = $stderr } } $Root = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "..\..")) Set-Location $Root if (-not $Version) { $Version = (python -c "from douyin.core.version import APP_VERSION; print(str(APP_VERSION).lstrip('Vv'))").Trim() } if (-not $DistDir) { $DistDir = Join-Path $Root "dist\DouyinGuaji-$Version" } if (-not $Installer) { $Installer = Join-Path $Root "dist\installer\DouyinGuaji-$Version-Setup.exe" } if (-not $WorkRoot) { $WorkRoot = Join-Path $Root "artifacts\fresh_install_ci" } $DistDir = Resolve-FullPath $DistDir $Installer = Resolve-FullPath $Installer $WorkRoot = Resolve-FullPath $WorkRoot $ArtifactsRoot = Resolve-FullPath "artifacts" Assert-UnderDirectory $WorkRoot $ArtifactsRoot "WorkRoot" Assert-DirExists $DistDir "portable dist" Assert-FileExists $Installer "installer" python -m tools.distribution_manifest $DistDir if ($LASTEXITCODE -ne 0) { throw "distribution manifest check failed" } python -m tools.exe_packing $DistDir --smoke-only if ($LASTEXITCODE -ne 0) { throw "exe smoke check failed" } if (Test-Path -LiteralPath $WorkRoot) { Remove-Item -LiteralPath $WorkRoot -Recurse -Force } New-Item -ItemType Directory -Force $WorkRoot | Out-Null $InstallDir = Join-Path $WorkRoot "DouyinGuajiApp" $InstallLog = Join-Path $WorkRoot "install.log" $InstallerHash = (Get-FileHash -LiteralPath $Installer -Algorithm SHA256).Hash $InstallArgs = @( "/VERYSILENT", "/SUPPRESSMSGBOXES", "/NORESTART", "/DIR=$InstallDir", "/LOG=$InstallLog" ) $installProc = Start-Process -FilePath $Installer -ArgumentList $InstallArgs -Wait -PassThru if ($installProc.ExitCode -ne 0) { throw "installer exited with code $($installProc.ExitCode); log=$InstallLog" } Assert-DirExists $InstallDir "installed app" Assert-FileExists (Join-Path $InstallDir "DouyinGuajiGUI.exe") "dashboard exe" Assert-FileExists (Join-Path $InstallDir "web_gui.exe") "web gui exe" Assert-FileExists (Join-Path $InstallDir "run_cli.exe") "cli exe" Assert-FileExists (Join-Path $InstallDir "config\config.json") "default runtime config" Assert-FileExists (Join-Path $InstallDir "platform-tools\adb.exe") "bundled adb" Assert-FileExists (Join-Path $InstallDir "platform-tools\AdbWinApi.dll") "ADB Win API" Assert-FileExists (Join-Path $InstallDir "platform-tools\AdbWinUsbApi.dll") "ADB USB API" Assert-FileExists (Join-Path $InstallDir "android-tools\ADBKeyboard.apk") "ADBKeyboard helper" $adbVersion = Invoke-CheckedProcess ` -FilePath (Join-Path $InstallDir "platform-tools\adb.exe") ` -Arguments @("version") ` -Label "bundled adb version" ` -WorkingDirectory $InstallDir $cliProbe = Invoke-ProcessCapture ` -FilePath (Join-Path $InstallDir "run_cli.exe") ` -Arguments @("--help") ` -WorkingDirectory $InstallDir if ($cliProbe.ExitCode -ne 86 -or (($cliProbe.Stdout + $cliProbe.Stderr) -notmatch "missing|缺少")) { throw "run_cli --help should fail closed without license, got exit=$($cliProbe.ExitCode)" } $licensePath = Join-Path $InstallDir "license.dat" if (Test-Path -LiteralPath $licensePath) { throw "fresh install unexpectedly contains license.dat" } $webLog = Join-Path $WorkRoot "web_gui.stdout.log" $webErr = Join-Path $WorkRoot "web_gui.stderr.log" $oldPort = $env:PORT $oldDisableUpdate = $env:DOUYIN_DISABLE_AUTO_UPDATE $oldLicenseRequired = $env:DOUYIN_LICENSE_REQUIRED $oldSkipOcr = $env:SKIP_OCR_INIT $webProc = $null try { $env:PORT = [string]$WebPort $env:DOUYIN_DISABLE_AUTO_UPDATE = "1" $env:DOUYIN_LICENSE_REQUIRED = "1" $env:SKIP_OCR_INIT = "1" $webProc = Start-Process ` -FilePath (Join-Path $InstallDir "web_gui.exe") ` -WorkingDirectory $InstallDir ` -RedirectStandardOutput $webLog ` -RedirectStandardError $webErr ` -WindowStyle Hidden ` -PassThru $rootResponse = $null $deadline = (Get-Date).AddSeconds($WebStartupTimeoutSeconds) while ((Get-Date) -lt $deadline) { if ($webProc.HasExited) { throw "web_gui exited early with code $($webProc.ExitCode); stdout=$webLog stderr=$webErr" } try { $rootResponse = Invoke-WebRequest -Uri "http://127.0.0.1:$WebPort/" -UseBasicParsing -TimeoutSec 2 break } catch { Start-Sleep -Milliseconds 500 } } if ($null -eq $rootResponse) { throw "web_gui did not become ready on port $WebPort" } if ([int]$rootResponse.StatusCode -ne 200) { throw "web root returned status $($rootResponse.StatusCode)" } if (-not ($rootResponse.Content.Contains("license-form") -and $rootResponse.Content.Contains("license-input"))) { throw "web root did not render the first-run license gate" } $licenseStatus = Invoke-RestMethod -Uri "http://127.0.0.1:$WebPort/api/license" -TimeoutSec 10 if ($licenseStatus.ok -ne $false -or "$($licenseStatus.reason)" -ne "missing") { throw "expected missing license status, got ok=$($licenseStatus.ok) reason=$($licenseStatus.reason)" } $csrf = Invoke-RestMethod -Uri "http://127.0.0.1:$WebPort/api/csrf-token" -TimeoutSec 10 $startRunStatus = $null $startRunBody = "" try { Invoke-RestMethod ` -Uri "http://127.0.0.1:$WebPort/api/start-run" ` -Method Post ` -ContentType "application/json" ` -Headers @{"X-CSRF-Token" = $csrf.token} ` -Body "{}" ` -TimeoutSec 10 | Out-Null throw "start-run unexpectedly succeeded without license" } catch { $startRunStatus = [int]$_.Exception.Response.StatusCode $startRunBody = $_.ErrorDetails.Message } if ($startRunStatus -ne 403 -or $startRunBody -notlike "*license_required*") { throw "expected /api/start-run 403 license_required, got status=$startRunStatus body=$startRunBody" } } finally { if ($webProc -and -not $webProc.HasExited) { Stop-Process -Id $webProc.Id -Force } $env:PORT = $oldPort $env:DOUYIN_DISABLE_AUTO_UPDATE = $oldDisableUpdate $env:DOUYIN_LICENSE_REQUIRED = $oldLicenseRequired $env:SKIP_OCR_INIT = $oldSkipOcr } $dashProc = $null try { $env:DOUYIN_DISABLE_AUTO_UPDATE = "1" $env:DOUYIN_LICENSE_REQUIRED = "1" $env:SKIP_OCR_INIT = "1" $dashProc = Start-Process ` -FilePath (Join-Path $InstallDir "DouyinGuajiGUI.exe") ` -WorkingDirectory $InstallDir ` -WindowStyle Hidden ` -PassThru Start-Sleep -Seconds $DashboardStartupSeconds if ($dashProc.HasExited) { throw "dashboard exited early with code $($dashProc.ExitCode)" } } finally { if ($dashProc -and -not $dashProc.HasExited) { Stop-Process -Id $dashProc.Id -Force } $env:DOUYIN_DISABLE_AUTO_UPDATE = $oldDisableUpdate $env:DOUYIN_LICENSE_REQUIRED = $oldLicenseRequired $env:SKIP_OCR_INIT = $oldSkipOcr } $report = [ordered]@{ ok = $true version = $Version installer = $Installer installer_sha256 = $InstallerHash install_dir = $InstallDir web_port = $WebPort checks = [ordered]@{ distribution_manifest = "pass" exe_smoke = "pass" silent_install = "pass" bundled_config = "pass" bundled_adb = "pass" adb_version = ($adbVersion -split "`r?`n" | Select-Object -First 1) cli_fails_closed_without_license = "pass" web_license_gate = "pass" web_runtime_requires_license = "pass" dashboard_waits_for_license = "pass" } } $reportPath = Join-Path $WorkRoot "fresh_install_report.json" $report | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath $reportPath -Encoding UTF8 Write-Host "[FRESH-INSTALL] PASS version=$Version report=$reportPath"