|
| 1 | +# This script is meant to be sourced with ROOTDIR set. |
| 2 | + |
| 3 | +if (-not $env:ROOTDIR) { |
| 4 | + Write-Error 'ensure-go-installed.ps1 invoked without ROOTDIR set!' |
| 5 | +} |
| 6 | + |
| 7 | +# Function to check if Go is installed and at least version 1.21 |
| 8 | +function GoOk { |
| 9 | + $goVersionOutput = & go version 2>$null |
| 10 | + if ($goVersionOutput) { |
| 11 | + $goVersion = $goVersionOutput -match 'go(\d+)\.(\d+)' | Out-Null |
| 12 | + $majorVersion = [int]$Matches[1] |
| 13 | + $minorVersion = [int]$Matches[2] |
| 14 | + return ($majorVersion -eq 1 -and $minorVersion -ge 21) |
| 15 | + } |
| 16 | + return $false |
| 17 | +} |
| 18 | + |
| 19 | +# Function to set up a local Go installation if available |
| 20 | +function SetUpVendoredGo { |
| 21 | + $GO_VERSION = "go1.23.7" |
| 22 | + $VENDORED_GOROOT = Join-Path -Path $env:ROOTDIR -ChildPath "vendor/$GO_VERSION/go" |
| 23 | + if (Test-Path -Path "$VENDORED_GOROOT/bin/go") { |
| 24 | + $env:GOROOT = $VENDORED_GOROOT |
| 25 | + $env:PATH = "$env:GOROOT/bin;$env:PATH" |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +# Function to check if Make is installed and install it if needed |
| 30 | +function EnsureMakeInstalled { |
| 31 | + $makeInstalled = $null -ne (Get-Command "make" -ErrorAction SilentlyContinue) |
| 32 | + if (-not $makeInstalled) { |
| 33 | + #Write-Host "Installing Make using winget..." |
| 34 | + winget install --no-upgrade --nowarn -e --id GnuWin32.Make |
| 35 | + if ($LASTEXITCODE -ne 0 -and $LASTEXITCODE -ne 0x8A150061) { |
| 36 | + Write-Error "Failed to install Make. Please install it manually. Exit code: $LASTEXITCODE" |
| 37 | + } |
| 38 | + # Refresh PATH to include the newly installed Make |
| 39 | + $env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("PATH", "User") |
| 40 | + } |
| 41 | + |
| 42 | + # Add GnuWin32 bin directory directly to the PATH |
| 43 | + $gnuWin32Path = "C:\Program Files (x86)\GnuWin32\bin" |
| 44 | + if (Test-Path -Path $gnuWin32Path) { |
| 45 | + $env:PATH = "$gnuWin32Path;$env:PATH" |
| 46 | + } else { |
| 47 | + Write-Host "Couldn't find GnuWin32 bin directory at the expected location." |
| 48 | + # Also refresh PATH from environment variables as a fallback |
| 49 | + $env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("PATH", "User") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +SetUpVendoredGo |
| 54 | + |
| 55 | +if (-not (GoOk)) { |
| 56 | + & ./script/install-vendored-go >$null |
| 57 | + if ($LASTEXITCODE -ne 0) { |
| 58 | + exit 1 |
| 59 | + } |
| 60 | + SetUpVendoredGo |
| 61 | +} |
| 62 | + |
| 63 | +# Ensure Make is installed |
| 64 | +EnsureMakeInstalled |
0 commit comments