168 lines
5.2 KiB
PowerShell
168 lines
5.2 KiB
PowerShell
param(
|
|
[string]$FnpackPath = "",
|
|
[string]$GoBin = "go",
|
|
[string]$NginxImageVersion = "1.9.0"
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
$project = Join-Path $PSScriptRoot "havline"
|
|
$mainRepo = Join-Path $PSScriptRoot "app-src"
|
|
$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"
|
|
}
|
|
}
|
|
|
|
function Resolve-DockerCli {
|
|
if ($env:DOCKER_BIN) { return $env:DOCKER_BIN }
|
|
$command = Get-Command docker -ErrorAction SilentlyContinue
|
|
if ($command) { return $command.Source }
|
|
$default = "C:\Program Files\Docker\Docker\resources\bin\docker.exe"
|
|
if (Test-Path -LiteralPath $default) { return $default }
|
|
throw "找不到 docker 命令"
|
|
}
|
|
|
|
function Export-Nginx {
|
|
param(
|
|
[string]$DockerBin,
|
|
[string]$Platform,
|
|
[string]$Arch,
|
|
[string]$Image,
|
|
[string]$OutputDir
|
|
)
|
|
|
|
$archInfo = (& $DockerBin image inspect $Image --format '{{.Architecture}}' 2>$null)
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "找不到 Nginx 来源镜像:$Image"
|
|
}
|
|
if ($archInfo.Trim() -ne $Arch) {
|
|
throw "镜像架构不匹配:$Image 是 $($archInfo.Trim()),需要 $Arch"
|
|
}
|
|
|
|
$archDir = Join-Path $OutputDir $Arch
|
|
New-Item -ItemType Directory -Force $archDir | Out-Null
|
|
$script = @'
|
|
set -e
|
|
mkdir -p /out/lib
|
|
cp /usr/sbin/nginx /out/nginx
|
|
cp /etc/nginx/mime.types /out/mime.types
|
|
ldd /usr/sbin/nginx | awk '/=> \// {print $3} /^\// {print $1}' | sort -u | while read lib; do
|
|
case "$(basename "$lib")" in
|
|
libc.so.6|ld-linux-*.so.*) continue ;;
|
|
esac
|
|
cp "$lib" /out/lib/
|
|
done
|
|
'@
|
|
|
|
& $DockerBin run --rm --platform $Platform -v "${archDir}:/out" --entrypoint sh $Image -c $script
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "提取 $Arch Nginx 失败,退出码:$LASTEXITCODE"
|
|
}
|
|
|
|
Copy-Item -LiteralPath (Join-Path $archDir "mime.types") -Destination (Join-Path $OutputDir "mime.types") -Force
|
|
}
|
|
|
|
if (-not (Test-Path -LiteralPath $project)) {
|
|
throw "找不到 Native 应用工程:$project"
|
|
}
|
|
|
|
if (-not $FnpackPath) {
|
|
$localTool = Join-Path $PSScriptRoot "fnpack.exe"
|
|
$FnpackPath = if (Test-Path -LiteralPath $localTool) { $localTool } else { "fnpack" }
|
|
}
|
|
|
|
Push-Location (Join-Path $mainRepo "web")
|
|
try {
|
|
if (-not (Test-Path -LiteralPath (Join-Path (Get-Location) "node_modules"))) {
|
|
npm ci
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "安装前端依赖失败,退出码:$LASTEXITCODE"
|
|
}
|
|
}
|
|
$env:VITE_BASE_PATH = "/app/havline/"
|
|
$env:VITE_APP_VERSION = $version
|
|
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")
|
|
|
|
$serverDir = Join-Path $stage "app\server"
|
|
$dockerBin = Resolve-DockerCli
|
|
Export-Nginx -DockerBin $dockerBin -Platform "linux/amd64" -Arch "amd64" -Image "jayhub.top/mrlee/havline:$NginxImageVersion" -OutputDir $serverDir
|
|
Export-Nginx -DockerBin $dockerBin -Platform "linux/arm64" -Arch "arm64" -Image "havline:$NginxImageVersion" -OutputDir $serverDir
|
|
|
|
$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
|
|
}
|