Skip to content

Instantly share code, notes, and snippets.

@aldrichtr
Forked from JustinGrote/StartVSCodeServer.ps1
Created February 16, 2024 00:04
Show Gist options
  • Save aldrichtr/6c7a56e0a883674dac7f616f3efc0d93 to your computer and use it in GitHub Desktop.
Save aldrichtr/6c7a56e0a883674dac7f616f3efc0d93 to your computer and use it in GitHub Desktop.
Powershell Bootstrap script for VSCode Server
#require -version 5.1
#Usage: iwr https://tinyurl.com/VSCodeServer | iex
#Parameterized usage: & ([ScriptBlock]::Create((iwr https://tinyurl.com/VSCodeServer))) -Your -Options
param(
#Path to install the vscode binary to. This defaults to a folder in your Local AppData path. Must be writable by your current user without sudo
[string]$InstallPath,
#Installation architecture. This is normally autodetected.
$Arch,
#Installation platform. Normally autodetected.
$Target = 'unknown-linux-gnu',
#Path to find the vscode server launcher. You normally dont need to change.
$Uri = 'https://aka.ms/vscode-server-launcher',
#Normally this script will use the existing detected code server. Specify to force a new install.
$Force,
#This script typically starts code-server automatically upon run. Specify to prevent that behavior
$InstallOnly,
#This script updates the path to include code-server so it can be specified again. Specify to prevent that behavior
$NoUpdatePath
)
$ErrorActionPreference = 'Stop'
if (-not $InstallPath) {
$InstallPath = Join-Path -Path ([System.Environment]::GetFolderPath('LocalApplicationData')) -ChildPath 'vscode-server'
}
#Supports 5.1+
$thisIsWindows = if ($PSVersionTable.psVersion -lt '6.0.0') {$true;$isWinPS=$true} else {$isWindows}
if ($thisIsWindows) {
$Arch = switch ($ENV:PROCESSOR_ARCHITECTURE) {
'AMD64' {'x86_64'}
'ARM64' {'aarch64'}
default {throw [NotSupportedException]'Unknown Windows Architecture, this is either a bug or you are trying to install on an x86 machine'}
}
$Target = 'pc-windows-msvc'
} elseif ($isLinux -or $isMacOS) {
$Arch = (& uname -m) -replace 'arm64', 'aarch64'
if (-not $Arch) {
Write-Warning "Architecture not detected on Linux. Using generic binary. This is an uncommon issue."
$Arch = ''
}
if ($isMacOS) {
$Target = 'apple-darwin-signed'
}
} else {
throw [NotSupportedException]'This platform is not supported by this script.'
}
[Uri]$InstallUri = "$Uri/$Arch-$Target"
$OutBin = if ($thisIsWindows) {'code-server.exe'} else {'code-server'}
$OutBinPath = Join-Path -Path $InstallPath -ChildPath $OutBin
if (-not (Test-Path $OutBinPath) -or $Force) {
Write-Verbose "Downloading vscode $Target $Arch from $InstallUri"
#Prep the target folder if it doesn't exist and download the binary
New-Item -ItemType Directory -Force -Path $InstallPath | Out-Null
if ($isWinPS) {
#Improves download performance on 5.1
$lastProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
}
Invoke-WebRequest -UseBasicParsing -Uri $InstallUri -OutFile $OutBinPath
if (-not $thisIsWindows) {
& chmod +x $OutBinPath
}
if ($isWinPS) {
$ProgressPreference = $lastProgressPreference
}
Write-Verbose "Downloaded code server to $OutBinPath"
if (-not $NoUpdatePath) {
$Paths = [Environment]::GetEnvironmentVariable('Path', 'User') -split [io.path]::PathSeparator
if ($InstallPath -notin $Paths) {
Write-Verbose "PATH variable already contains $InstallPath, skipping..."
} else {
Write-Verbose "Updating PATH to include $InstallPath"
}
[string]$newPathVar = "$InstallPath" + [io.path]::PathSeparator + [Environment]::GetEnvironmentVariable('Path', 'User')
[Environment]::SetEnvironmentVariable('Path', $newPathVar, 'User')
}
} else {
Write-Verbose "Found existing code server at $OutBinPath"
}
if (-not $InstallOnly) {
Write-Verbose "Starting $OutBinPath in current path $PWD"
& $OutBinPath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment