2022-04-26 08:19:31 +00:00
<#
. synopsis
2022-05-06 01:56:10 +00:00
Dependency installer for Mind Reader on Windows .
2022-04-26 08:19:31 +00:00
This sets up a development environment from a BARE windows install .
. description
2022-05-06 01:56:10 +00:00
Install Git for Windows , clone the Mind Reader repository , and install all dependencies .
2022-04-26 08:19:31 +00:00
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 ForceInstall
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
. / install-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
. / install-windows . ps1 -DryRun
Perform a dry run of the upgrade process , so you can evaluate what commands will be run
. example
. / install-windows . ps1 -NoPrompt
Don ' t prompt for user input when upgrading
. example
. / install-windows . ps1 AllowAdministrator
Allow script to run as Administrator
#>
param (
[ string ] $GitDir = " $HOME /git/ " , # Path to clone the git repo into
[ switch ] $h , [ switch ] $Help , # Get help
[ switch ] $AllowAdministrator , # Force allow installation as administrator
[ switch ] $NoPrompt , # Disable the 3-second wait and press-any-key prompt
[ switch ] $ForceInstall , # Always try to install
[ switch ] $DryRun # Run script without installing
)
2022-04-28 22:30:55 +00:00
$RepoURI = " https://github.com/We-Dont-Byte/Mind_Reader.git "
2022-04-26 09:41:15 +00:00
$RepoPath = " $GitDir \Mind_Reader "
$SetupPath = " $RepoPath \setup-development\windows "
2022-04-26 08:19:31 +00:00
if ( $h -or $Help ) {
Get-Help . / install-windows . ps1
exit
}
# .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-04-26 08:19:31 +00:00
$env:Path = [ System.Environment ] :: GetEnvironmentVariable ( " Path " , " Machine " ) + " ; " + [ System.Environment ] :: GetEnvironmentVariable ( " Path " , " User " )
}
# 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 ) {
Write-Warning " Script was run as Administrator. Exit now if you didn't mean to do this! "
# If you pass 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 ) {
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
}
} else {
# Throw a fatal error if the user tries to run as administrator.
Throw " Script must be run as a normal user. "
}
}
# Install Git
2022-04-30 18:01:04 +00:00
if ( -not ( Get-CommandAvailable git ) ) {
2022-04-26 08:19:31 +00:00
Write-Host " `n Installing Git with winget... "
2022-04-30 18:01:04 +00:00
Invoke-DryRun 'winget install --id Git.Git'
Reset-Path
if ( -not ( Get-CommandAvailable git ) ) {
2022-04-26 08:19:31 +00:00
Throw " Git failed to install. Aborting. "
}
} else {
Write-Host " Git already installed. " -ForegroundColor green
}
# Create git directory in GitDir
if ( -not ( Test-Path " $GitDir " ) ) {
2022-04-30 18:01:04 +00:00
Invoke-DryRun " mkdir ' $GitDir ' "
2022-04-26 08:19:31 +00:00
}
# Clone the repository in GitDir
$dir = $pwd
2022-04-30 18:01:04 +00:00
Set-Location $GitDir
Invoke-DryRun " git clone ' $RepoURI ' "
2022-04-28 22:30:55 +00:00
# TODO: Remove this when merging
2022-04-30 18:01:04 +00:00
Set-Location Mind_reader
Invoke-DryRun " git checkout johnBreaux "
Set-Location . .
2022-04-28 22:30:55 +00:00
# TODO: Remove this when merging
2022-04-26 08:19:31 +00:00
# Run the install script
if ( -not ( Test-Path " $SetupPath " ) ) {
Throw " Repository contains no subdirectory ' $SetupPath '. "
}
2022-04-30 18:01:04 +00:00
Set-Location $SetupPath
2022-04-26 08:19:31 +00:00
# Run upgrade-windows to install the rest of the dependency chain.
2022-04-30 18:01:04 +00:00
$upgradeArgs = if ( $AllowAdministrator ) { " -AllowAdministrator " } else { " " }
$upgradeArgs + = if ( $DryRun ) { " -DryRun " } else { " " }
PowerShell ( " ./upgrade-windows.ps1 -Install -NoPrompt " + $upgradeArgs )
Reset-Path
2022-04-26 08:19:31 +00:00
2022-04-26 09:19:14 +00:00
# Open VSCode in the repository location
2022-04-26 09:41:15 +00:00
Write-Host " `n Opening Visual Studio Code "
2022-04-30 18:01:04 +00:00
Set-Location $RepoPath
Invoke-DryRun " code . "
2022-04-26 08:19:31 +00:00
2022-04-30 18:01:04 +00:00
Set-Location $dir
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
}