2022-04-26 08:19:31 +00:00
<#
. synopsis
2022-05-06 01:56:10 +00:00
Dependency updater for Mind Reader on Windows .
2022-04-26 08:19:31 +00:00
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
2022-05-06 01:56:10 +00:00
Perform a default upgrade of all Mind Reader dependencies
2022-04-26 08:19:31 +00:00
. 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
2022-05-05 21:45:48 +00:00
[ 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
2022-04-30 18:01:04 +00:00
# Get-CommandAvailable: Checks whether a given command is available.
2022-04-26 08:19:31 +00:00
# If command is available, returns $false
2022-04-30 18:01:04 +00:00
function Get-CommandAvailable {
2022-04-26 08:19:31 +00:00
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
2022-04-30 18:01:04 +00:00
# Invoke-Dryrun a powershell statement
function Invoke-Dryrun {
2022-04-26 08:19:31 +00:00
param ( [ string ] $command )
$prompt = " > "
if ( $DryRun ) {
Write-Host " $prompt $command [dry] " -ForegroundColor darkgray
}
else {
Write-Host " $prompt $command " -ForegroundColor white
Invoke-Expression $command
}
}
# .description
2022-04-30 18:01:04 +00:00
# Reset-Path: Reload the Path environment variable
function Reset-Path {
2022-04-26 08:19:31 +00:00
Write-Output " Reloading Path... "
2022-05-05 21:12:55 +00:00
#* This code was created by user [mpen](https://stackoverflow.com/users/65387/mpen) on [StackOverflow](https://stackoverflow.com/a/31845512) and is used in accordance with Creative Commons CC BY-SA 3.0
2022-05-05 21:45:48 +00:00
$env:Path = [ System.Environment ] :: GetEnvironmentVariable ( " Path " , " Machine " ) + " ; " + [ System.Environment ] :: GetEnvironmentVariable ( " Path " , " User " )
2022-04-26 08:19:31 +00:00
}
# Check if Winget is available
2022-04-30 18:01:04 +00:00
if ( -not ( Get-CommandAvailable winget ) ) {
2022-04-28 22:30:55 +00:00
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
2022-04-30 18:01:04 +00:00
Start-Sleep 1
2022-04-26 08:19:31 +00:00
}
Write-Host " Press any key to continue... "
[ void][Console ] :: ReadKey ( 1 ) # Equivalent to Command Prompt's `pause` command
}
2022-05-05 21:45:48 +00:00
}
else {
2022-04-30 18:01:04 +00:00
# Throw a fatal errorOccurred if the user tries to run as administrator.
2022-04-26 08:19:31 +00:00
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... "
2022-04-30 18:01:04 +00:00
Invoke-Dryrun 'winget install Microsoft.VisualStudio.2022.BuildTools --override "--wait --quiet --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"'
Invoke-Dryrun " winget import winget/dependencies.json "
2022-04-28 22:30:55 +00:00
# Reload the PATH, so we can use some of those sweet new commands we just installed
2022-04-30 18:01:04 +00:00
Reset-Path
2022-04-28 22:30:55 +00:00
}
2022-04-26 08:19:31 +00:00
# Check whether everything is available now:
2022-04-30 18:01:04 +00:00
$errorOccurred = 0
if ( -not ( Get-CommandAvailable code ) ) {
$errorOccurred + = 1 ; Write-Host -ForegroundColor red " Visual Studio Code not available "
2022-04-26 08:19:31 +00:00
}
2022-04-30 18:01:04 +00:00
if ( -not ( Get-CommandAvailable node ) ) {
$errorOccurred + = 2 ; Write-Host -ForegroundColor red " NodeJS not available "
2022-04-26 08:19:31 +00:00
}
2022-04-30 18:01:04 +00:00
if ( -not ( Get-CommandAvailable npm ) ) {
$errorOccurred + = 4 ; Write-Host -ForegroundColor red " Node Package Manager not available " ;
2022-04-26 08:19:31 +00:00
}
2022-04-30 18:01:04 +00:00
if ( $errorOccurred ) { exit }
2022-04-26 08:19:31 +00:00
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
)
2022-04-30 18:01:04 +00:00
if ( ( $Install ) -or -not ( Get-CommandAvailable $command [ 0 ] ) ) {
2022-04-28 22:30:55 +00:00
Write-Host " `n Installing $( $command [ 0 ] ) ... "
2022-04-30 18:01:04 +00:00
Invoke-Dryrun " npm install -g $( [ string ] $command ) "
Reset-Path
if ( -not ( Get-CommandAvailable $command [ 0 ] ) ) {
2022-04-28 22:30:55 +00:00
Throw " $command failed to install. Aborting. "
}
2022-05-05 21:45:48 +00:00
}
else {
2022-04-28 22:30:55 +00:00
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-30 18:01:04 +00:00
Set-Location . . \ . .
Invoke-Dryrun " npm install "
2022-04-28 22:30:55 +00:00
# Run npm audit fix to upgrade vulnerable dependencies, except breaking changes.
2022-04-30 18:01:04 +00:00
Invoke-Dryrun " 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 ) {
2022-05-05 21:45:48 +00:00
#?: Do we update this in the future, or stop maintaining it and remove this entire switch block?
" 1\.67\.\d+ " { $electronversion = " 17.4.1 " ; break } # April 2022 update
" 1\.66\.\d+ " { $electronversion = " 17.2.0 " ; break } # March 2022 update
default { $electronversion = $false } # Unknown update
}
if ( $electronversion ) {
Write-Host " `n Rebuilding Electron for your version of VSCode... "
Invoke-Dryrun " electron-rebuild --version=' $electronversion ' "
Write-Host " Done! " -ForegroundColor green
}
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
2022-04-26 08:19:31 +00:00
2022-05-05 21:45:48 +00:00
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
2022-04-26 08:19:31 +00:00
2022-05-05 21:45:48 +00:00
Write-Host " Note the Electron version ` (17.2.0 in the above example ` ). " -ForegroundColor darkcyan
2022-04-26 08:19:31 +00:00
2022-05-05 21:45:48 +00:00
Write-Host " Run the command " -NoNewLine
Write-Host " electron-rebuild --version ELECTRON_VERSION " -NoNewLine -ForegroundColor green
2022-05-06 01:56:10 +00:00
Write-Host " in Mind Reader `' s root folder. `n "
2022-04-26 08:19:31 +00:00
}
# Return from whence we came
2022-04-30 18:01:04 +00:00
Set-Location $prev_directory
2022-04-26 08:19:31 +00:00
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
}