Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions bucket/pwsh.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
"Since Scoop uses pwsh.exe internally, to update PowerShell Core itself,",
"run `scoop update pwsh` from Windows PowerShell, i.e. powershell.exe.",
"",
"Add PowerShell Core as a explorer context menu by running: '$dir\\install-explorer-context.reg'",
"For file context menu, run '$dir\\install-file-context.reg'"
"Add PowerShell Core as a explorer context menu by running:",
"$dir\\install-explorer-context.reg",
"",
"For file context menu, run:",
"$dir\\install-file-context.reg",
"",
"Add Windows Terminal PowerShell Profile by running:",
"powershell -ExecutionPolicy Bypass -File $dir\\install-profile.ps1"
],
"architecture": {
"64bit": {
Expand Down Expand Up @@ -39,6 +45,9 @@
" if ($global) { $content = $content.Replace('HKEY_CURRENT_USER', 'HKEY_LOCAL_MACHINE') }",
" Set-Content \"$dir\\$_.reg\" $content -Encoding Ascii -Force",
" }",
"}",
"if (Test-Path \"$bucketsdir\\main\\scripts\\pwsh\\install-profile.ps1\") {",
" Copy-Item \"$bucketsdir\\main\\scripts\\pwsh\\install-profile.ps1\" \"$dir\\install-profile.ps1\" -Force",
"}"
],
"bin": "pwsh.exe",
Expand Down
73 changes: 73 additions & 0 deletions scripts/pwsh/install-profile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Made by rafaeloledo (rafaeloliveiraledo@gmail.com)

# Usage: powershell -ExecutionPolicy Bypass -File install-profile.ps1

$packagesPath = "$env:LOCALAPPDATA\Packages"

$scoopPath = $env:SCOOP.Replace('\','\\')

if (-not $scoopPath) {
Write-Host "SCOOP environment variable is not set." -ForegroundColor Red
exit 1
}

function Get-WindowsTerminalPath {
$wtPackages = Get-ChildItem -Path $packagesPath -Filter "Microsoft.WindowsTerminal_*" -Directory
if ($wtPackages.Count -eq 0) { return $null }
return $wtPackages[0]
}

function Get-WindowsTerminalSettingsPath {
$wtPackage = Get-WindowsTerminalPath
$settingsPath = Join-Path $wtPackage.FullName "LocalState\settings.json"
if (-not (Test-Path $settingsPath)) { return $null }
return $settingsPath
}

$settingsPath = Get-WindowsTerminalSettingsPath
if (-not $settingsPath) { exit 1 }

$settingsText = Get-Content $settingsPath -Raw -Encoding UTF8

try {
$settings = $settingsText | ConvertFrom-Json -ErrorAction Stop
} catch {
Write-Host "Please check for trailing commas or other syntax errors on $settingsPath" -ForegroundColor Red
exit 1
}

$ProfileName = "PowerShell"
$existingProfile = $settings.profiles.list | Where-Object { $_.name -eq $ProfileName }
if ($existingProfile) { exit 0 }

$guid = [guid]::NewGuid().ToString()

$newProfile = @"
{
"commandline": "$scoopPath\\apps\\pwsh\\current\\pwsh.exe -nologo",
"guid": "{$guid}",
"hidden": false,
"icon": "$scoopPath\\apps\\pwsh\\current\\pwsh.exe",
"name": "$ProfileName",
"startingDirectory": "%USERPROFILE%"
}
"@

# magic regex
$regex = '(?s)(\"list\":\s*\[)(.*?)(\s*\])'

if ($settingsText -match $regex) {
$listContent = $matches[2]

if ($listContent.Trim() -eq "") {
# empty list -> insert profile without a leading comma
$result = "`$1`n$newProfile`$3"
} else {
# non-empty list -> append profile with a single separating comma
$result = "`$1`$2,`n$newProfile`$3"
}

$settingsText = $settingsText -replace $regex, $result
}

[System.IO.File]::WriteAllText($settingsPath, $settingsText, [System.Text.UTF8Encoding]($false))