param( [switch]$SkipClean # 增量打包:跳过 clean,只重新 build ) $ErrorActionPreference = "Stop" $root = (Resolve-Path (Join-Path $PSScriptRoot "..\..")).Path $flutterDir = Join-Path $root "flutter_sender" $signalDir = Join-Path $root "signaling_server" $rustDir = Join-Path $root "rust_core" $dist = Join-Path $root "dist\TTSTDConnect-Windows" $flutterOutput = Join-Path $flutterDir "build\windows\x64\runner\Release" foreach ($required in @($flutterDir, $signalDir, $rustDir)) { if (-not (Test-Path $required)) { throw "Missing project directory: $required" } } # 停掉从 dist 或各构建目录运行的进程(会锁定 exe/dll,导致 clean 或复制失败)。 $ownedDirs = @( $dist, (Join-Path $rustDir "target"), $flutterOutput ) Get-Process -ErrorAction SilentlyContinue | Where-Object { $process = $_ $process.Path -and ($ownedDirs | Where-Object { $process.Path.StartsWith($_, [System.StringComparison]::OrdinalIgnoreCase) }) } | ForEach-Object { Write-Host "Stopping process using build output: $($_.ProcessName) ($($_.Id))" Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue } Start-Sleep -Milliseconds 500 function Invoke-BuildStep([string]$description, [string]$directory, [string[]]$arguments) { Write-Host "=== $description ===" Push-Location $directory try { & $arguments[0] @($arguments[1..($arguments.Count - 1)]) if ($LASTEXITCODE -ne 0) { throw "$description failed with exit code $LASTEXITCODE" } } finally { Pop-Location } } if (-not $SkipClean) { Write-Host "=== Cleaning all build artifacts ===" Invoke-BuildStep "Cleaning Flutter" $flutterDir @("flutter", "clean") Invoke-BuildStep "Cleaning Rust" $rustDir @("cargo", "clean") Invoke-BuildStep "Cleaning Go" $signalDir @("go", "clean") if (Test-Path $dist) { Remove-Item -Recurse -Force $dist } } Invoke-BuildStep "Building Rust Core cdylib" $rustDir @("cargo", "build", "--release") $coreDll = Join-Path $rustDir "target\release\ttstdconnect_core.dll" if (-not (Test-Path $coreDll)) { throw "Rust Core dynamic library not found: $coreDll" } Invoke-BuildStep "Building Flutter Windows app" $flutterDir @("flutter", "build", "windows", "--release") if (-not (Test-Path (Join-Path $flutterOutput "ttstd_sender.exe"))) { throw "Flutter Windows build output not found: $flutterOutput" } # flutter build 不会复制外部 cdylib,这里补一次。 Copy-Item -Force $coreDll (Join-Path $flutterOutput "ttstdconnect_core.dll") Invoke-BuildStep "Building embedded signaling server" $signalDir @("go", "build", "-o", (Join-Path $signalDir "signaling_server.exe"), ".") $signalExe = Join-Path $signalDir "signaling_server.exe" if (-not (Test-Path $signalExe)) { throw "Signaling server binary not found: $signalExe" } Write-Host "=== Assembling package ===" if (Test-Path $dist) { Remove-Item -Recurse -Force $dist } New-Item -ItemType Directory -Force -Path $dist | Out-Null Copy-Item -Recurse -Force (Join-Path $flutterOutput "*") $dist Copy-Item -Force $signalExe (Join-Path $dist "signaling_server.exe") Copy-Item -Force $coreDll (Join-Path $dist "ttstdconnect_core.dll") # 校验所有核心文件与构建输出哈希一致,避免旧文件残留。 $pairs = @( @((Join-Path $flutterOutput "ttstd_sender.exe"), (Join-Path $dist "ttstd_sender.exe")), @((Join-Path $flutterOutput "data\app.so"), (Join-Path $dist "data\app.so")), @($signalExe, (Join-Path $dist "signaling_server.exe")), @($coreDll, (Join-Path $dist "ttstdconnect_core.dll")) ) foreach ($pair in $pairs) { $source = $pair[0] $target = $pair[1] if (-not (Test-Path $source) -or -not (Test-Path $target)) { throw "Packaging file missing: source=$source target=$target" } if ((Get-FileHash $source -Algorithm SHA256).Hash -ne (Get-FileHash $target -Algorithm SHA256).Hash) { throw "Packaged file does not match build output: $target" } } Write-Host "Package ready: $dist" Write-Host "Mode: full rebuild. Use -SkipClean for incremental packaging." Write-Host "Default mode: local signaling on port 8080. Use Settings to disable it or switch to an external server."