Skip to content

Instantly share code, notes, and snippets.

@Jacobboogiebear
Created October 28, 2023 15:40
Show Gist options
  • Save Jacobboogiebear/0becb9160c17cd5850837a69b599317b to your computer and use it in GitHub Desktop.
Save Jacobboogiebear/0becb9160c17cd5850837a69b599317b to your computer and use it in GitHub Desktop.

What it does?

Allows you to run "Enable-MSYS2" to open MSYS2 terminal in current directory in powershell, and installs it using winget if not installed

How to install?

Just copy the contents of Microsoft.PowerShell_profile.ps1 into that same file in %userprofile%/Documents/PowerShell/Microsoft.PowerShell_profile.ps1

function global:Invoke-BatchFile
{
param([string]$Path, [string]$Parameters)
$tempFile = [IO.Path]::GetTempFileName()
cmd.exe /c " `"$Path`" $Parameters && set > `"$tempFile`" "
Get-Content $tempFile | Foreach-Object {
if ($_ -match "^(.*?)=(.*)$")
{
Set-Content "env:\$($matches[1])" $matches[2]
}
}
Remove-Item $tempFile
}
function global:Enable-MSYS2
{
if (-not [System.IO.File]::Exists("$env:USERPROFILE\Documents\PowerShell\msys2.cmd"))
{
$file = "$env:USERPROFILE\Documents\PowerShell\msys2.cmd"
Add-Content "$file" "@echo off"
Add-Content "$file" "setlocal"
Add-Content "$file" "rem To activate windows native symlinks uncomment next line"
Add-Content "$file" "set MSYS=winsymlinks:nativestrict"
Add-Content "$file" "rem Shell types"
Add-Content "$file" "if ""x%~1"" == ""x-msys2"" set MSYSTEM=MSYS"
Add-Content "$file" "if ""x%~1"" == ""x-mingw32"" set MSYSTEM=MINGW32"
Add-Content "$file" "if ""x%~1"" == ""x-mingw64"" set MSYSTEM=MINGW64"
Add-Content "$file" "rem Shell types"
Add-Content "$file" "rem set MSYSTEM=MSYS"
Add-Content "$file" "C:\msys64\usr\bin\bash.exe --login -i -c ""cd %1 && /bin/bash"""
Add-Content "$file" "exit /b 0"
}
if (-not [System.IO.File]::Exists("C:\msys64\usr\bin\bash.exe"))
{
winget install --id=MSYS2.MSYS2 -e
C:\msys64\usr\bin\bash.exe --login -i -c "pacman -Sy --noconfirm pacman-mirrors && pacman -Sy --noconfirm unzip && mkdir /usr/local/bin && curl -sS https://starship.rs/install.sh | sh -s -- --force && echo eval ""`$(starship init bash)"" >> ~/.bashrc"
}
Invoke-BatchFile "$env:USERPROFILE\Documents\PowerShell\msys2.cmd" $PWD.Path.Replace("\", "/")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment