|
3 | 3 |
|
4 | 4 | $ErrorActionPreference = "Stop" |
5 | 5 |
|
| 6 | +# Ensure TLS 1.2 for GitHub API/downloads (older Windows defaults to TLS 1.0) |
| 7 | +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 |
| 8 | + |
6 | 9 | # Configuration |
7 | | -$GOENV_ROOT = if ($env:GOENV_ROOT) { $env:GOENV_ROOT } else { "$HOME\.goenv" } |
| 10 | +$GOENV_ROOT = if ($env:GOENV_ROOT) { $env:GOENV_ROOT } else { Join-Path ($env:USERPROFILE ?? $HOME) ".goenv" } |
8 | 11 | $GITHUB_REPO = "go-nv/goenv" |
9 | | -$INSTALL_DIR = "$GOENV_ROOT\bin" |
| 12 | +$INSTALL_DIR = Join-Path $GOENV_ROOT "bin" |
10 | 13 |
|
11 | | -# Colors |
12 | | -function Write-ColorOutput($ForegroundColor) { |
13 | | - $fc = $host.UI.RawUI.ForegroundColor |
14 | | - $host.UI.RawUI.ForegroundColor = $ForegroundColor |
15 | | - if ($args) { |
16 | | - Write-Output $args |
17 | | - } |
18 | | - $host.UI.RawUI.ForegroundColor = $fc |
| 14 | +# Colors — use Write-Host which works in non-interactive/piped contexts |
| 15 | +function Write-ColorOutput { |
| 16 | + param( |
| 17 | + [Parameter(Position=0)] |
| 18 | + [System.ConsoleColor]$ForegroundColor, |
| 19 | + [Parameter(Position=1, ValueFromRemainingArguments)] |
| 20 | + [string[]]$Message |
| 21 | + ) |
| 22 | + Write-Host ($Message -join ' ') -ForegroundColor $ForegroundColor |
19 | 23 | } |
20 | 24 |
|
21 | 25 | # Detect architecture |
@@ -85,34 +89,84 @@ function Install-Binary { |
85 | 89 | # Copy binary |
86 | 90 | $binaryPath = Join-Path $tmpDir "goenv.exe" |
87 | 91 | if (Test-Path $binaryPath) { |
88 | | - Copy-Item -Path $binaryPath -Destination "$INSTALL_DIR\goenv.exe" -Force |
| 92 | + Copy-Item -Path $binaryPath -Destination (Join-Path $INSTALL_DIR "goenv.exe") -Force |
89 | 93 | } else { |
90 | 94 | throw "Binary not found in archive" |
91 | 95 | } |
92 | 96 |
|
93 | 97 | # Copy completions if they exist |
94 | 98 | $completionsPath = Join-Path $tmpDir "completions" |
95 | 99 | if (Test-Path $completionsPath) { |
96 | | - $targetCompletions = "$GOENV_ROOT\completions" |
| 100 | + $targetCompletions = Join-Path $GOENV_ROOT "completions" |
97 | 101 | New-Item -ItemType Directory -Path $targetCompletions -Force | Out-Null |
98 | 102 | Copy-Item -Path "$completionsPath\*" -Destination $targetCompletions -Recurse -Force -ErrorAction SilentlyContinue |
99 | 103 | } |
100 | 104 |
|
101 | 105 | Write-ColorOutput Green "goenv installed successfully!" |
| 106 | + |
| 107 | + # Remove stale goenv shim from v2 installations. |
| 108 | + # v2's goenv-rehash bakes the Cellar/libexec path into shims at creation time. |
| 109 | + # Only remove if it contains "libexec/goenv" — the v2 fingerprint. |
| 110 | + $staleShim = Join-Path $GOENV_ROOT "shims" "goenv" |
| 111 | + if (Test-Path $staleShim) { |
| 112 | + $shimContent = Get-Content $staleShim -Raw -ErrorAction SilentlyContinue |
| 113 | + if ($shimContent -and $shimContent -match "libexec/goenv") { |
| 114 | + Write-ColorOutput Yellow "Removing stale v2 goenv shim..." |
| 115 | + Remove-Item -Path $staleShim -Force -ErrorAction SilentlyContinue |
| 116 | + Write-ColorOutput Green "Stale shim removed" |
| 117 | + } |
| 118 | + } |
102 | 119 | } |
103 | 120 | catch { |
104 | 121 | Write-ColorOutput Red "Installation failed: $_" |
105 | 122 | exit 1 |
106 | 123 | } |
107 | 124 | finally { |
108 | | - # Cleanup |
| 125 | + # Cleanup temp directory |
109 | 126 | if (Test-Path $tmpDir) { |
110 | 127 | Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue |
111 | 128 | } |
112 | 129 | } |
113 | 130 | } |
114 | 131 |
|
115 | | -# Print setup instructions |
| 132 | +# Auto-configure PowerShell profile |
| 133 | +function Initialize-PowerShellProfile { |
| 134 | + $profilePath = $PROFILE |
| 135 | + |
| 136 | + # Create profile directory if it doesn't exist |
| 137 | + $profileDir = Split-Path -Parent $profilePath |
| 138 | + if (-not (Test-Path $profileDir)) { |
| 139 | + New-Item -ItemType Directory -Path $profileDir -Force | Out-Null |
| 140 | + } |
| 141 | + |
| 142 | + # Create profile file if it doesn't exist |
| 143 | + if (-not (Test-Path $profilePath)) { |
| 144 | + New-Item -ItemType File -Path $profilePath -Force | Out-Null |
| 145 | + } |
| 146 | + |
| 147 | + # Check if goenv is already configured |
| 148 | + $profileContent = Get-Content $profilePath -Raw -ErrorAction SilentlyContinue |
| 149 | + if ($profileContent -and $profileContent -match "goenv init") { |
| 150 | + Write-ColorOutput Green "goenv is already configured in $profilePath" |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + # Add goenv configuration with comment marker |
| 155 | + Write-ColorOutput Yellow "Adding goenv configuration to $profilePath..." |
| 156 | + |
| 157 | + $goenvConfig = @" |
| 158 | +
|
| 159 | +# goenv - Go version manager (auto-configured by installer) |
| 160 | +`$env:GOENV_ROOT = "`$HOME\.goenv" |
| 161 | +`$env:PATH = "`$env:GOENV_ROOT\bin;`$env:PATH" |
| 162 | +& goenv init - | Invoke-Expression |
| 163 | +"@ |
| 164 | + |
| 165 | + Add-Content -Path $profilePath -Value $goenvConfig |
| 166 | + Write-ColorOutput Green "PowerShell profile configured successfully!" |
| 167 | +} |
| 168 | + |
| 169 | +# Print setup completion message |
116 | 170 | function Show-Instructions { |
117 | 171 | $profilePath = $PROFILE |
118 | 172 |
|
@@ -158,6 +212,7 @@ function Main { |
158 | 212 |
|
159 | 213 | $version = Get-LatestVersion |
160 | 214 | Install-Binary -Version $version -Arch $arch |
| 215 | + Initialize-PowerShellProfile |
161 | 216 | Show-Instructions |
162 | 217 | } |
163 | 218 |
|
|
0 commit comments