# ============================================================================== # G-Launcher Auto-Updater & Environment Bootstrap Script # Architecture: Download to Temp -> Build in Temp -> Replace Old -> Delete Temp # ============================================================================== param( [string]$Branch = "Dev", [string]$RepoUrl = "https://gaytea.cod-algieba.ts.net/GaCy/G-Launcher.git", [switch]$NoLaunch ) $ErrorActionPreference = "Stop" [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $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 Write-Host "==========================================================" -ForegroundColor Cyan Write-Host "" function Download-And-Extract { param( [string]$Url, [string]$ZipName, [string]$DestinationDir, [string]$Description ) Write-Host ">> Downloading $Description..." -ForegroundColor Yellow $zipPath = Join-Path $toolsDir $ZipName if (Get-Command curl.exe -ErrorAction SilentlyContinue) { & curl.exe -L --fail --silent --show-error -o $zipPath $Url } else { (New-Object System.Net.WebClient).DownloadFile($Url, $zipPath) } Write-Host ">> Extracting $Description..." -ForegroundColor Yellow if (!(Test-Path $DestinationDir)) { New-Item -ItemType Directory -Path $DestinationDir -Force | Out-Null } Expand-Archive -LiteralPath $zipPath -DestinationPath $DestinationDir -Force Remove-Item -Path $zipPath -Force -ErrorAction SilentlyContinue Write-Host "[OK] $Description installed successfully!" -ForegroundColor Green } # ------------------------------------------------------------------------------ # 1. Verify Git (or portable MinGit) # ------------------------------------------------------------------------------ Write-Host "[1/6] Checking Git..." -ForegroundColor White $gitCmd = Get-Command git -ErrorAction SilentlyContinue if (-not $gitCmd) { $minGitPath = Join-Path $toolsDir "mingit\cmd" if (Test-Path (Join-Path $minGitPath "git.exe")) { $env:PATH = "$minGitPath;" + $env:PATH } else { $minGitUrl = "https://github.com/git-for-windows/git/releases/download/v2.48.1.windows.1/MinGit-2.48.1-64-bit.zip" Download-And-Extract -Url $minGitUrl -ZipName "mingit.zip" -DestinationDir (Join-Path $toolsDir "mingit") -Description "Portable Git (MinGit)" $env:PATH = "$minGitPath;" + $env:PATH } } $gitVersion = (& git --version) Write-Host " -> Found: $gitVersion" -ForegroundColor Gray # ------------------------------------------------------------------------------ # 2. Verify Go Compiler (or portable Go) # ------------------------------------------------------------------------------ Write-Host "[2/6] Checking Go Compiler..." -ForegroundColor White $goCmd = Get-Command go -ErrorAction SilentlyContinue if (-not $goCmd) { $goBinPath = Join-Path $toolsDir "go\bin" if (Test-Path (Join-Path $goBinPath "go.exe")) { $env:PATH = "$goBinPath;" + $env:PATH $env:GOROOT = Join-Path $toolsDir "go" } else { $goUrl = "https://go.dev/dl/go1.24.0.windows-amd64.zip" Download-And-Extract -Url $goUrl -ZipName "go.zip" -DestinationDir $toolsDir -Description "Portable Go 1.24" $env:PATH = "$goBinPath;" + $env:PATH $env:GOROOT = Join-Path $toolsDir "go" } } $goVersion = (& go version) Write-Host " -> Found: $goVersion" -ForegroundColor Gray # ------------------------------------------------------------------------------ # 3. Verify Node.js & npm (or portable Node.js) # ------------------------------------------------------------------------------ Write-Host "[3/6] Checking Node.js & npm..." -ForegroundColor White $nodeCmd = Get-Command node -ErrorAction SilentlyContinue if (-not $nodeCmd) { $nodePath = Join-Path $toolsDir "node" if (Test-Path (Join-Path $nodePath "node.exe")) { $env:PATH = "$nodePath;" + $env:PATH } else { $nodeUrl = "https://nodejs.org/dist/v22.14.0/node-v22.14.0-win-x64.zip" $tempNodeExtract = Join-Path $toolsDir "node_temp" Download-And-Extract -Url $nodeUrl -ZipName "node.zip" -DestinationDir $tempNodeExtract -Description "Portable Node.js v22" $extractedDir = Get-ChildItem -Path $tempNodeExtract | Where-Object { $_.PSIsContainer } | Select-Object -First 1 if ($extractedDir) { Move-Item -Path $extractedDir.FullName -Destination $nodePath -Force Remove-Item -Path $tempNodeExtract -Recurse -Force -ErrorAction SilentlyContinue } $env:PATH = "$nodePath;" + $env:PATH } } $nodeVersion = (& node -v) $npmVersion = (& npm -v) Write-Host " -> Found: Node.js $nodeVersion (npm v$npmVersion)" -ForegroundColor Gray # ------------------------------------------------------------------------------ # 4. Verify Wails v3 CLI # ------------------------------------------------------------------------------ Write-Host "[4/6] Checking Wails v3 CLI..." -ForegroundColor White $gopath = (& go env GOPATH).Trim() $wailsBin = Join-Path $gopath "bin" $env:PATH = "$wailsBin;" + $env:PATH $wailsCmd = Get-Command wails3 -ErrorAction SilentlyContinue if (-not $wailsCmd) { Write-Host ">> Installing Wails v3 CLI..." -ForegroundColor Yellow & go install github.com/wailsapp/wails/v3/cmd/wails3@v3.0.0-alpha.74 } Write-Host " -> Wails v3 CLI is ready" -ForegroundColor Gray # ------------------------------------------------------------------------------ # 5. Download to Temp -> Build in Temp -> Replace Old -> Delete Temp # ------------------------------------------------------------------------------ $tempBuildDir = Join-Path $rootDir "temp\build_workspace" $tempExe = Join-Path $tempBuildDir "bin\G-Launcher.exe" try { Write-Host "[5/6] Preparing clean temporary workspace..." -ForegroundColor White if (Test-Path $tempBuildDir) { Remove-Item -Path $tempBuildDir -Recurse -Force -ErrorAction SilentlyContinue } New-Item -ItemType Directory -Path $tempBuildDir -Force | Out-Null 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 (-not $cloned -and (Test-Path (Join-Path $rootDir ".git"))) { & git clone --depth 1 --branch $Branch $rootDir $tempBuildDir $cloned = $true } 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 "" } 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 } } if (-not $NoLaunch) { Write-Host "🎮 Launching G-Launcher..." -ForegroundColor Cyan Start-Process -FilePath $targetExe }