Files
LinkPort/scripts/build.ps1
T
曾志威 7e5ca6b497
continuous-integration/drone Build encountered an error
ci: use China dependency mirrors and persist npm cache
2026-10-14 10:06:34 +08:00

55 lines
2.4 KiB
PowerShell

param([string]$Version, [switch]$SkipTests, [switch]$RebuildWeb, [string]$OutputPath = 'bin/linkport.exe')
$ErrorActionPreference = 'Stop'
. (Join-Path $PSScriptRoot 'build-lock.ps1')
$buildLock = Enter-LinkPortBuildLock
$previousPath = $env:PATH
try {
$buildTimer = [Diagnostics.Stopwatch]::StartNew()
$phaseTimer = [Diagnostics.Stopwatch]::StartNew()
function Write-BuildPhase([string]$Name) {
Write-Host ('{0}: {1:N1}s' -f $Name, $phaseTimer.Elapsed.TotalSeconds)
$phaseTimer.Restart()
}
. (Join-Path $PSScriptRoot 'version.ps1')
$buildInfo = Get-linkportBuildInfo -Version $Version
Set-Location -LiteralPath (Split-Path $PSScriptRoot -Parent)
$env:GOTOOLCHAIN = 'go1.27.1'
$env:GOPROXY = 'https://goproxy.cn'
$env:GO111MODULE = 'on'
. (Join-Path $PSScriptRoot 'go-toolchain.ps1')
$goExecutable = Get-LinkPortGoExecutable -ProjectRoot (Split-Path $PSScriptRoot -Parent)
. (Join-Path $PSScriptRoot 'node-toolchain.ps1')
$nodeExecutable = Initialize-LinkPortNodeRuntime -ProjectRoot (Split-Path $PSScriptRoot -Parent)
$keepPath = Join-Path $PWD 'web/dist/.gitkeep'
$keepBytes = [byte[]]@()
if (Test-Path -LiteralPath $keepPath) { $keepBytes = [IO.File]::ReadAllBytes($keepPath) }
try {
$webArguments = @((Join-Path $PSScriptRoot 'build-web.mjs'))
if ($RebuildWeb) { $webArguments += '--force' }
& $nodeExecutable @webArguments
if ($LASTEXITCODE -ne 0) { throw 'Frontend build failed' }
} finally {
New-Item -ItemType Directory -Path (Split-Path $keepPath -Parent) -Force | Out-Null
[IO.File]::WriteAllBytes($keepPath, $keepBytes)
}
Write-BuildPhase 'Frontend and dependencies'
if (!$SkipTests) {
& $goExecutable test ./... -count=1 -timeout=120s
if ($LASTEXITCODE -ne 0) { throw 'Go tests failed' }
Write-BuildPhase 'Go tests'
} else {
Write-Host 'Fast development build: full Go tests skipped (use start-dev.cmd -FullCheck to run them).'
}
if (![IO.Path]::IsPathRooted($OutputPath)) { $OutputPath = Join-Path $PWD $OutputPath }
$OutputPath = [IO.Path]::GetFullPath($OutputPath)
New-Item -ItemType Directory -Force (Split-Path $OutputPath -Parent) | Out-Null
& $goExecutable build -trimpath -ldflags $buildInfo.LDFlags -o $OutputPath ./cmd/linkport
if ($LASTEXITCODE -ne 0) { throw 'Go build failed' }
Write-BuildPhase 'Go build'
Write-Host ('Build total: {0:N1}s' -f $buildTimer.Elapsed.TotalSeconds)
Write-Output $OutputPath
} finally {
$env:PATH = $previousPath
$buildLock.Dispose()
}