param([string]$OutputRoot, [string]$MapSeedRoot) $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', 'config.example.json', 'third-party-licenses.txt', 'dependency-licenses.txt', 'build-info.json', 'SHA256SUMS.txt') $ridTarget = 'x86_64-pc-windows-msvc' $ridMapReadme = '程序自动读取 EXE 同目录的 map_cache。请从地图管理下载离线瓦片,或将已有瓦片放入此目录。' function Assert-CleanDestination { if (Test-Path -LiteralPath $ridDestination) { foreach ($ridEntry in Get-ChildItem -LiteralPath $ridDestination -Force) { if ($ridEntry.PSIsContainer -and $ridEntry.Name -eq 'map_cache' -and !($ridEntry.Attributes -band [IO.FileAttributes]::ReparsePoint)) { $files = @(Get-ChildItem -LiteralPath $ridEntry.FullName -Force) if ($files.Count -eq 0 -or ($files.Count -eq 1 -and $files[0].Name -eq 'README.txt' -and !$files[0].PSIsContainer -and !($files[0].Attributes -band [IO.FileAttributes]::ReparsePoint) -and ([IO.File]::ReadAllText($files[0].FullName).TrimEnd() -ceq $ridMapReadme))) { continue } } 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-RidMapSeed([string]$Root) { $rootItem = Get-Item -LiteralPath $Root -Force if (-not $rootItem.PSIsContainer -or ($rootItem.Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw "地图种子目录必须为普通目录:$Root" } $kinds = if ($rootItem.Name -eq 'map_cache') { @('portable') } else { @('cache', 'offline') } foreach ($kind in $kinds) { $source = if ($kind -eq 'portable') { $rootItem.FullName } else { Join-Path $rootItem.FullName $kind } $destination = if ($kind -eq 'portable') { 'map_cache' } else { "maps/$kind" } if (-not (Test-Path -LiteralPath $source)) { continue } $sourceItem = Get-Item -LiteralPath $source -Force if (-not $sourceItem.PSIsContainer -or ($sourceItem.Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw "不能从链接或非目录读取地图:$source" } foreach ($item in Get-ChildItem -LiteralPath $source -Recurse -Force) { if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) { throw "地图种子目录含链接:$($item.FullName)" } if ($item.PSIsContainer) { continue } $relative = $item.FullName.Substring($source.Length + 1).Replace('\','/') $layer = '(vec_w|cva_w|img_w|cia_w)' if ($kind -ne 'cache' -and $relative -cmatch "^$layer[.]mbtiles$") { [pscustomobject]@{ Source=$item.FullName; Path="$destination/$relative" } } elseif ($relative -cmatch "^$layer/([0-9]{1,2})/([0-9]{1,7})/([0-9]{1,7})[.](tile|png|jpg|jpeg)$") { $z = [int]$Matches[2] $x = [int]$Matches[3] $y = [int]$Matches[4] if ($z -le 18 -and $x -lt (1 -shl $z) -and $y -lt (1 -shl $z) -and ($kind -ne 'cache' -or $Matches[5] -eq 'tile')) { [pscustomobject]@{ Source=$item.FullName; Path="$destination/$relative" } } } } } } function Get-RidSourceManifest { $ridInputs = @('Cargo.toml','Cargo.lock','rust-toolchain.toml','build.rs','config.example.json') | 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 $ridSeedFiles = @() $ridPaths = $ridWhitelist + 'map_cache/README.txt' if ($MapSeedRoot) { $ridSeedFiles = @(Get-RidMapSeed $MapSeedRoot | Sort-Object Path) $ridPaths += 'registry/data/README.txt' $ridPaths += @($ridSeedFiles | ForEach-Object Path) } & (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 --target $ridTarget 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 "$ridTarget/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 'config.example.json') -Destination $ridStage Copy-Item -LiteralPath (Join-Path $ridRepo 'crates/web-ui/public/vendor/LICENSE.txt') -Destination (Join-Path $ridStage 'third-party-licenses.txt') Get-Content -LiteralPath (Join-Path $ridRepo 'crates/web-ui/workbench/leaflet/LICENSE.txt') | Add-Content -LiteralPath (Join-Path $ridStage 'third-party-licenses.txt') -Encoding utf8 $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 $ridStage 'third-party-licenses.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 '源码在构建期间发生变化,已停止发布并保留原发布包;请在源码稳定后重新构建。' } foreach ($ridSeed in $ridSeedFiles) { $ridSeedDestination = Join-Path $ridStage $ridSeed.Path New-Item -ItemType Directory -Force -Path (Split-Path $ridSeedDestination -Parent) | Out-Null Copy-Item -LiteralPath $ridSeed.Source -Destination $ridSeedDestination } New-Item -ItemType Directory -Force -Path (Join-Path $ridStage 'map_cache') | Out-Null $ridMapReadme | Set-Content -LiteralPath (Join-Path $ridStage 'map_cache/README.txt') -Encoding utf8 if ($MapSeedRoot) { New-Item -ItemType Directory -Force -Path (Join-Path $ridStage 'registry/data') | Out-Null '应用首次启动时在此创建 SQLite 数据库。已有业务数据请在停机备份后单独迁移。' | Set-Content -LiteralPath (Join-Path $ridStage 'registry/data/README.txt') -Encoding utf8 } $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 mapSeedFileCount=$ridSeedFiles.Count sourceManifest=$ridSourceManifest } $ridBuildInfo | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath (Join-Path $ridStage 'build-info.json') -Encoding utf8 $ridPaths | 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 -Recurse -File -Force | ForEach-Object { $_.FullName.Substring($ridStage.Length + 1).Replace('\','/') }) if ($ridActual.Count -ne $ridPaths.Count -or @(Compare-Object $ridActual $ridPaths -CaseSensitive).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 $ridPaths) { $ridPublishFile = Join-Path $ridDestination $ridName $ridParent = Split-Path $ridPublishFile -Parent if (-not (Test-Path -LiteralPath $ridParent)) { New-Item -ItemType Directory -Force -Path $ridParent | Out-Null } Copy-Item -LiteralPath (Join-Path $ridStage $ridName) -Destination $ridPublishFile } $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 $ridPaths) { $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) { Get-ChildItem -LiteralPath $ridStage -Directory -Recurse -Force | Sort-Object { $_.FullName.Length } -Descending | ForEach-Object { if (@(Get-ChildItem -LiteralPath $_.FullName -Force).Count -eq 0) { [IO.Directory]::Delete($_.FullName, $false) } } if (@(Get-ChildItem -LiteralPath $ridStage -Force).Count -eq 0) { [IO.Directory]::Delete($ridStage, $false) } } if ((Test-Path -LiteralPath $ridStageRoot -PathType Container) -and @(Get-ChildItem -LiteralPath $ridStageRoot -Force).Count -eq 0) { Remove-Item -LiteralPath $ridStageRoot } }