# 一键编译打包 viewsagent。 # # 生成一个没有控制台窗口的 viewsagent.exe,直接放在项目根目录。 # 用法:双击本文件即可;或在 PowerShell 里执行 .\build.ps1 # 可选参数:notest 表示跳过单元测试。 $ErrorActionPreference = "Stop" Set-Location -Path $PSScriptRoot function Fail($msg) { Write-Host "" Write-Host " [失败] $msg" -ForegroundColor Red exit 1 } function Step($msg) { Write-Host "" Write-Host "==> $msg" -ForegroundColor Cyan } # ---------- 1. 找 go ---------- $go = Get-Command go -ErrorAction SilentlyContinue if (-not $go) { Fail "没有找到 go 命令。请先安装 Go(1.24 以上)并把 go.exe 加入 PATH。" } Write-Host " Go : $($go.Source)" -ForegroundColor DarkGray # ---------- 2. 找 gcc ---------- # Fyne 的 OpenGL 绑定 go-gl 是 cgo 包,必须有 C 编译器才能编译, # 所以这里自动去常见安装位置里找 gcc 并临时加进 PATH。 $gccDirs = @( "C:\msys64\ucrt64\bin", "C:\msys64\mingw64\bin", "$env:USERPROFILE\msys64\ucrt64\bin", "$env:USERPROFILE\msys64\mingw64\bin", "E:\Tools\msys64\ucrt64\bin", "E:\Tools\msys64\mingw64\bin", "C:\Tools\msys64\mingw64\bin", "C:\mingw64\bin", "C:\TDM-GCC-64\bin", "C:\ProgramData\chocolatey\bin" ) $gcc = Get-Command gcc -ErrorAction SilentlyContinue if (-not $gcc) { foreach ($dir in $gccDirs) { if (Test-Path (Join-Path $dir "gcc.exe")) { $env:Path = "$dir;" + $env:Path $gcc = Get-Command gcc -ErrorAction SilentlyContinue break } } } if (-not $gcc) { Fail "没有找到 gcc。Fyne 依赖 cgo,需要先装 mingw-w64(MSYS2: pacman -S mingw-w64-x86_64-gcc,或 chocolatey: choco install mingw)。" } Write-Host " gcc : $($gcc.Source)" -ForegroundColor DarkGray $env:CGO_ENABLED = "1" # ---------- 3. 跑测试 ---------- if ($args -contains "notest") { Write-Host "" Write-Host "==> 跳过单元测试" -ForegroundColor Cyan } else { Step "运行单元测试" go test ./... if ($LASTEXITCODE -ne 0) { Fail "单元测试没有通过,已中止打包。" } } # ---------- 4. 编译 ---------- Step "编译可执行文件" $out = Join-Path $PSScriptRoot "viewsagent.exe" if (Test-Path $out) { try { Remove-Item $out -Force -ErrorAction Stop } catch { Fail "无法覆盖 viewsagent.exe,请先关闭正在运行的 viewsagent 程序。" } } # -H windowsgui 去掉控制台窗口,程序就是纯粹的桌面应用。 go build -trimpath -ldflags "-H windowsgui -s -w" -o $out . if ($LASTEXITCODE -ne 0) { Fail "编译失败。" } if (-not (Test-Path $out)) { Fail "编译结束但没有生成 viewsagent.exe。" } $size = [math]::Round((Get-Item $out).Length / 1MB, 1) Write-Host "" Write-Host " [完成] viewsagent.exe 已生成($size MB)" -ForegroundColor Green Write-Host " 路径 : $out" -ForegroundColor Gray Write-Host ""