Last active
October 16, 2021 01:36
-
-
Save Nejat/0df134a3fbfc6f7d47d384f5e78a7b09 to your computer and use it in GitHub Desktop.
installs MSys2 or refreshes an existing installation using winget; updates pacman, and optionally executes additional bash setup commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
# provide a list of addition bash setup commands to invoke | |
[string] $additional = "msys2-additional-setup", | |
# true teardown existing installations, false to stop if installation exists | |
[switch] $refresh = $false | |
) | |
# constant defining MSys2 winget package name | |
[string] $package = "MSys2" | |
# checks to see if an installaiton already exists | |
[string] $check = winget list $package --source winget | |
# generates a user home path, c:\msys64 is default winget installation path | |
[string] $user_home = "C:\msys64\home\$($env:UserName)" | |
# determines the padding for uniform formated progress output | |
[int32] $padding = 18 + $user_home.Length # 18 is derived from potentially the widest output without the user name | |
# load and parse any additional bash setup commands if file it exists, otherwise skips any additional setup | |
[PSCustomObject[]] $bashCommands = if (Test-Path -Path $additional -PathType Leaf) { | |
[string[]] $parts = @() | |
[string] $command = "" | |
[string] $message = "" | |
Get-Content $additional | ForEach-Object { | |
# ignore blank lines or comment lines | |
if ([string]::IsNullOrWhiteSpace($_) -or $_.Trim().StartsWith("#")) { | |
return | |
} | |
# parses the line "command | message" | |
$parts = $_.Split('|') | |
$command = $parts[0].Trim() | |
# minimally validate command | |
if ([string]::IsNullOrWhiteSpace($command)) { | |
Write-Host "Additional setup must provide a valid bash ""Command""" -ForegroundColor Red | |
exit -1 | |
} | |
$message = $parts[1].Trim() | |
# minimally validate message | |
if ([string]::IsNullOrWhiteSpace($message)) { | |
Write-Host "Additional setup must provide a descriptive ""Message""" -ForegroundColor Red | |
exit -2 | |
} | |
# determines the padding for uniform formated progress output | |
if ($message.Length -gt $padding) { | |
$padding = $message.Length | |
} | |
# adds a --noconfirm flag to pacman instructions | |
if ($command.ToUpper().Contains("PACMAN") -and !$command.ToUpper().Contains("--NOCONFIRM")) { | |
$command += " --noconfirm" | |
} | |
[PSCustomObject] @{ | |
Command = $command | |
Message = $message | |
} | |
} | |
} else { | |
@() # otherwise no additional setup | |
} | |
# confims the success of the previous instruction and outputs a status message, exits script if it failed | |
function Confirm-Success { | |
Write-Host " - " -NoNewline | |
if ($LASTEXITCODE -ne 0) { | |
Write-Host failed: $LASTEXITCODE -ForegroundColor Red | |
exit $LASTEXITCODE | |
} else { | |
Write-Host succeeded -ForegroundColor Blue | |
} | |
} | |
# invokes and checks the success of bash command | |
function Invoke-BashCommand { | |
param ([string] $command) | |
# c:\msys64 is default winget installation path | |
C:\msys64\usr\bin\bash -l -c $command *>$null | |
Confirm-Success | |
} | |
# checks to see if MSys2 is installed | |
if (!$check.ToUpper().Contains("NO INSTALLED PACKAGE") ) { | |
# if installed and the refresh flag is set, MSys2 is uninstalled | |
if ($refresh) { | |
Write-Host "Uninstalling $package".PadRight($padding) -NoNewline | |
winget uninstall $package --source winget | Out-Null | |
Confirm-Success | |
} else { | |
# otherwise the script is exited | |
Write-Host $package is already installed -ForegroundColor Yellow | |
return | |
} | |
} | |
# install MSys2 and confirm success | |
Write-Host "Installing $package".PadRight($padding) -NoNewline | |
winget install $package --source winget | Out-Null | |
Confirm-Success | |
# create the current users home folder and confirm success | |
Write-Host "Creating ""$user_home"" Folder".PadRight($padding) -NoNewline | |
New-Item -ItemType Directory -Path $user_home | Out-Null | |
Confirm-Success | |
# update system packages and confirm success | |
Write-Host "Updating $package System Packages".PadRight($padding) -NoNewline | |
Invoke-BashCommand "pacman -Syu --noconfirm" | |
# update packages and confirm success | |
Write-Host "Updating $package Packages".PadRight($padding) -NoNewline | |
Invoke-BashCommand "pacman -Su --noconfirm" | |
# invoke additional bash setup comands | |
foreach($bash in $bashCommands) { | |
Write-Host $bash.Message.PadRight($padding) -NoNewline | |
Invoke-BashCommand $bash.Command | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment