28 lines
904 B
PowerShell
28 lines
904 B
PowerShell
|
|
# 保存调用进程的 Go 构建环境,脚本结束后恢复。
|
||
|
|
$hasGOOS = Test-Path Env:GOOS
|
||
|
|
$hasGOARCH = Test-Path Env:GOARCH
|
||
|
|
$hasGOFLAGS = Test-Path Env:GOFLAGS
|
||
|
|
$originalGOOS = $env:GOOS
|
||
|
|
$originalGOARCH = $env:GOARCH
|
||
|
|
$originalGOFLAGS = $env:GOFLAGS
|
||
|
|
|
||
|
|
Push-Location (Join-Path $PSScriptRoot '..')
|
||
|
|
try {
|
||
|
|
New-Item -ItemType Directory -Force -Path './build' | Out-Null
|
||
|
|
|
||
|
|
$env:GOOS = 'linux'
|
||
|
|
$env:GOARCH = 'amd64'
|
||
|
|
$env:GOFLAGS = '-buildvcs=false'
|
||
|
|
|
||
|
|
& go build -o './build/ops-files' './cmd/main/main.go'
|
||
|
|
if ($LASTEXITCODE -ne 0) {
|
||
|
|
throw "go build 失败,退出码:$LASTEXITCODE"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
finally {
|
||
|
|
if ($hasGOOS) { $env:GOOS = $originalGOOS } else { Remove-Item Env:GOOS }
|
||
|
|
if ($hasGOARCH) { $env:GOARCH = $originalGOARCH } else { Remove-Item Env:GOARCH }
|
||
|
|
if ($hasGOFLAGS) { $env:GOFLAGS = $originalGOFLAGS } else { Remove-Item Env:GOFLAGS }
|
||
|
|
Pop-Location
|
||
|
|
}
|