2022-04-26 08:19:31 +00:00
<#
. synopsis
Dependency updater for Mind-Reader on Windows .
This script expects to be run from Mind_Reader / setup-development
. description
Updates dependencies ( NodeJS , Python , etc . ) , VSCode , NVDA
The script uses winget ( A . K . A . " App Installer " ) to download and install the latest versions of each dependency , defined in winget / dependencies . json
Winget comes preinstalled on Windows 11 ( 21H2 ) / 10 ( 21H1 ) or newer , and can be installed on Windows 10 1704 + through the Windows Store .
If you download Microsoft ' s developer VM , you have it !
As WinGet is built into Windows , it sidesteps any annoying third-party package managers , and is the lowest common denominator for package installation .
. link
https : / / github . com / We-Dont -Byte / Mind_Reader /
. parameter GitDir
Path to clone the git repo into ( Default : $HOME / git / )
. parameter AllowAdministrator
Force-allow running this script as Administrator ( not recommended , despite the frequent UAC prompts ! )
. parameter NoPrompt
Disable all prompts for user input , and all waiting . ( not recommended when combined with AllowAdministrator ! )
. parameter Install
Force installation / upgrade of all modules , even if already present on the system .
. parameter DryRun
Perform a " dry run " of the script , changing directories and running commands , but without modifying anything .
. example
. / upgrade-windows . ps1
Perform a default upgrade of all Mind_Reader dependencies
. example
. / upgrade-windows . ps1 -DryRun
Perform a dry run of the upgrade process , so you can evaluate what commands will be run
. example
. / upgrade-windows . ps1 -NoPrompt
Don ' t prompt for user input when upgrading
. example
. / upgrade-windows . ps1 -AllowAdministrator
Allow script to be run as Administrator
#>
param (
[ switch ] $AllowAdministrator , # Force allow installation as administrator
[ switch ] $NoPrompt , # Disable the 3-second wait and press-any-key prompt
[ switch ] $Install , # Perform all installations, even when commands are present
[ switch ] $DryRun , # Run script without installing
2022-04-28 22:30:55 +00:00
[ switch ] $NoWinget # Don't update dependdencies with winget
2022-04-26 08:19:31 +00:00
)
# .description
# Command-Available: Checks whether a given command is available.
# If command is available, returns $false
function Command-Available {
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!
RETURN ( Get-Command -Name $command * )
}
# .description
# Dry-Run a powershell statement
function Dry-Run {
param ( [ string ] $command )
$prompt = " > "
if ( $DryRun ) {
Write-Host " $prompt $command [dry] " -ForegroundColor darkgray
}
else {
Write-Host " $prompt $command " -ForegroundColor white
Invoke-Expression $command
}
}
# .description
# Reload-Path: Reload the Path environment variable
function Reload-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 " )
}
# Check if Winget is available
2022-04-28 22:30:55 +00:00
if ( -not ( Command-Available winget ) ) {
Write-Warning " It looks like winget isn't available. `n "
2022-04-26 08:19:31 +00:00
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
exit
}
# Check if the user ran the script with administrator privileges.
# Warn them.
if ( ( [ Security.Principal.WindowsIdentity ] :: GetCurrent ( ) . Groups -contains 'S-1-5-32-544' ) ) {
# If an administrator requests installation as administator,
# for example, to keep UAC prompts to a minimum, allow it.
if ( $AllowAdministrator ) {
# If you pass -AllowAdministrator -NoPrompt as an arg, you're very aware of the damage this script could do to your build env, and you just don't care
# The true chad of sysadmins.
if ( ! $NoPrompt ) {
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
}
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 " Script must be run as a normal user. "
}
}
# Import the packages from dependencies.json (autogenerated file, do not edit!)
2022-04-28 22:30:55 +00:00
if ( -not $NoWinget ) {
Write-Host " `n Installing 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 "
# Reload the PATH, so we can use some of those sweet new commands we just installed
Reload-Path
}
2022-04-26 08:19:31 +00:00
# Check whether everything is available now:
$error = 0
if ( -not ( Command-Available code ) ) {
$error + = 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 ( Command-Available npm ) ) {
$error + = 4 ; Write-Host -ForegroundColor red " Node Package Manager not available " ;
}
if ( $error ) { exit }
2022-04-28 22:30:55 +00:00
# .description
# EnsureNodePackageInstalled:
# Checks for the presence of a cmdlet with a given name
# If it's not found, attempt to install it using npm
# If it's still not found, abort (this may not be good behavior?)
function EnsureNodePackageInstalled {
param (
[ string[] ] $command
)
if ( ( $Install ) -or -not ( Command-Available $command [ 0 ] ) ) {
Write-Host " `n Installing $( $command [ 0 ] ) ... "
Dry-Run " npm install -g $( [ string ] $command ) "
Reload-Path
if ( -not ( Command-Available $command [ 0 ] ) ) {
Throw " $command failed to install. Aborting. "
}
} else {
Write-Host " `n $( $command [ 0 ] ) already installed. " -ForegroundColor green
2022-04-26 08:19:31 +00:00
}
}
2022-04-28 22:30:55 +00:00
# Check if electron-rebuild is installed, if not, install it
EnsureNodePackageInstalled electron-rebuild
# These are useful (but not necessary) packages to have installed when working on new VSCode extensions
EnsureNodePackageInstalled yo , generator-code
2022-04-26 08:19:31 +00:00
# We're about to do some path traversal, so save the current directory
$prev_directory = $pwd
# install NodeJS dependencies for this extension
Write-Host " `n Installing NodeJS Dependencies... "
2022-04-26 09:41:15 +00:00
cd . . \ . .
2022-04-28 22:30:55 +00:00
Dry-Run " npm install "
# Run npm audit fix to upgrade vulnerable dependencies, except breaking changes.
Dry-Run " npm audit fix "
2022-04-26 08:19:31 +00:00
# if we're on a known VSCode version, go ahead and run electron-rebuild
switch -Regex ( code - -version ) {
<# "1\ . 6[7-9]\ . [0-9]+" {
#?: Do we update this in the future, or stop maintaining it and remove this entire switch block?
} #>
" 1\.66\.[0-9]+ " { # 1.66
Write-Host " `n Rebuilding Electron for your version of VSCode... "
Dry-Run 'electron-rebuild --version="17.2.0"'
Write-Host " Done! " -ForegroundColor green
break
}
" \d+\.\d+\.\d+ " { # Anything else
Write-Host " `n Open Visual Studio Code, select the `" Help `" tab in the Toolbar, and go to `" About `" . `n You should see a page that looks like the following: " -ForegroundColor darkcyan
Write-Host " ` (i ` ) Visual Studio Code `n `n Version: 1.66.2 ` (user setup ` ) `n Commit: [Commit ID] `n Date: 2022-04-11T07:46:01.075Z `n Electron: 17.2.0 `n [ ... ] " -ForegroundColor White
Write-Host " Note the Electron version ` (17.2.0 in the above example ` ). " -ForegroundColor darkcyan
Write-Host " Run the command " -NoNewLine
Write-Host " electron-rebuild --version ELECTRON_VERSION " -NoNewLine -ForegroundColor green
Write-Host " in Mind-Reader `' s root folder. `n "
break # Don't process the next items in the collection.
}
default { } # Leave blank
}
# Return from whence we came
cd $prev_directory
if ( -not $NoPrompt ) {
2022-04-26 09:41:15 +00:00
Write-Host " `n Press any key to exit. " ; [ void][Console ] :: ReadKey ( 1 )
2022-04-26 08:19:31 +00:00
}