Powershellize the windows install-scripts

- Replace powershell aliases with canonical Cmdlet names
- Update function names to only use approved Verbs
This commit is contained in:
John 2022-04-30 13:01:04 -05:00
parent 971e6fe2fb
commit f86f2cb6b9
2 changed files with 55 additions and 55 deletions

View File

@ -66,9 +66,9 @@ if ($h -or $Help) {
} }
# .description # .description
# Command-Available: Checks whether a given command is available. # Get-CommandAvailable: Checks whether a given command is available.
# If command is available, returns $false # If command is available, returns $false
function Command-Available { function Get-CommandAvailable {
param ($command) param ($command)
# Use a wildcard here so the command doesn't throw an exception we'd have to trycatch # Use a wildcard here so the command doesn't throw an exception we'd have to trycatch
# It's not a filthy hack if it's elegant! # It's not a filthy hack if it's elegant!
@ -76,8 +76,8 @@ function Command-Available {
} }
#.description #.description
# Dry-Run a powershell statement # Invoke-DryRun a powershell statement
function Dry-Run { function Invoke-DryRun {
param ([string] $command) param ([string] $command)
$prompt = "> " $prompt = "> "
if ($DryRun) { if ($DryRun) {
@ -90,8 +90,8 @@ function Dry-Run {
} }
#.description #.description
# Reload-Path: Reload the Path environment variable # Reset-Path: Reload the Path environment variable
function Reload-Path { function Reset-Path {
Write-Output "Reloading Path..." Write-Output "Reloading Path..."
#* Courtesy of user [mpen](https://stackoverflow.com/users/65387/mpen) on [StackOverflow](https://stackoverflow.com/a/31845512) #* Courtesy of user [mpen](https://stackoverflow.com/users/65387/mpen) on [StackOverflow](https://stackoverflow.com/a/31845512)
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
@ -99,7 +99,7 @@ function Reload-Path {
# Check if Winget is available # Check if Winget is available
if ( -not (Command-Available winget) ) { if ( -not (Get-CommandAvailable winget) ) {
Write-Warning "It looks like winget isn't available.`n" Write-Warning "It looks like winget isn't available.`n"
Write-Host "Update 'App Installer' through the Microsoft Store, or grab the '.msixbundle' from the winget-cli repository:" Write-Host "Update 'App Installer' through the Microsoft Store, or grab the '.msixbundle' from the winget-cli repository:"
Write-Host "( https://github.com/microsoft/winget-cli/releases/latest )`n" -ForegroundColor White Write-Host "( https://github.com/microsoft/winget-cli/releases/latest )`n" -ForegroundColor White
@ -118,7 +118,7 @@ if ( ([Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5
if (!$NoPrompt) { if (!$NoPrompt) {
for ( $i = 3; $i -gt 0; $i--) { for ( $i = 3; $i -gt 0; $i--) {
Write-Host "Press Ctrl+C to exit. Continuing in $i...`r" -NoNewLine Write-Host "Press Ctrl+C to exit. Continuing in $i...`r" -NoNewLine
sleep 1 Start-Sleep 1
} }
Write-Host "Press any key to continue... " Write-Host "Press any key to continue... "
[void][Console]::ReadKey(1) # Equivalent to Command Prompt's `pause` command [void][Console]::ReadKey(1) # Equivalent to Command Prompt's `pause` command
@ -130,11 +130,11 @@ if ( ([Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5
} }
# Install Git # Install Git
if ( -not (Command-Available git) ) { if ( -not (Get-CommandAvailable git) ) {
Write-Host "`nInstalling Git with winget..." Write-Host "`nInstalling Git with winget..."
Dry-Run 'winget install --id Git.Git' Invoke-DryRun 'winget install --id Git.Git'
Reload-Path Reset-Path
if ( -not (Command-Available git)) { if ( -not (Get-CommandAvailable git)) {
Throw "Git failed to install. Aborting." Throw "Git failed to install. Aborting."
} }
} else { } else {
@ -143,37 +143,37 @@ if ( -not (Command-Available git) ) {
# Create git directory in GitDir # Create git directory in GitDir
if ( -not (Test-Path "$GitDir") ) { if ( -not (Test-Path "$GitDir") ) {
Dry-Run "mkdir '$GitDir'" Invoke-DryRun "mkdir '$GitDir'"
} }
# Clone the repository in GitDir # Clone the repository in GitDir
$dir = $pwd $dir = $pwd
cd $GitDir Set-Location $GitDir
Dry-Run "git clone '$RepoURI'" Invoke-DryRun "git clone '$RepoURI'"
# TODO: Remove this when merging # TODO: Remove this when merging
cd Mind_reader Set-Location Mind_reader
Dry-Run "git checkout johnBreaux" Invoke-DryRun "git checkout johnBreaux"
cd .. Set-Location ..
# TODO: Remove this when merging # TODO: Remove this when merging
# Run the install script # Run the install script
if ( -not (Test-Path "$SetupPath")) { if ( -not (Test-Path "$SetupPath")) {
Throw "Repository contains no subdirectory '$SetupPath'." Throw "Repository contains no subdirectory '$SetupPath'."
} }
cd $SetupPath Set-Location $SetupPath
# Run upgrade-windows to install the rest of the dependency chain. # Run upgrade-windows to install the rest of the dependency chain.
$args = if ($AllowAdministrator) {" -AllowAdministrator"} else {""} $upgradeArgs = if ($AllowAdministrator) {" -AllowAdministrator"} else {""}
$args += if ($DryRun) {" -DryRun"} else {""} $upgradeArgs += if ($DryRun) {" -DryRun"} else {""}
PowerShell ("./upgrade-windows.ps1 -Install -NoPrompt" + $args) PowerShell ("./upgrade-windows.ps1 -Install -NoPrompt" + $upgradeArgs)
Reload-Path Reset-Path
# Open VSCode in the repository location # Open VSCode in the repository location
Write-Host "`nOpening Visual Studio Code" Write-Host "`nOpening Visual Studio Code"
cd $RepoPath Set-Location $RepoPath
Dry-Run "code ." Invoke-DryRun "code ."
cd $dir Set-Location $dir
if ( -not $NoPrompt ) { if ( -not $NoPrompt ) {
Write-Host "`nPress any key to exit."; [void][Console]::ReadKey(1) Write-Host "`nPress any key to exit."; [void][Console]::ReadKey(1)
} }

View File

@ -56,9 +56,9 @@ param (
) )
# .description # .description
# Command-Available: Checks whether a given command is available. # Get-CommandAvailable: Checks whether a given command is available.
# If command is available, returns $false # If command is available, returns $false
function Command-Available { function Get-CommandAvailable {
param ($command) param ($command)
# Use a wildcard here so the command doesn't throw an exception we'd have to trycatch # Use a wildcard here so the command doesn't throw an exception we'd have to trycatch
# It's not a filthy hack if it's elegant! # It's not a filthy hack if it's elegant!
@ -66,8 +66,8 @@ function Command-Available {
} }
#.description #.description
# Dry-Run a powershell statement # Invoke-Dryrun a powershell statement
function Dry-Run { function Invoke-Dryrun {
param ([string] $command) param ([string] $command)
$prompt = "> " $prompt = "> "
if ($DryRun) { if ($DryRun) {
@ -80,8 +80,8 @@ function Dry-Run {
} }
#.description #.description
# Reload-Path: Reload the Path environment variable # Reset-Path: Reload the Path environment variable
function Reload-Path { function Reset-Path {
Write-Output "Reloading Path..." Write-Output "Reloading Path..."
#* Courtesy of user [mpen](https://stackoverflow.com/users/65387/mpen) on [StackOverflow](https://stackoverflow.com/a/31845512) #* Courtesy of user [mpen](https://stackoverflow.com/users/65387/mpen) on [StackOverflow](https://stackoverflow.com/a/31845512)
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
@ -89,7 +89,7 @@ function Reload-Path {
# Check if Winget is available # Check if Winget is available
if ( -not (Command-Available winget) ) { if ( -not (Get-CommandAvailable winget) ) {
Write-Warning "It looks like winget isn't available.`n" Write-Warning "It looks like winget isn't available.`n"
Write-Host "Update 'App Installer' through the Microsoft Store, or grab the '.msixbundle' from the winget-cli repository:" Write-Host "Update 'App Installer' through the Microsoft Store, or grab the '.msixbundle' from the winget-cli repository:"
Write-Host "( https://github.com/microsoft/winget-cli/releases/latest )`n" -ForegroundColor White Write-Host "( https://github.com/microsoft/winget-cli/releases/latest )`n" -ForegroundColor White
@ -108,14 +108,14 @@ if ( ([Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5
Write-Warning "Script was run as Administrator. Exit now if you didn't mean to do this!" Write-Warning "Script was run as Administrator. Exit now if you didn't mean to do this!"
for ( $i = 3; $i -gt 0; $i--) { for ( $i = 3; $i -gt 0; $i--) {
Write-Host "Press Ctrl+C to exit. Continuing in $i...`r" -NoNewLine Write-Host "Press Ctrl+C to exit. Continuing in $i...`r" -NoNewLine
sleep 1 Start-Sleep 1
} }
Write-Host "Press any key to continue... " Write-Host "Press any key to continue... "
[void][Console]::ReadKey(1) # Equivalent to Command Prompt's `pause` command [void][Console]::ReadKey(1) # Equivalent to Command Prompt's `pause` command
} }
} else { } else {
# Throw a fatal error if the user tries to run as administrator. # Throw a fatal errorOccurred if the user tries to run as administrator.
Throw "Script must be run as a normal user." Throw "Script must be run as a normal user."
} }
} }
@ -123,24 +123,24 @@ if ( ([Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5
# Import the packages from dependencies.json (autogenerated file, do not edit!) # Import the packages from dependencies.json (autogenerated file, do not edit!)
if ( -not $NoWinget) { if ( -not $NoWinget) {
Write-Host "`nInstalling packages with winget..." Write-Host "`nInstalling packages with winget..."
Dry-Run 'winget install Microsoft.VisualStudio.2022.BuildTools --override "--wait --quiet --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"' Invoke-Dryrun 'winget install Microsoft.VisualStudio.2022.BuildTools --override "--wait --quiet --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"'
Dry-Run "winget import winget/dependencies.json" Invoke-Dryrun "winget import winget/dependencies.json"
# Reload the PATH, so we can use some of those sweet new commands we just installed # Reload the PATH, so we can use some of those sweet new commands we just installed
Reload-Path Reset-Path
} }
# Check whether everything is available now: # Check whether everything is available now:
$error = 0 $errorOccurred = 0
if ( -not (Command-Available code) ) { if ( -not (Get-CommandAvailable code) ) {
$error += 1; Write-Host -ForegroundColor red "Visual Studio Code not available" $errorOccurred += 1; Write-Host -ForegroundColor red "Visual Studio Code not available"
} }
if ( -not (Command-Available node) ) { if ( -not (Get-CommandAvailable node) ) {
$error += 2; Write-Host -ForegroundColor red "NodeJS not available" $errorOccurred += 2; Write-Host -ForegroundColor red "NodeJS not available"
} }
if ( -not (Command-Available npm ) ) { if ( -not (Get-CommandAvailable npm ) ) {
$error += 4; Write-Host -ForegroundColor red "Node Package Manager not available"; $errorOccurred += 4; Write-Host -ForegroundColor red "Node Package Manager not available";
} }
if ( $error ) { exit } if ( $errorOccurred ) { exit }
# .description # .description
# EnsureNodePackageInstalled: # EnsureNodePackageInstalled:
@ -151,11 +151,11 @@ function EnsureNodePackageInstalled {
param ( param (
[string[]]$command [string[]]$command
) )
if ( ($Install) -or -not (Command-Available $command[0]) ) { if ( ($Install) -or -not (Get-CommandAvailable $command[0]) ) {
Write-Host "`nInstalling $($command[0])..." Write-Host "`nInstalling $($command[0])..."
Dry-Run "npm install -g $([string]$command)" Invoke-Dryrun "npm install -g $([string]$command)"
Reload-Path Reset-Path
if ( -not (Command-Available $command[0])) { if ( -not (Get-CommandAvailable $command[0])) {
Throw "$command failed to install. Aborting." Throw "$command failed to install. Aborting."
} }
} else { } else {
@ -174,11 +174,11 @@ $prev_directory = $pwd
# install NodeJS dependencies for this extension # install NodeJS dependencies for this extension
Write-Host "`nInstalling NodeJS Dependencies..." Write-Host "`nInstalling NodeJS Dependencies..."
cd ..\.. Set-Location ..\..
Dry-Run "npm install" Invoke-Dryrun "npm install"
# Run npm audit fix to upgrade vulnerable dependencies, except breaking changes. # Run npm audit fix to upgrade vulnerable dependencies, except breaking changes.
Dry-Run "npm audit fix" Invoke-Dryrun "npm audit fix"
# if we're on a known VSCode version, go ahead and run electron-rebuild # if we're on a known VSCode version, go ahead and run electron-rebuild
switch -Regex (code --version) { switch -Regex (code --version) {
@ -187,7 +187,7 @@ switch -Regex (code --version) {
} #> } #>
"1\.66\.[0-9]+" { # 1.66 "1\.66\.[0-9]+" { # 1.66
Write-Host "`nRebuilding Electron for your version of VSCode..." Write-Host "`nRebuilding Electron for your version of VSCode..."
Dry-Run 'electron-rebuild --version="17.2.0"' Invoke-Dryrun 'electron-rebuild --version="17.2.0"'
Write-Host "Done!" -ForegroundColor green Write-Host "Done!" -ForegroundColor green
break break
} }
@ -207,7 +207,7 @@ switch -Regex (code --version) {
} }
# Return from whence we came # Return from whence we came
cd $prev_directory Set-Location $prev_directory
if ( -not $NoPrompt ) { if ( -not $NoPrompt ) {
Write-Host "`nPress any key to exit."; [void][Console]::ReadKey(1) Write-Host "`nPress any key to exit."; [void][Console]::ReadKey(1)
} }