905086
2026-11-09 cbb26873e88a7a13e868448c1cc578ce70b8c6ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 打包 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)"