Files
cdxs/scripts/publish-binaries.ps1
T

102 lines
3.3 KiB
PowerShell

param(
[string]$Version = "",
[string]$OutputDir = "dist",
[string]$RustImage = "rust:1-bookworm",
[switch]$SkipDockerLinux,
[switch]$Clean
)
$ErrorActionPreference = "Stop"
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..")
Set-Location $repoRoot
if (-not $Version) {
$cargoToml = Get-Content -Raw -Path "Cargo.toml"
if ($cargoToml -notmatch '(?m)^version\s*=\s*"([^"]+)"') {
throw "Cannot read package version from Cargo.toml"
}
$Version = $Matches[1]
}
$dist = Join-Path $repoRoot $OutputDir
if ($Clean -and (Test-Path $dist)) {
Remove-Item -LiteralPath $dist -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $dist | Out-Null
function Copy-ReleaseFile {
param(
[string]$SourceFile,
[string]$OutputName
)
if (-not (Test-Path $SourceFile)) {
throw "Missing release binary: $SourceFile"
}
$outputPath = Join-Path $dist $OutputName
Copy-Item -LiteralPath $SourceFile -Destination $outputPath -Force
Write-Host "Wrote $outputPath"
}
Write-Host "Building Windows x64"
cargo build --release --target x86_64-pc-windows-msvc
Copy-ReleaseFile `
-SourceFile (Join-Path $repoRoot "target/x86_64-pc-windows-msvc/release/cdxs.exe") `
-OutputName "cdxs-$Version-windows-x64.exe"
if (-not $SkipDockerLinux) {
$linuxTargets = @(
@{ Name = "linux-x64"; RustTarget = "x86_64-unknown-linux-gnu"; Platform = "linux/amd64" },
@{ Name = "linux-arm64"; RustTarget = "aarch64-unknown-linux-gnu"; Platform = "linux/arm64" }
)
foreach ($target in $linuxTargets) {
$containerName = "cdxs-build-$($target.Name)-$([guid]::NewGuid().ToString('N'))"
$imageTag = "cdxs-build:$($target.Name)"
$tmpOutDir = Join-Path $dist ".tmp-$($target.Name)"
New-Item -ItemType Directory -Force -Path $tmpOutDir | Out-Null
Write-Host "Building $($target.Name) with Docker platform $($target.Platform)"
$buildArgs = @(
"buildx", "build",
"--platform", $target.Platform,
"--target", "builder",
"--build-arg", "RUST_TARGET=$($target.RustTarget)",
"--build-arg", "RUST_IMAGE=$RustImage",
"--load",
"-t", $imageTag,
"-f", "scripts/docker/Dockerfile.release",
"."
)
docker @buildArgs
if ($LASTEXITCODE -ne 0) {
throw "Docker build failed for $($target.Name)"
}
docker image inspect $imageTag | Out-Null
if ($LASTEXITCODE -ne 0) {
throw "Docker image was not created: $imageTag"
}
$containerId = docker create --name $containerName $imageTag
if ($LASTEXITCODE -ne 0 -or -not $containerId) {
throw "Docker create failed for $imageTag"
}
try {
docker cp "${containerName}:/out/cdxs" (Join-Path $tmpOutDir "cdxs")
if ($LASTEXITCODE -ne 0) {
throw "Docker cp failed for $($target.Name)"
}
} finally {
docker rm -f $containerName | Out-Null
}
Copy-ReleaseFile `
-SourceFile (Join-Path $tmpOutDir "cdxs") `
-OutputName "cdxs-$Version-$($target.Name)"
Remove-Item -LiteralPath $tmpOutDir -Recurse -Force
}
}
Write-Host "Binary release artifacts are in $dist"