diff --git a/setup-development/windows/install-windows.ps1 b/setup-development/windows/install-windows.ps1 index ac864db..86c2d36 100644 --- a/setup-development/windows/install-windows.ps1 +++ b/setup-development/windows/install-windows.ps1 @@ -66,9 +66,9 @@ if ($h -or $Help) { } # .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 -function Command-Available { +function Get-CommandAvailable { param ($command) # 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! @@ -76,8 +76,8 @@ function Command-Available { } #.description -# Dry-Run a powershell statement -function Dry-Run { +# Invoke-DryRun a powershell statement +function Invoke-DryRun { param ([string] $command) $prompt = "> " if ($DryRun) { @@ -90,8 +90,8 @@ function Dry-Run { } #.description -# Reload-Path: Reload the Path environment variable -function Reload-Path { +# Reset-Path: Reload the Path environment variable +function Reset-Path { Write-Output "Reloading Path..." #* 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") @@ -99,7 +99,7 @@ function Reload-Path { # 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-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 @@ -118,7 +118,7 @@ if ( ([Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5 if (!$NoPrompt) { for ( $i = 3; $i -gt 0; $i--) { Write-Host "Press Ctrl+C to exit. Continuing in $i...`r" -NoNewLine - sleep 1 + Start-Sleep 1 } Write-Host "Press any key to continue... " [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 -if ( -not (Command-Available git) ) { +if ( -not (Get-CommandAvailable git) ) { Write-Host "`nInstalling Git with winget..." - Dry-Run 'winget install --id Git.Git' - Reload-Path - if ( -not (Command-Available git)) { + Invoke-DryRun 'winget install --id Git.Git' + Reset-Path + if ( -not (Get-CommandAvailable git)) { Throw "Git failed to install. Aborting." } } else { @@ -143,37 +143,37 @@ if ( -not (Command-Available git) ) { # Create git directory in GitDir if ( -not (Test-Path "$GitDir") ) { - Dry-Run "mkdir '$GitDir'" + Invoke-DryRun "mkdir '$GitDir'" } # Clone the repository in GitDir $dir = $pwd -cd $GitDir -Dry-Run "git clone '$RepoURI'" +Set-Location $GitDir +Invoke-DryRun "git clone '$RepoURI'" # TODO: Remove this when merging -cd Mind_reader -Dry-Run "git checkout johnBreaux" -cd .. +Set-Location Mind_reader +Invoke-DryRun "git checkout johnBreaux" +Set-Location .. # TODO: Remove this when merging # Run the install script if ( -not (Test-Path "$SetupPath")) { Throw "Repository contains no subdirectory '$SetupPath'." } -cd $SetupPath +Set-Location $SetupPath # Run upgrade-windows to install the rest of the dependency chain. -$args = if ($AllowAdministrator) {" -AllowAdministrator"} else {""} -$args += if ($DryRun) {" -DryRun"} else {""} -PowerShell ("./upgrade-windows.ps1 -Install -NoPrompt" + $args) -Reload-Path +$upgradeArgs = if ($AllowAdministrator) {" -AllowAdministrator"} else {""} +$upgradeArgs += if ($DryRun) {" -DryRun"} else {""} +PowerShell ("./upgrade-windows.ps1 -Install -NoPrompt" + $upgradeArgs) +Reset-Path # Open VSCode in the repository location Write-Host "`nOpening Visual Studio Code" -cd $RepoPath -Dry-Run "code ." +Set-Location $RepoPath +Invoke-DryRun "code ." -cd $dir +Set-Location $dir if ( -not $NoPrompt ) { Write-Host "`nPress any key to exit."; [void][Console]::ReadKey(1) } \ No newline at end of file diff --git a/setup-development/windows/upgrade-windows.ps1 b/setup-development/windows/upgrade-windows.ps1 index 809a5fe..09094bb 100644 --- a/setup-development/windows/upgrade-windows.ps1 +++ b/setup-development/windows/upgrade-windows.ps1 @@ -56,9 +56,9 @@ param ( ) # .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 -function Command-Available { +function Get-CommandAvailable { param ($command) # 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! @@ -66,8 +66,8 @@ function Command-Available { } #.description -# Dry-Run a powershell statement -function Dry-Run { +# Invoke-Dryrun a powershell statement +function Invoke-Dryrun { param ([string] $command) $prompt = "> " if ($DryRun) { @@ -80,8 +80,8 @@ function Dry-Run { } #.description -# Reload-Path: Reload the Path environment variable -function Reload-Path { +# Reset-Path: Reload the Path environment variable +function Reset-Path { Write-Output "Reloading Path..." #* 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") @@ -89,7 +89,7 @@ function Reload-Path { # 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-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 @@ -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!" for ( $i = 3; $i -gt 0; $i--) { Write-Host "Press Ctrl+C to exit. Continuing in $i...`r" -NoNewLine - sleep 1 + Start-Sleep 1 } Write-Host "Press any key to continue... " [void][Console]::ReadKey(1) # Equivalent to Command Prompt's `pause` command } } 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." } } @@ -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!) if ( -not $NoWinget) { Write-Host "`nInstalling packages with winget..." - Dry-Run '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 install Microsoft.VisualStudio.2022.BuildTools --override "--wait --quiet --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"' + Invoke-Dryrun "winget import winget/dependencies.json" # 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: -$error = 0 -if ( -not (Command-Available code) ) { - $error += 1; Write-Host -ForegroundColor red "Visual Studio Code not available" +$errorOccurred = 0 +if ( -not (Get-CommandAvailable code) ) { + $errorOccurred += 1; Write-Host -ForegroundColor red "Visual Studio Code not available" } -if ( -not (Command-Available node) ) { - $error += 2; Write-Host -ForegroundColor red "NodeJS not available" +if ( -not (Get-CommandAvailable node) ) { + $errorOccurred += 2; Write-Host -ForegroundColor red "NodeJS not available" } -if ( -not (Command-Available npm ) ) { - $error += 4; Write-Host -ForegroundColor red "Node Package Manager not available"; +if ( -not (Get-CommandAvailable npm ) ) { + $errorOccurred += 4; Write-Host -ForegroundColor red "Node Package Manager not available"; } -if ( $error ) { exit } +if ( $errorOccurred ) { exit } # .description # EnsureNodePackageInstalled: @@ -151,11 +151,11 @@ function EnsureNodePackageInstalled { param ( [string[]]$command ) - if ( ($Install) -or -not (Command-Available $command[0]) ) { + if ( ($Install) -or -not (Get-CommandAvailable $command[0]) ) { Write-Host "`nInstalling $($command[0])..." - Dry-Run "npm install -g $([string]$command)" - Reload-Path - if ( -not (Command-Available $command[0])) { + Invoke-Dryrun "npm install -g $([string]$command)" + Reset-Path + if ( -not (Get-CommandAvailable $command[0])) { Throw "$command failed to install. Aborting." } } else { @@ -174,11 +174,11 @@ $prev_directory = $pwd # install NodeJS dependencies for this extension Write-Host "`nInstalling NodeJS Dependencies..." -cd ..\.. -Dry-Run "npm install" +Set-Location ..\.. +Invoke-Dryrun "npm install" # 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 switch -Regex (code --version) { @@ -187,7 +187,7 @@ switch -Regex (code --version) { } #> "1\.66\.[0-9]+" { # 1.66 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 break } @@ -207,7 +207,7 @@ switch -Regex (code --version) { } # Return from whence we came -cd $prev_directory +Set-Location $prev_directory if ( -not $NoPrompt ) { Write-Host "`nPress any key to exit."; [void][Console]::ReadKey(1) } \ No newline at end of file