param([string]$OutputRoot) $ErrorActionPreference = 'Stop' . (Join-Path $PSScriptRoot 'rust-env.ps1') $ridRepo = Split-Path $PSScriptRoot -Parent if (-not $OutputRoot) { $OutputRoot = Join-Path $ridRepo 'dist' } $ridOutputRoot = [IO.Path]::GetFullPath($OutputRoot) $ridDestination = Join-Path $ridOutputRoot 'FPV-RID-Rust' $ridWhitelist = @('fpv-rid-registry.exe', 'rid-map-downloader.exe', 'map-downloader-LICENSE.txt', '启动Rust版.cmd', '使用说明.md', 'third-party-licenses.txt', 'dependency-licenses.txt', 'build-info.json', 'SHA256SUMS.txt') function Assert-CleanDestination { if (Test-Path -LiteralPath $ridDestination) { foreach ($ridEntry in Get-ChildItem -LiteralPath $ridDestination -Force) { if ($ridEntry.PSIsContainer -or ($ridEntry.Name -notin $ridWhitelist -and $ridEntry.Name -ne 'qrcode-LICENSE.txt') -or ($ridEntry.Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw "发布目录含运行数据或非发布文件,已停止打包并保留原内容:$($ridEntry.FullName)。请通过 -OutputRoot 指定单独的输出目录。" } } } } function Get-RidSourceManifest { $ridInputs = @('Cargo.toml','Cargo.lock','rust-toolchain.toml','build.rs') | ForEach-Object { Get-Item -LiteralPath (Join-Path $ridRepo $_) } $ridInputs += @('src','crates','xtask','scripts','.cargo') | ForEach-Object { Get-ChildItem -LiteralPath (Join-Path $ridRepo $_) -Recurse -File } @($ridInputs | Sort-Object FullName | ForEach-Object { [ordered]@{path=$_.FullName.Substring($ridRepo.Length + 1).Replace('\','/');sha256=(Get-FileHash -LiteralPath $_.FullName -Algorithm SHA256).Hash} }) } # Refuse a used deployment folder before building or changing any published files. Assert-CleanDestination & (Join-Path $PSScriptRoot 'build-map-downloader.ps1') $ridStageRoot = Join-Path $ridOutputRoot ('.package-' + [guid]::NewGuid().ToString('N')) $ridStage = Join-Path $ridStageRoot 'FPV-RID-Rust' $ridTemporaryZip = Join-Path $ridStageRoot 'release.zip' Push-Location $ridRepo $ridBuildLock = $null $ridOldBuildOwner = $env:RID_PACKAGE_BUILD try { $ridBuildLock = [IO.File]::Open((Join-Path $ridRepo '.build.lock'), [IO.FileMode]::OpenOrCreate, [IO.FileAccess]::ReadWrite, [IO.FileShare]::None) $env:RID_PACKAGE_BUILD = '1' $ridInitialManifest = @(Get-RidSourceManifest) $ridInitialManifestJson = ConvertTo-Json -InputObject $ridInitialManifest -Depth 5 -Compress cargo xtask web if ($LASTEXITCODE -ne 0) { throw 'Rust frontend build failed' } cargo build --release --locked -p fpv-rid-registry if ($LASTEXITCODE -ne 0) { throw 'Release build failed' } $ridMetadata = cargo metadata --locked --format-version 1 --no-deps | ConvertFrom-Json if ($LASTEXITCODE -ne 0) { throw 'Cargo metadata failed' } New-Item -ItemType Directory -Force -Path $ridStage | Out-Null Copy-Item -LiteralPath (Join-Path $ridMetadata.target_directory 'release/fpv-rid-registry-fullstack.exe') -Destination (Join-Path $ridStage 'fpv-rid-registry.exe') Copy-Item -LiteralPath $env:RID_MAP_DOWNLOADER -Destination (Join-Path $ridStage 'rid-map-downloader.exe') Copy-Item -LiteralPath (Join-Path $PSScriptRoot 'map-downloader/LICENSE-upstream.txt') -Destination (Join-Path $ridStage 'map-downloader-LICENSE.txt') Copy-Item -LiteralPath (Join-Path $ridRepo '启动Rust版.cmd') -Destination $ridStage Copy-Item -LiteralPath (Join-Path $ridRepo 'doc/registry/rust/使用说明.md') -Destination $ridStage Copy-Item -LiteralPath (Join-Path $ridRepo 'crates/web-ui/public/vendor/LICENSE.txt') -Destination (Join-Path $ridStage 'third-party-licenses.txt') $ridLicenses = cargo tree --workspace --locked --prefix none --format '{p} {l}' if ($LASTEXITCODE -ne 0) { throw 'Dependency inventory failed' } $ridLicenses | Set-Content -LiteralPath (Join-Path $ridStage 'dependency-licenses.txt') -Encoding utf8 Get-Content -LiteralPath (Join-Path $ridRepo 'crates/web-ui/public/vendor/LICENSE.txt') | Add-Content -LiteralPath (Join-Path $ridStage 'dependency-licenses.txt') -Encoding utf8 $ridSourceManifest = @(Get-RidSourceManifest) if ((ConvertTo-Json -InputObject $ridSourceManifest -Depth 5 -Compress) -ne $ridInitialManifestJson) { throw '源码在构建期间发生变化,已停止发布并保留原发布包;请在源码稳定后重新构建。' } $ridBuildInfo = [ordered]@{ builtAtUtc=[DateTime]::UtcNow.ToString('o') gitRevision=(git rev-parse HEAD) workingTreeDirty=[bool](git status --porcelain) rust=(rustc --version) trunk='0.22.0' wasmBindgen='0.2.129' executableSha256=(Get-FileHash -LiteralPath (Join-Path $ridStage 'fpv-rid-registry.exe') -Algorithm SHA256).Hash sourceManifest=$ridSourceManifest } $ridBuildInfo | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath (Join-Path $ridStage 'build-info.json') -Encoding utf8 $ridWhitelist | Where-Object { $_ -ne 'SHA256SUMS.txt' } | ForEach-Object { '{0} {1}' -f (Get-FileHash -LiteralPath (Join-Path $ridStage $_) -Algorithm SHA256).Hash, $_ } | Set-Content -LiteralPath (Join-Path $ridStage 'SHA256SUMS.txt') -Encoding utf8 $ridActual = @(Get-ChildItem -LiteralPath $ridStage -Force) if ($ridActual.Count -ne $ridWhitelist.Count -or @($ridActual | Where-Object { $_.PSIsContainer -or $_.Name -notin $ridWhitelist }).Count) { throw 'Staging directory does not match the release whitelist' } # Only the fresh, checked staging tree is ever compressed. Compress-Archive -LiteralPath $ridStage -DestinationPath $ridTemporaryZip Assert-CleanDestination New-Item -ItemType Directory -Force -Path $ridDestination | Out-Null foreach ($ridName in $ridWhitelist) { Copy-Item -LiteralPath (Join-Path $ridStage $ridName) -Destination (Join-Path $ridDestination $ridName) } $ridObsoleteLicense = Join-Path $ridDestination 'qrcode-LICENSE.txt' if (Test-Path -LiteralPath $ridObsoleteLicense -PathType Leaf) { Remove-Item -LiteralPath $ridObsoleteLicense } $ridZip = Join-Path $ridOutputRoot 'FPV-RID-Rust-Windows-x64.zip' Move-Item -LiteralPath $ridTemporaryZip -Destination $ridZip -Force '{0} FPV-RID-Rust-Windows-x64.zip' -f (Get-FileHash -LiteralPath $ridZip -Algorithm SHA256).Hash | Set-Content -LiteralPath "$ridZip.sha256" -Encoding utf8 Write-Output $ridDestination Write-Output $ridZip } finally { $env:RID_PACKAGE_BUILD = $ridOldBuildOwner if ($ridBuildLock) { $ridBuildLock.Dispose() } Pop-Location # Clean only files this invocation created. Never recursively delete an output directory. foreach ($ridName in $ridWhitelist) { $ridOwnedFile = Join-Path $ridStage $ridName if (Test-Path -LiteralPath $ridOwnedFile -PathType Leaf) { Remove-Item -LiteralPath $ridOwnedFile } } if (Test-Path -LiteralPath $ridTemporaryZip -PathType Leaf) { Remove-Item -LiteralPath $ridTemporaryZip } if ((Test-Path -LiteralPath $ridStage -PathType Container) -and @(Get-ChildItem -LiteralPath $ridStage -Force).Count -eq 0) { Remove-Item -LiteralPath $ridStage } if ((Test-Path -LiteralPath $ridStageRoot -PathType Container) -and @(Get-ChildItem -LiteralPath $ridStageRoot -Force).Count -eq 0) { Remove-Item -LiteralPath $ridStageRoot } }