|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Installs pyenv-win |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + Installs pyenv-win to $HOME\.pyenv |
| 7 | + If pyenv-win is already installed, try to update to the latest version. |
| 8 | +
|
| 9 | + .PARAMETER Uninstall |
| 10 | + Uninstall pyenv-win. Note that this uninstalls any Python versions that were installed with pyenv-win. |
| 11 | +
|
| 12 | + .INPUTS |
| 13 | + None. |
| 14 | +
|
| 15 | + .OUTPUTS |
| 16 | + None. |
| 17 | +
|
| 18 | + .EXAMPLE |
| 19 | + PS> install-pyenv-win.ps1 |
| 20 | +
|
| 21 | + .LINK |
| 22 | + Online version: https://pyenv-win.github.io/pyenv-win/ |
| 23 | +#> |
| 24 | + |
| 25 | +param ( |
| 26 | + [Switch] $Uninstall = $False |
| 27 | +) |
| 28 | + |
| 29 | +$PyEnvDir = "${env:USERPROFILE}\.pyenv" |
| 30 | +$PyEnvWinDir = "${PyEnvDir}\pyenv-win" |
| 31 | +$BinPath = "${PyEnvWinDir}\bin" |
| 32 | +$ShimsPath = "${PyEnvWinDir}\shims" |
| 33 | + |
| 34 | +Function Remove-PyEnvVars() { |
| 35 | + $PathParts = [System.Environment]::GetEnvironmentVariable('PATH', "User") -Split ";" |
| 36 | + $NewPathParts = $PathParts.Where{ $_ -ne $BinPath }.Where{ $_ -ne $ShimsPath } |
| 37 | + $NewPath = $NewPathParts -Join ";" |
| 38 | + [System.Environment]::SetEnvironmentVariable('PATH', $NewPath, "User") |
| 39 | + |
| 40 | + [System.Environment]::SetEnvironmentVariable('PYENV', $null, "User") |
| 41 | + [System.Environment]::SetEnvironmentVariable('PYENV_ROOT', $null, "User") |
| 42 | + [System.Environment]::SetEnvironmentVariable('PYENV_HOME', $null, "User") |
| 43 | +} |
| 44 | + |
| 45 | +Function Remove-PyEnv() { |
| 46 | + Write-Host "Removing $PyEnvDir..." |
| 47 | + If (Test-Path $PyEnvDir) { |
| 48 | + Remove-Item -Path $PyEnvDir -Recurse |
| 49 | + } |
| 50 | + Write-Host "Removing environment variables..." |
| 51 | + Remove-PyEnvVars |
| 52 | +} |
| 53 | + |
| 54 | +Function Get-CurrentVersion() { |
| 55 | + $VersionFilePath = "$PyEnvDir\.version" |
| 56 | + If (Test-Path $VersionFilePath) { |
| 57 | + $CurrentVersion = Get-Content $VersionFilePath |
| 58 | + } |
| 59 | + Else { |
| 60 | + $CurrentVersion = "" |
| 61 | + } |
| 62 | + |
| 63 | + Return $CurrentVersion |
| 64 | +} |
| 65 | + |
| 66 | +Function Get-LatestVersion() { |
| 67 | + $LatestVersionFilePath = "$PyEnvDir\latest.version" |
| 68 | + (New-Object System.Net.WebClient).DownloadFile("https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/.version", $LatestVersionFilePath) |
| 69 | + $LatestVersion = Get-Content $LatestVersionFilePath |
| 70 | + |
| 71 | + Remove-Item -Path $LatestVersionFilePath |
| 72 | + |
| 73 | + Return $LatestVersion |
| 74 | +} |
| 75 | + |
| 76 | +Function Main() { |
| 77 | + If ($Uninstall) { |
| 78 | + Remove-PyEnv |
| 79 | + If ($? -eq $True) { |
| 80 | + Write-Host "pyenv-win successfully uninstalled." |
| 81 | + } |
| 82 | + Else { |
| 83 | + Write-Host "Uninstallation failed." |
| 84 | + } |
| 85 | + exit |
| 86 | + } |
| 87 | + |
| 88 | + $BackupDir = "${env:Temp}/pyenv-win-backup" |
| 89 | + |
| 90 | + $CurrentVersion = Get-CurrentVersion |
| 91 | + If ($CurrentVersion) { |
| 92 | + Write-Host "pyenv-win $CurrentVersion installed." |
| 93 | + $LatestVersion = Get-LatestVersion |
| 94 | + If ($CurrentVersion -eq $LatestVersion) { |
| 95 | + Write-Host "No updates available." |
| 96 | + exit |
| 97 | + } |
| 98 | + Else { |
| 99 | + Write-Host "New version available: $LatestVersion. Updating..." |
| 100 | + |
| 101 | + Write-Host "Backing up existing Python installations..." |
| 102 | + $FoldersToBackup = "install_cache", "versions", "shims" |
| 103 | + ForEach ($Dir in $FoldersToBackup) { |
| 104 | + If (-not (Test-Path $BackupDir)) { |
| 105 | + New-Item -ItemType Directory -Path $BackupDir |
| 106 | + } |
| 107 | + Move-Item -Path "${PyEnvWinDir}/${Dir}" -Destination $BackupDir |
| 108 | + } |
| 109 | + |
| 110 | + Write-Host "Removing $PyEnvDir..." |
| 111 | + Remove-Item -Path $PyEnvDir -Recurse |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + New-Item -Path $PyEnvDir -ItemType Directory |
| 116 | + |
| 117 | + $DownloadPath = "$PyEnvDir\pyenv-win.zip" |
| 118 | + |
| 119 | + (New-Object System.Net.WebClient).DownloadFile("https://github.com/pyenv-win/pyenv-win/archive/master.zip", $DownloadPath) |
| 120 | + |
| 121 | + Start-Process -FilePath "powershell.exe" -ArgumentList @( |
| 122 | + "-NoProfile", |
| 123 | + "-Command `"Microsoft.PowerShell.Archive\Expand-Archive -Path \`"$DownloadPath\`" -DestinationPath \`"$PyEnvDir\`"`"" |
| 124 | + ) -NoNewWindow -Wait |
| 125 | + |
| 126 | + Move-Item -Path "$PyEnvDir\pyenv-win-master\*" -Destination "$PyEnvDir" |
| 127 | + Remove-Item -Path "$PyEnvDir\pyenv-win-master" -Recurse |
| 128 | + Remove-Item -Path $DownloadPath |
| 129 | + |
| 130 | + # Update env vars |
| 131 | + [System.Environment]::SetEnvironmentVariable('PYENV', "${PyEnvWinDir}\", "User") |
| 132 | + [System.Environment]::SetEnvironmentVariable('PYENV_ROOT', "${PyEnvWinDir}\", "User") |
| 133 | + [System.Environment]::SetEnvironmentVariable('PYENV_HOME', "${PyEnvWinDir}\", "User") |
| 134 | + |
| 135 | + $PathParts = [System.Environment]::GetEnvironmentVariable('PATH', "User") -Split ";" |
| 136 | + |
| 137 | + # Remove existing paths, so we don't add duplicates |
| 138 | + $NewPathParts = $PathParts.Where{ $_ -ne $BinPath }.Where{ $_ -ne $ShimsPath } |
| 139 | + $NewPathParts = ($BinPath, $ShimsPath) + $NewPathParts |
| 140 | + $NewPath = $NewPathParts -Join ";" |
| 141 | + [System.Environment]::SetEnvironmentVariable('PATH', $NewPath, "User") |
| 142 | + |
| 143 | + If (Test-Path $BackupDir) { |
| 144 | + Write-Host "Restoring Python installations..." |
| 145 | + Move-Item -Path "$BackupDir/*" -Destination $PyEnvWinDir |
| 146 | + } |
| 147 | + |
| 148 | + If ($? -eq $True) { |
| 149 | + Write-Host "pyenv-win is successfully installed. You may need to close and reopen your terminal before using it." |
| 150 | + } |
| 151 | + Else { |
| 152 | + Write-Host "pyenv-win was not installed successfully. If this issue persists, please open a ticket: https://github.com/pyenv-win/pyenv-win/issues." |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +Main |
0 commit comments