# 打包 gamebox-server 为 Linux 部署包
|
# 用法:在 server 目录下执行 .\build-linux.ps1 [-Arch amd64|arm64] [-WithFiles]
|
# -WithFiles: 一并打包游戏安装包(uploads/file,体积大,默认不带)
|
param(
|
[ValidateSet('amd64', 'arm64')]
|
[string]$Arch = 'amd64',
|
[switch]$WithFiles
|
)
|
|
$ErrorActionPreference = 'Stop'
|
$root = $PSScriptRoot
|
$name = "gamebox-linux-$Arch"
|
$dist = Join-Path $root 'dist'
|
$stage = Join-Path $dist $name
|
|
Write-Host "==> 清理旧目录"
|
Remove-Item -Recurse -Force $stage -ErrorAction SilentlyContinue
|
New-Item -ItemType Directory -Force -Path $stage | Out-Null
|
|
Write-Host "==> 交叉编译 linux/$Arch"
|
$env:GOOS = 'linux'
|
$env:GOARCH = $Arch
|
$env:CGO_ENABLED = '0'
|
Push-Location $root
|
try {
|
go build -trimpath -ldflags '-s -w' -o (Join-Path $stage 'gamebox-server') .
|
if ($LASTEXITCODE -ne 0) { throw 'go build 失败' }
|
} finally {
|
Pop-Location
|
Remove-Item Env:GOOS, Env:GOARCH, Env:CGO_ENABLED -ErrorAction SilentlyContinue
|
}
|
|
Write-Host "==> 拷贝静态页面与配置"
|
Copy-Item (Join-Path $root 'admin') $stage -Recurse
|
Copy-Item (Join-Path $root 'showcase') $stage -Recurse
|
Copy-Item (Join-Path $root 'download') $stage -Recurse
|
Copy-Item (Join-Path $root 'config.yaml') $stage
|
|
Write-Host "==> 拷贝图片资源(封面/截图,数据库里的 /static/... 均指向这些文件)"
|
foreach ($d in 'cover', 'covers', 'image') {
|
$src = Join-Path $root "uploads/$d"
|
if (Test-Path $src) {
|
Copy-Item $src (Join-Path $stage 'uploads') -Recurse
|
} else {
|
New-Item -ItemType Directory -Force -Path (Join-Path $stage "uploads/$d") | Out-Null
|
}
|
}
|
|
if ($WithFiles) {
|
Write-Host "==> 拷贝游戏安装包(uploads/file)"
|
$src = Join-Path $root 'uploads/file'
|
if (Test-Path $src) { Copy-Item $src (Join-Path $stage 'uploads') -Recurse }
|
}
|
|
Write-Host "==> 创建其余空目录"
|
foreach ($d in 'file', 'tmp') {
|
New-Item -ItemType Directory -Force -Path (Join-Path $stage "uploads/$d") | Out-Null
|
}
|
|
Write-Host "==> 生成启动脚本"
|
$start = @(
|
'#!/bin/sh',
|
'cd "$(dirname "$0")"',
|
'exec ./gamebox-server'
|
) -join "`n"
|
[System.IO.File]::WriteAllText((Join-Path $stage 'start.sh'), $start + "`n", (New-Object System.Text.UTF8Encoding($false)))
|
|
Write-Host "==> 打包 tar.gz"
|
$pkg = Join-Path $dist "$name.tar.gz"
|
Remove-Item -Force $pkg -ErrorAction SilentlyContinue
|
tar -czf $pkg -C $dist $name
|
if ($LASTEXITCODE -ne 0) { throw 'tar 打包失败' }
|
|
$size = [math]::Round((Get-Item $pkg).Length / 1MB, 1)
|
Write-Host "==> 完成: $pkg (${size} MB)"
|