Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add powershell install script #56

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ Now add this line to your `~/.bashrc`, `~/.profile`, or `~/.zshrc` file.
export PATH="$HOME/.zm/current:$PATH"
```

### Windows

#### PowerShell

```ps1
irm https://raw.githubusercontent.com/hendriknielaender/zvm/master/install.ps1 | iex
```

#### Command Prompt

```cmd
powershell -c "irm https://raw.githubusercontent.com/hendriknielaender/zvm/master/install.ps1 | iex"
```

## Usage
```bash
zvm list # List all available Zig versions
Expand Down
76 changes: 76 additions & 0 deletions install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,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."
Loading