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