-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.ps1
76 lines (67 loc) · 3 KB
/
install.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
# Define constants and variables
$zvmBaseUrl = "https://github.com/hendriknielaender/zvm/releases/latest/download"
$zvmInstallDir = "$HOME\.zm"
$architecture = if ([Environment]::Is64BitOperatingSystem) { "x86_64" } else { "x86" }
$zvmFileName = "$architecture-windows-zvm.zip"
$zvmExeFileName = "$architecture-windows-zvm.exe"
$zvmZipPath = "$zvmInstallDir\$zvmFileName"
$zvmExePath = "$zvmInstallDir\$zvmExeFileName"
$zvmRenamedExePath = "$zvmInstallDir\zvm.exe"
$zvmUrl = "$zvmBaseUrl/$zvmFileName"
# Create the installation directory if it doesn't exist
if (-not (Test-Path -Path $zvmInstallDir)) {
Write-Output "Creating installation directory at $zvmInstallDir..."
New-Item -Path $zvmInstallDir -ItemType Directory | Out-Null
}
# Download the latest release
Write-Output "Downloading zvm from $zvmUrl..."
try {
Invoke-WebRequest -Uri $zvmUrl -OutFile $zvmZipPath
Write-Output "Download complete."
} catch {
Write-Output "Error: Failed to download zvm. Please check your internet connection and URL."
exit 1
}
# Unzip the downloaded file
Write-Output "Extracting zvm..."
try {
Expand-Archive -Path $zvmZipPath -DestinationPath $zvmInstallDir -Force
Write-Output "Extraction complete."
} catch {
Write-Output "Error: Failed to extract $zvmFileName. Please check the file and try again."
Remove-Item -Path $zvmZipPath
exit 1
}
# Remove the downloaded zip file
Remove-Item -Path $zvmZipPath
# Check if the existing zvm.exe exists and remove it
if (Test-Path -Path $zvmRenamedExePath) {
Remove-Item -Path $zvmRenamedExePath -Force
Write-Output "Removed existing zvm.exe."
}
# Rename the new executable
if (Test-Path -Path $zvmExePath) {
Rename-Item -Path $zvmExePath -NewName "zvm.exe"
Write-Output "Renamed $zvmExeFileName to zvm.exe"
try {
# Set the user environment variable
Write-Output "Setting ZVM_HOME environment variable..."
[System.Environment]::SetEnvironmentVariable("ZVM_HOME", $zvmInstallDir, [System.EnvironmentVariableTarget]::User)
Write-Output "ZVM_HOME has been set to $zvmInstallDir for the current user."
# Add the zvm directory to the user PATH
$currentPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
if ($currentPath -notlike "*$zvmInstallDir*") {
$newPath = "$currentPath;$zvmInstallDir"
[System.Environment]::SetEnvironmentVariable("Path", $newPath, [System.EnvironmentVariableTarget]::User)
Write-Output "Added $zvmInstallDir to PATH for the current user."
} else {
Write-Output "$zvmInstallDir is already in the user PATH."
}
} catch {
Write-Output "Error: Unable to set environment variable or update PATH. Please run the script as an administrator."
}
} else {
Write-Output "Error: zvm executable not found after extraction. Please check the downloaded files."
exit 1
}
Write-Output "Installation complete. Please restart your terminal or computer to apply changes."