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', '启动Rust版.cmd', '使用说明.md', 'qrcode-LICENSE.txt', 'dependency-licenses.txt', '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 -or ($ridEntry.Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw "发布目录含运行数据或非发布文件,已停止打包并保留原内容:$($ridEntry.FullName)。请通过 -OutputRoot 指定单独的输出目录。" } } } } # Refuse a used deployment folder before building or changing any published files. Assert-CleanDestination $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 try { cargo build --release --locked 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.exe') -Destination $ridStage 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 'registry/web/vendor/LICENSE.txt') -Destination (Join-Path $ridStage 'qrcode-LICENSE.txt') $ridLicenses = cargo tree --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 $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) } $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 { 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 } }