Last active
December 26, 2023 10:09
-
-
Save rjmholt/76692674c319e7acae2dd3b052c0d9da to your computer and use it in GitHub Desktop.
PowerShell script to build the PowerShell VSCode extension
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
# Copyright (c) Microsoft. All rights reserved. | |
# Licensed under the MIT license. | |
# Requires PowerShell, git and VSCode | |
param( | |
[switch]$NoPackage, | |
[switch]$LaunchVSCode | |
) | |
# Make sure we're using an up to date TLS protocol so our web requests succeed | |
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 | |
# Set up some strings we're going to use | |
$extRepoUrl = 'https://github.com/PowerShell/vscode-PowerShell' | |
$servicesRepoUrl = 'https://github.com/PowerShell/PowerShellEditorServices' | |
$baseDir = 'C:\Repos' | |
$extDir = "$baseDir\vscode-PowerShell" | |
$servicesDir = "$baseDir\PowerShellEditorServices" | |
$gitInstallUrl = 'https://github.com/git-for-windows/git/releases/download/v2.17.0.windows.1/Git-2.17.0-64-bit.exe' | |
$vscodeInstallUrl = 'https://go.microsoft.com/fwlink/?Linkid=852157' | |
$nodeMsiUrl = 'https://nodejs.org/dist/v8.11.1/node-v8.11.1-x64.msi' | |
$pkgMgmtMsi = 'https://download.microsoft.com/download/C/4/1/C41378D4-7F41-4BBE-9D0D-0E4F98585C61/PackageManagement_x64.msi' | |
$vsixPath = "$extDir\PowerShell-insiders.vsix" | |
function Update-Path | |
{ | |
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") | |
} | |
function Test-ExeExists | |
{ | |
param([string]$exeName) | |
where.exe $exeName 2>&1 | Out-Null | |
$? | |
} | |
function Install-Git | |
{ | |
Write-Verbose "Installing git..." | |
$gitExe = Join-Path $env:TEMP 'install-git.exe' | |
Invoke-RestMethod $gitInstallUrl -OutFile $gitExe | |
Start-Process -Wait $gitExe -ArgumentList "/silent" | |
Update-Path | |
} | |
function Install-VSCode | |
{ | |
Write-Verbose "Installing VSCode..." | |
$codeExe = Join-Path $env:TEMP 'install-code.exe' | |
Invoke-RestMethod $vscodeInstallUrl -OutFile $codeExe | |
Start-Process -Wait $codeExe -ArgumentList "/silent /mergetasks=!runcode" | |
Update-Path | |
} | |
function Install-NodeJS | |
{ | |
Write-Verbose "Downloading node..." | |
$nodeMsiPath = Join-Path $env:TEMP 'install-node.msi' | |
Invoke-RestMethod $nodeMsiUrl -OutFile $nodeMsiPath | |
Write-Verbose "Installing node..." | |
Start-Process msiexec.exe -Wait -ArgumentList "/I $nodeMsiPath /quiet" | |
Update-Path | |
} | |
function Test-HasInstallModule | |
{ | |
Get-Command "Install-Module" -ErrorAction SilentlyContinue; return $? | |
} | |
function Install-PowerShellGet | |
{ | |
Write-Verbose "Downloading PowerShellGet..." | |
$psGetMsiPath = Join-Path $env:TEMP "install-psget.msi" | |
Invoke-RestMethod $pkgMgmtMsi -OutFile $psGetMsiPath | |
Write-Verbose "Installing PowerShellGet..." | |
Start-Process msiexec.exe -Wait -ArgumentList "/I $pkgMgmtMsi /quiet" | |
Update-Path | |
} | |
function Restore-EditorServices | |
{ | |
Write-Verbose "Building EditorServices..." | |
git clone $servicesRepoUrl $servicesDir | |
Push-Location $servicesDir | |
Invoke-Build Build | |
Pop-Location | |
} | |
function Restore-CodeExtension | |
{ | |
Write-Verbose "Building VSCode extension..." | |
git clone $extRepoUrl $extDir | |
Push-Location $extDir | |
Invoke-Build Build | |
Pop-Location | |
} | |
function Install-PwshVsix | |
{ | |
Write-Verbose "Packaging the VSIX..." | |
Set-Location $extDir | |
Invoke-Build Package | |
Write-Verbose "Installing the VSIX..." | |
code.cmd --install-extension $vsixPath | |
} | |
# Make sure we're somewhere where we know where we are | |
if (-not (Test-Path $baseDir)) | |
{ | |
New-Item -Type Directory -Path $baseDir | |
} | |
Set-Location $baseDir | |
# Allow script execution | |
Set-ExecutionPolicy RemoteSigned | |
# Install git | |
if (-not (Test-ExeExists "git")) | |
{ | |
Install-Git | |
} | |
# Install VSCode | |
if (-not (Test-ExeExists "code")) | |
{ | |
Install-VSCode | |
} | |
# Install NodeJS | |
if (-not (Test-ExeExists "npm")) | |
{ | |
Install-NodeJS | |
} | |
# In older Windows versions, we may need to install PowerShellGet | |
if (-not Test-HasInstallModule) | |
{ | |
Install-PowerShellGet | |
} | |
# Install InvokeBuild | |
Write-Verbose "Installing InvokeBuild..." | |
Install-Module InvokeBuild -Scope CurrentUser -Force | |
# Build the backend first | |
Restore-EditorServices | |
# Now build the extension | |
Restore-CodeExtension | |
# Want to build the package by default | |
if ($NoPackage) | |
{ | |
if ($LaunchVSCode) | |
{ | |
Write-Verbose "Launching VSCode..." | |
Set-Location $extDir | |
code.cmd . | |
} | |
return | |
} | |
# Go to the VSCode extension folder, build the VSIX and install it | |
Install-PwshVsix | |
if ($LaunchVSCode) | |
{ | |
Write-Verbose "Launching VSCode..." | |
code.cmd . | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment