Last active
October 8, 2024 00:59
-
-
Save da-moon/bd9dd0f9f5164f06108727fa35bfe644 to your computer and use it in GitHub Desktop.
Powershell script to build and setup latest version of zed on windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Zed Build and Update Script for Windows | |
# This script builds or updates Zed from source and sets up a command-line interface. | |
# Enable strict mode | |
Set-StrictMode -Version Latest | |
$ErrorActionPreference = "Stop" | |
# Configuration | |
$ZedRepo = "https://github.com/zed-industries/zed.git" | |
$BuildDir = "$env:USERPROFILE\zed-build" | |
$InstallDir = "$env:LOCALAPPDATA\Programs\Zed" | |
# Function to get the latest release version from GitHub | |
function Get-LatestRelease { | |
param ( | |
[switch]$Preview | |
) | |
$releasesUrl = "https://api.github.com/repos/zed-industries/zed/releases" | |
$releases = Invoke-RestMethod -Uri $releasesUrl -Method Get | |
if ($Preview) { | |
$latestRelease = $releases | Where-Object { $_.prerelease -eq $true } | Select-Object -First 1 | |
} else { | |
$latestRelease = $releases | Where-Object { $_.prerelease -eq $false } | Select-Object -First 1 | |
} | |
return $latestRelease.tag_name | |
} | |
# Function to compare versions | |
function Compare-Versions { | |
param ( | |
[string]$Version1, | |
[string]$Version2 | |
) | |
$v1 = $Version1 -replace '^v', '' -replace '-pre$', '' | |
$v2 = $Version2 -replace '^v', '' -replace '-pre$', '' | |
$v1Parts = $v1.Split('.') | ForEach-Object { [int]$_ } | |
$v2Parts = $v2.Split('.') | ForEach-Object { [int]$_ } | |
for ($i = 0; $i -lt 3; $i++) { | |
if ($v1Parts[$i] -gt $v2Parts[$i]) { | |
return 1 | |
} | |
if ($v1Parts[$i] -lt $v2Parts[$i]) { | |
return -1 | |
} | |
} | |
return 0 | |
} | |
# Function to detect architecture | |
function Get-Architecture { | |
$arch = (Get-WmiObject Win32_OperatingSystem).OSArchitecture | |
if ($arch -like "*64*") { | |
if ([Environment]::Is64BitProcess) { | |
return "x86_64-pc-windows-msvc" | |
} else { | |
return "aarch64-pc-windows-msvc" | |
} | |
} else { | |
return "i686-pc-windows-msvc" | |
} | |
} | |
# Check current installation | |
$currentVersion = $null | |
if (Test-Path "$InstallDir\version.txt") { | |
$currentVersion = Get-Content "$InstallDir\version.txt" | |
Write-Host "Current installed version: $currentVersion" | |
} else { | |
Write-Host "Zed is not currently installed." | |
} | |
# Determine channel and get latest version | |
$isPreview = $env:ZED_CHANNEL -eq "preview" | |
$latestVersion = Get-LatestRelease -Preview:$isPreview | |
Write-Host "Latest $(if ($isPreview) { 'preview' } else { 'stable' }) version: $latestVersion" | |
# Determine if update is needed | |
$updateNeeded = $false | |
if ($null -eq $currentVersion) { | |
$updateNeeded = $true | |
} else { | |
$comparison = Compare-Versions -Version1 $latestVersion -Version2 $currentVersion | |
$updateNeeded = $comparison -gt 0 | |
} | |
if (-not $updateNeeded) { | |
Write-Host "Zed is already up to date." | |
exit 0 | |
} | |
Write-Host "Updating Zed to version: $latestVersion" | |
function Check-Command($cmdname) { | |
return [bool](Get-Command -Name $cmdname -ErrorAction SilentlyContinue) | |
} | |
function Install-Rust { | |
Write-Host "Installing Rust..." | |
Invoke-WebRequest -Uri 'https://win.rustup.rs/' -OutFile 'rustup-init.exe' | |
Start-Process -FilePath 'rustup-init.exe' -ArgumentList '-y' -Wait | |
Remove-Item 'rustup-init.exe' | |
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") | |
} | |
function Install-VisualStudio { | |
Write-Host "Visual Studio build tools are required. Please install them manually from:" | |
Write-Host "https://visualstudio.microsoft.com/visual-cpp-build-tools/" | |
Write-Host "Ensure you select 'Desktop development with C++' workload." | |
Read-Host "Press Enter to continue once you've installed Visual Studio build tools" | |
} | |
function Install-WindowsSDK { | |
Write-Host "Windows 10 SDK is required. Please install it manually from:" | |
Write-Host "https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk/" | |
Read-Host "Press Enter to continue once you've installed Windows 10 SDK" | |
} | |
# Check and install dependencies | |
if (-not (Check-Command "rustc")) { | |
Install-Rust | |
} | |
if (-not (Test-Path "C:\Program Files (x86)\Microsoft Visual Studio")) { | |
Install-VisualStudio | |
} | |
if (-not (Test-Path "C:\Program Files (x86)\Windows Kits\10")) { | |
Install-WindowsSDK | |
} | |
# Update Rust | |
rustup update | |
# Detect architecture | |
$arch = Get-Architecture | |
Write-Host "Detected architecture: $arch" | |
# Clone or update Zed repository | |
if (-not (Test-Path $BuildDir)) { | |
New-Item -ItemType Directory -Force -Path $BuildDir | Out-Null | |
Set-Location $BuildDir | |
git clone $ZedRepo . | |
} else { | |
Set-Location $BuildDir | |
git fetch --all | |
} | |
git checkout $latestVersion | |
# Build Zed | |
Write-Host "Building Zed... This may take a while." | |
$env:RUSTFLAGS = "-C target-feature=+crt-static" | |
cargo build --release --target $arch | |
# Create installation directory | |
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null | |
# Copy built files | |
Copy-Item "target\$arch\release\zed.exe" -Destination $InstallDir | |
# Create a version file | |
$latestVersion | Out-File -FilePath "$InstallDir\version.txt" | |
# Function to create a shim for Zed | |
function Create-ZedShim { | |
$shimPath = "$env:LOCALAPPDATA\Microsoft\WindowsApps\zed.ps1" | |
$zedPath = "$InstallDir\zed.exe" | |
$shimContent = @" | |
#!/usr/bin/env pwsh | |
`$zedPath = "$zedPath" | |
if (-not (Test-Path `$zedPath)) { | |
Write-Error "Zed executable not found at `$zedPath. Please ensure Zed is installed correctly." | |
exit 1 | |
} | |
`$psi = New-Object System.Diagnostics.ProcessStartInfo | |
`$psi.FileName = `$zedPath | |
`$psi.WorkingDirectory = (Get-Location).Path | |
if (`$args.Count -eq 0 -or `$args[0] -notmatch '^(-|/)') { | |
# No arguments or first argument is not a flag | |
`$psi.UseShellExecute = `$true | |
`$psi.CreateNoWindow = `$false | |
# Handle file arguments | |
`$fileArgs = @() | |
foreach (`$arg in `$args) { | |
if (Test-Path `$arg -PathType Leaf) { | |
`$fileArgs += (Resolve-Path `$arg).Path | |
} elseif (`$arg -notmatch '^(-|/)') { | |
# Treat as a new file in the current directory | |
`$fileArgs += Join-Path `$psi.WorkingDirectory `$arg | |
} else { | |
`$fileArgs += `$arg | |
} | |
} | |
try { | |
Start-Process -FilePath `$zedPath -ArgumentList `$fileArgs -WorkingDirectory `$psi.WorkingDirectory | |
} | |
catch { | |
Write-Error "Failed to start Zed: `$_" | |
exit 1 | |
} | |
} | |
else { | |
# Handle command-line flags | |
`$psi.UseShellExecute = `$false | |
`$psi.RedirectStandardOutput = `$true | |
`$psi.RedirectStandardError = `$true | |
`$psi.Arguments = `$args | |
try { | |
`$process = [System.Diagnostics.Process]::Start(`$psi) | |
`$output = `$process.StandardOutput.ReadToEnd() | |
`$error_output = `$process.StandardError.ReadToEnd() | |
`$process.WaitForExit() | |
if (`$output) { Write-Output `$output } | |
if (`$error_output) { Write-Error `$error_output } | |
exit `$process.ExitCode | |
} | |
catch { | |
Write-Error "Failed to execute Zed command: `$_" | |
exit 1 | |
} | |
} | |
"@ | |
$shimContent | Out-File -FilePath $shimPath -Encoding utf8 -Force | |
Write-Host "Created Zed shim at $shimPath" | |
} | |
# Create a shortcut on the desktop | |
$WshShell = New-Object -comObject WScript.Shell | |
$Shortcut = $WshShell.CreateShortcut("$env:USERPROFILE\Desktop\Zed.lnk") | |
$Shortcut.TargetPath = "$InstallDir\zed.exe" | |
$Shortcut.Save() | |
# Create Zed shim | |
Create-ZedShim | |
# Add Zed to PATH | |
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User") | |
if ($UserPath -notlike "*$InstallDir*") { | |
[Environment]::SetEnvironmentVariable("Path", $UserPath + ";$InstallDir", "User") | |
$env:Path += ";$InstallDir" | |
} | |
# Function to run Zed with arguments | |
function Run-Zed { | |
param ( | |
[string]$Arguments = "" | |
) | |
$zedPath = "$InstallDir\zed.exe" | |
if (Test-Path $zedPath) { | |
$psi = New-Object System.Diagnostics.ProcessStartInfo | |
$psi.FileName = $zedPath | |
$psi.Arguments = $Arguments | |
$psi.UseShellExecute = $false | |
$psi.RedirectStandardOutput = $true | |
$psi.RedirectStandardError = $true | |
$process = [System.Diagnostics.Process]::Start($psi) | |
$output = $process.StandardOutput.ReadToEnd() | |
$error_output = $process.StandardError.ReadToEnd() | |
$process.WaitForExit() | |
if ($output) { Write-Output $output } | |
if ($error_output) { Write-Error $error_output } | |
return $process.ExitCode | |
} else { | |
Write-Error "Zed executable not found at $zedPath" | |
return 1 | |
} | |
} | |
# Run diagnostics | |
Write-Host "`nRunning Zed diagnostics:" | |
Run-Zed "--help" | |
# Check for version information in the binary | |
$versionInfo = (Get-Item "$InstallDir\zed.exe").VersionInfo | |
if ($versionInfo.FileVersion) { | |
Write-Host "Zed file version: $($versionInfo.FileVersion)" | |
} else { | |
Write-Host "No version information found in the Zed executable." | |
} | |
Write-Host "`nAttempting to launch Zed GUI:" | |
Start-Process "$InstallDir\zed.exe" | |
Start-Sleep -Seconds 5 # Wait for 5 seconds | |
$zedProcess = Get-Process zed -ErrorAction SilentlyContinue | |
if ($zedProcess) { | |
Write-Host "Zed process is running with PID: $($zedProcess.Id)" | |
} else { | |
Write-Host "No Zed process found running." | |
} | |
Write-Host "`nZed installation and diagnostics complete." | |
Write-Host "You can now run Zed from the command line using 'zed' command." | |
Write-Host "For example, try 'zed --help'" | |
Write-Host "To launch the Zed GUI, simply run 'zed' without arguments." | |
# Add a note about the version discrepancy | |
Write-Host "`nNote: There may be a discrepancy between the Linux and Windows versions of Zed regarding the --version flag." | |
Write-Host "This might be due to differences in how the build process handles version information on different platforms." | |
Write-Host "You may want to report this issue to the Zed developers for further investigation." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment