fix(updater): build in temp directory, replace old launcher binary, and cleanup temp

This commit is contained in:
CAtlas0784 committed 2026-10-30 21:31:53 +07:00
1 parent 0ec9f62857
commit 0a97749a1c
8 files changed
+281 -98

No files matched your search

+115 -63
View File
@@ -1,5 +1,6 @@
# ==============================================================================
# G-Launcher Auto-Updater & Environment Bootstrap Script
# Architecture: Download to Temp -> Build in Temp -> Replace Old -> Delete Temp
# ==============================================================================
param(
[string]$Branch = "Dev",
@@ -13,7 +14,11 @@ $ErrorActionPreference = "Stop"
$rootDir = Split-Path -Parent $PSScriptRoot
Set-Location $rootDir
$toolsDir = Join-Path $rootDir "tools"
$binDir = Join-Path $rootDir "bin"
$targetExe = Join-Path $binDir "G-Launcher.exe"
if (!(Test-Path $toolsDir)) { New-Item -ItemType Directory -Path $toolsDir -Force | Out-Null }
if (!(Test-Path $binDir)) { New-Item -ItemType Directory -Path $binDir -Force | Out-Null }
Write-Host "==========================================================" -ForegroundColor Cyan
Write-Host " G-Launcher: Auto-Update & Local Build System" -ForegroundColor Cyan
@@ -123,79 +128,126 @@ if (-not $wailsCmd) {
Write-Host " -> Wails v3 CLI is ready" -ForegroundColor Gray
# ------------------------------------------------------------------------------
# 5. Git Pull latest code
# 5. Download to Temp -> Build in Temp -> Replace Old -> Delete Temp
# ------------------------------------------------------------------------------
Write-Host "[5/6] Syncing latest code from branch '$Branch'..." -ForegroundColor White
$env:GIT_TERMINAL_PROMPT = "0"
if (Test-Path (Join-Path $rootDir ".git")) {
try {
& git fetch origin $Branch --progress
& git checkout $Branch
$pullOutput = & git pull origin $Branch
Write-Host " -> $pullOutput" -ForegroundColor Gray
} catch {
Write-Host "Warning: Could not pull latest changes (offline or local modifications). Proceeding with current local code." -ForegroundColor DarkYellow
}
} else {
Write-Host ">> Cloning repository from $RepoUrl..." -ForegroundColor Yellow
& git clone --branch $Branch $RepoUrl .
}
# ------------------------------------------------------------------------------
# 6. Build Project (Frontend + Go Backend)
# ------------------------------------------------------------------------------
Write-Host "[6/6] Compiling G-Launcher..." -ForegroundColor White
Write-Host " [6.1] Installing Frontend dependencies..." -ForegroundColor Gray
& npm --prefix frontend install --no-audit --no-fund
Write-Host " [6.2] Building React 19 Frontend..." -ForegroundColor Gray
& npm --prefix frontend run build
Write-Host " [6.3] Running go mod tidy..." -ForegroundColor Gray
& go mod tidy
$binDir = Join-Path $rootDir "bin"
if (!(Test-Path $binDir)) { New-Item -ItemType Directory -Path $binDir -Force | Out-Null }
$outputExe = Join-Path $binDir "G-Launcher.exe"
$sysoPath = Join-Path $rootDir "wails_windows_amd64.syso"
$tempBuildDir = Join-Path $rootDir "temp\build_workspace"
$tempExe = Join-Path $tempBuildDir "bin\G-Launcher.exe"
try {
Write-Host " [6.4] Generating Windows Manifest & Syso..." -ForegroundColor Gray
if (Get-Command wails3 -ErrorAction SilentlyContinue) {
& wails3 generate syso -arch amd64 -icon build/windows/icon.ico -manifest build/windows/wails.exe.manifest -info build/windows/info.json -out $sysoPath
Write-Host "[5/6] Preparing clean temporary workspace..." -ForegroundColor White
if (Test-Path $tempBuildDir) {
Remove-Item -Path $tempBuildDir -Recurse -Force -ErrorAction SilentlyContinue
}
} catch {
Write-Host "Warning: Syso generation skipped, building executable without embedded syso." -ForegroundColor DarkYellow
}
New-Item -ItemType Directory -Path $tempBuildDir -Force | Out-Null
Write-Host " [6.5] Compiling Go backend binary..." -ForegroundColor Gray
& go build -tags production -trimpath -buildvcs=false -ldflags="-w -s -H windowsgui" -o $outputExe .
Write-Host ">> Downloading latest source code from branch '$Branch' into temp..." -ForegroundColor Yellow
$env:GIT_TERMINAL_PROMPT = "0"
$cloned = $false
try {
& git clone --depth 1 --branch $Branch $RepoUrl $tempBuildDir
$cloned = $true
} catch {
Write-Host "Warning: Remote clone failed. Attempting local repository clone..." -ForegroundColor DarkYellow
}
if (Test-Path $sysoPath) {
Remove-Item -Path $sysoPath -Force -ErrorAction SilentlyContinue
}
if (-not $cloned -and (Test-Path (Join-Path $rootDir ".git"))) {
& git clone --depth 1 --branch $Branch $rootDir $tempBuildDir
$cloned = $true
}
if ($LASTEXITCODE -ne 0 -or !(Test-Path $outputExe)) {
if (-not $cloned) {
throw "Failed to download repository source into temporary directory!"
}
Write-Host "[6/6] Building new G-Launcher in temporary workspace..." -ForegroundColor White
Push-Location $tempBuildDir
Write-Host " [6.1] Installing Frontend dependencies in temp..." -ForegroundColor Gray
& npm --prefix frontend install --no-audit --no-fund
Write-Host " [6.2] Compiling React 19 Frontend in temp..." -ForegroundColor Gray
& npm --prefix frontend run build
Write-Host " [6.3] Running go mod tidy in temp..." -ForegroundColor Gray
& go mod tidy
$tempBinDir = Join-Path $tempBuildDir "bin"
if (!(Test-Path $tempBinDir)) { New-Item -ItemType Directory -Path $tempBinDir -Force | Out-Null }
$sysoPath = Join-Path $tempBuildDir "wails_windows_amd64.syso"
try {
Write-Host " [6.4] Generating Windows Manifest & Syso in temp..." -ForegroundColor Gray
if (Get-Command wails3 -ErrorAction SilentlyContinue) {
& wails3 generate syso -arch amd64 -icon build/windows/icon.ico -manifest build/windows/wails.exe.manifest -info build/windows/info.json -out $sysoPath
}
} catch {
Write-Host "Warning: Syso generation skipped, building executable without embedded syso." -ForegroundColor DarkYellow
}
Write-Host " [6.5] Compiling Go backend binary in temp..." -ForegroundColor Gray
& go build -tags production -trimpath -buildvcs=false -ldflags="-w -s -H windowsgui" -o $tempExe .
if (Test-Path $sysoPath) {
Remove-Item -Path $sysoPath -Force -ErrorAction SilentlyContinue
}
if ($LASTEXITCODE -ne 0 -or !(Test-Path $tempExe)) {
throw "Compilation inside temporary workspace failed!"
}
$upxCmd = Get-Command upx -ErrorAction SilentlyContinue
if ($upxCmd) {
Write-Host " [6.6] Compressing binary with UPX in temp..." -ForegroundColor Gray
& upx --best --lzma $tempExe
}
Pop-Location
# --------------------------------------------------------------------------
# Replace old executable with newly built binary (with rollback protection)
# --------------------------------------------------------------------------
Write-Host ">> Replacing old launcher binary with new build..." -ForegroundColor Cyan
$backupExe = "$targetExe.old"
if (Test-Path $targetExe) {
if (Test-Path $backupExe) {
Remove-Item -Path $backupExe -Force -ErrorAction SilentlyContinue
}
Rename-Item -LiteralPath $targetExe -NewName (Split-Path -Leaf $backupExe) -Force
try {
Move-Item -LiteralPath $tempExe -Destination $targetExe -Force
Remove-Item -LiteralPath $backupExe -Force -ErrorAction SilentlyContinue
} catch {
Write-Host "Error during replacement! Rolling back..." -ForegroundColor Red
if (Test-Path $backupExe) {
Move-Item -LiteralPath $backupExe -Destination $targetExe -Force -ErrorAction SilentlyContinue
}
throw "Replacement failed and was rolled back: $_"
}
} else {
Move-Item -LiteralPath $tempExe -Destination $targetExe -Force
}
$exeSize = (Get-Item $targetExe).Length / 1MB
Write-Host ""
Write-Host "==========================================================" -ForegroundColor Green
Write-Host ("Build & Update Successful! File: bin/G-Launcher.exe ({0:N2} MB)" -f $exeSize) -ForegroundColor Green
Write-Host "==========================================================" -ForegroundColor Green
Write-Host ""
Write-Host "[ERROR] Compilation failed!" -ForegroundColor Red
exit 1
}
$upxCmd = Get-Command upx -ErrorAction SilentlyContinue
if ($upxCmd) {
Write-Host " [6.6] Compressing binary with UPX..." -ForegroundColor Gray
& upx --best --lzma $outputExe
} finally {
# --------------------------------------------------------------------------
# Always delete temporary workspace
# --------------------------------------------------------------------------
Set-Location $rootDir
if (Test-Path $tempBuildDir) {
Write-Host "🧹 Cleaning up and deleting temporary workspace ($tempBuildDir)..." -ForegroundColor Yellow
Remove-Item -LiteralPath $tempBuildDir -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "[OK] Temporary workspace deleted!" -ForegroundColor Green
}
}
$exeSize = (Get-Item $outputExe).Length / 1MB
Write-Host ""
Write-Host "==========================================================" -ForegroundColor Green
Write-Host ("Build Successful! Output: bin/G-Launcher.exe ({0:N2} MB)" -f $exeSize) -ForegroundColor Green
Write-Host "==========================================================" -ForegroundColor Green
Write-Host ""
if (-not $NoLaunch) {
Write-Host "Launching G-Launcher..." -ForegroundColor Cyan
Start-Process -FilePath $outputExe
Write-Host "🎮 Launching G-Launcher..." -ForegroundColor Cyan
Start-Process -FilePath $targetExe
}