更新代码

This commit is contained in:
hero920103 committed 2026-10-28 17:39:33 +08:00
1 parent eda7e8bc96
commit 6ff632e0b0
19 files changed
+2033 -197

No files matched your search

+86 -18
View File
@@ -1,31 +1,99 @@
# Build viewsagent for Windows (no console window).
# Usage: .\build.ps1
# Requires: Go 1.24+ and gcc from MSYS2 mingw-w64 (E:\Tools\msys64\mingw64\bin).
# 一键编译打包 viewsagent。
#
# 生成一个没有控制台窗口的 viewsagent.exe,直接放在项目根目录。
# 用法:双击本文件即可;或在 PowerShell 里执行 .\build.ps1
# 可选参数:notest 表示跳过单元测试。
$ErrorActionPreference = "Stop"
Set-Location -Path $PSScriptRoot
$mingw = "E:\Tools\msys64\mingw64\bin"
if (Test-Path $mingw) {
$env:Path = "$mingw;" + $env:Path
function Fail($msg) {
Write-Host ""
Write-Host " [失败] $msg" -ForegroundColor Red
exit 1
}
if (-not (Get-Command gcc -ErrorAction SilentlyContinue)) {
Write-Error "gcc not found. Fyne needs cgo; install mingw-w64-gcc first (see scripts/setup-gcc.sh)."
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"
if (-not (Test-Path "bin")) {
New-Item -ItemType Directory -Path "bin" | Out-Null
# ---------- 3. 跑测试 ----------
if ($args -contains "notest") {
Write-Host ""
Write-Host "==> 跳过单元测试" -ForegroundColor Cyan
} else {
Step "运行单元测试"
go test ./...
if ($LASTEXITCODE -ne 0) {
Fail "单元测试没有通过,已中止打包。"
}
}
Write-Host "==> go test" -ForegroundColor Cyan
go test ./...
if ($LASTEXITCODE -ne 0) { Write-Error "tests failed, build aborted." }
# ---------- 4. 编译 ----------
Step "编译可执行文件"
Write-Host "==> go build" -ForegroundColor Cyan
go build -trimpath -ldflags "-H windowsgui -s -w" -o "bin\viewsagent.exe" .
if ($LASTEXITCODE -ne 0) { Write-Error "build failed." }
$out = Join-Path $PSScriptRoot "viewsagent.exe"
if (Test-Path $out) {
try {
Remove-Item $out -Force -ErrorAction Stop
} catch {
Fail "无法覆盖 viewsagent.exe,请先关闭正在运行的 viewsagent 程序。"
}
}
$mb = [math]::Round((Get-Item "bin\viewsagent.exe").Length / 1MB, 1)
Write-Host "==> done: bin\viewsagent.exe ($mb MB)" -ForegroundColor Green
# -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 ""