param( [string]$FnpackPath = "", [string]$GoBin = "go" ) $ErrorActionPreference = "Stop" $root = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path $project = Join-Path $PSScriptRoot "havline" $mainRepo = Join-Path $root "havline" $version = (Get-Content -Raw (Join-Path $mainRepo "VERSION")).Trim() function Copy-Tree { param( [string]$Source, [string]$Destination ) New-Item -ItemType Directory -Force $Destination | Out-Null robocopy $Source $Destination /E /NFL /NDL /NJH /NJS /NP | Out-Null if ($LASTEXITCODE -ge 8) { throw "复制失败:$Source -> $Destination,退出码:$LASTEXITCODE" } } if (-not (Test-Path -LiteralPath $project)) { throw "找不到 Native 应用工程:$project" } if (-not $FnpackPath) { $localTool = Join-Path $root "fnos\fnpack.exe" $FnpackPath = if (Test-Path -LiteralPath $localTool) { $localTool } else { "fnpack" } } Push-Location (Join-Path $mainRepo "web") try { $env:VITE_BASE_PATH = "/app/havline/" npm run build if ($LASTEXITCODE -ne 0) { throw "前端构建失败,退出码:$LASTEXITCODE" } } finally { Pop-Location } $webDist = Join-Path $mainRepo "web\dist" $embedDist = Join-Path $mainRepo "cmd\havline\web\dist" New-Item -ItemType Directory -Force $embedDist | Out-Null robocopy $webDist $embedDist /E /NFL /NDL /NJH /NJS /NP | Out-Null if ($LASTEXITCODE -ge 8) { throw "复制前端产物失败,robocopy 退出码:$LASTEXITCODE" } $stamp = Get-Date -Format "yyyyMMddHHmmss" $stage = Join-Path $PSScriptRoot ".build\havline-$stamp" New-Item -ItemType Directory -Force $stage | Out-Null robocopy $project $stage /E /XF .DS_Store /NFL /NDL /NJH /NJS /NP | Out-Null if ($LASTEXITCODE -ge 8) { throw "复制 Native 应用工程失败,robocopy 退出码:$LASTEXITCODE" } Copy-Tree -Source (Join-Path $mainRepo "migrations") -Destination (Join-Path $stage "app\migrations") Copy-Tree -Source (Join-Path $mainRepo "web\assets\image") -Destination (Join-Path $stage "app\web\assets\image") $env:CGO_ENABLED = "0" $env:GOOS = "linux" foreach ($arch in @("amd64", "arm64")) { $outputDir = Join-Path $stage "app\server\$arch" New-Item -ItemType Directory -Force $outputDir | Out-Null $env:GOARCH = $arch Push-Location $mainRepo try { & $GoBin build -trimpath ` -ldflags "-X github.com/havline/havline/internal/version.Version=$version" ` -o (Join-Path $outputDir "havline") ` ./cmd/havline if ($LASTEXITCODE -ne 0) { throw "编译 $arch 二进制失败,退出码:$LASTEXITCODE" } } finally { Pop-Location } } Push-Location $PSScriptRoot try { & $FnpackPath build -d $stage if ($LASTEXITCODE -ne 0) { throw "fnpack build 失败,退出码:$LASTEXITCODE" } $output = Join-Path $PSScriptRoot "havline.fpk" $dist = Join-Path $PSScriptRoot "dist" New-Item -ItemType Directory -Force $dist | Out-Null $target = Join-Path $dist "havline-native-$version.fpk" Copy-Item -LiteralPath $output -Destination $target -Force $sizeMB = [math]::Round((Get-Item $target).Length / 1MB, 1) Write-Output "FPK: $target" Write-Output "FPK 大小:$sizeMB MB" } finally { Pop-Location }