Skip to content

Instantly share code, notes, and snippets.

@AndrewPla
Last active February 7, 2022 09:18
Show Gist options
  • Save AndrewPla/5c302e91af5448c89a65bfab364249d8 to your computer and use it in GitHub Desktop.
Save AndrewPla/5c302e91af5448c89a65bfab364249d8 to your computer and use it in GitHub Desktop.
Updates the Windows Terminal profiles.json file with all the schemes from https://github.com/mbadolato/iTerm2-Color-Schemes/blob/master/windowsterminal
# Path to the profile when installed from the Windows Store.
$profilePath = "C:\Users\$Env:Username\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\profiles.json"
# Remove existing comments from the profiles.json file.
$profile = (Get-Content $ProfilePath) -replace '(?m)(?<=^([^"]|"[^"]*")*)//.*' -replace '(?ms)/\*.*?\*/' | Out-String | ConvertFrom-Json
$backupProfilePath = "$home\Documents\WindowsTerminalprofiles.json"
Write-Verbose "Backing up profile to $backupProfilePath"
$profile | ConvertTo-Json | Set-Content $backupProfilePath
# Grab all schemes from github
Function Get-WtScheme {
<#
.Description
Returns color schemes from
https://github.com/mbadolato/iTerm2-Color-Schemes/blob/master/windowsterminal
.Parameter Url
Url to the iTerm2 project.
.Parameter Theme
Specify the name of the theme that you want returned. All themes are returned by default
.Example
PS> Get-WtTheme
Returns all available themes
.Example
PS> Get-WtTheme -Filter 'atom.json'
Retrieves the atom.json theme.
.Link
https://github.com/mbadolato/iTerm2-Color-Schemes/blob/master/windowsterminal/
.Link link to blogpost
#>
[cmdletbinding()]
param(
[string]
$Theme = '*',
[string]
$Url = 'https://github.com/mbadolato/iTerm2-Color-Schemes/tree/master/windowsterminal'
)
$page = Invoke-WebRequest $Url -UseBasicParsing
$links = $page.Links | Where-Object title -like "$Theme.json"
Write-Verbose "$($links.count) links found matching $Theme"
foreach ($link in $links) {
# Use the raw url so raw results can be returned and output
$base = 'https://raw.githubusercontent.com'
$href = $link.href
$rawUrl = $base + $href
$rawUrl = $rawUrl.replace('/blob', '')
Invoke-RestMethod $RawUrl
}
}
$schemes = Get-WtScheme
Write-Verbose "We have found $($schemes.count) schemes. Great Success!!"
# This object will contain schemes from our profile and all of the schemes that we just got.
$combinedProperties = [pscustomobject]@()
# loop through the original scheme and export the properties
foreach ($scheme in ($profile.schemes)) {
# Avoid adding duplicate schemes.
if (-not ($combinedProperties.name -like $scheme.name)) {
$combinedProperties += $scheme
}
}
# Add new schemes
foreach ($scheme in $schemes) {
if (-not ($combinedProperties.name -like $scheme.name)) {
$combinedProperties += $scheme
}
}
# Remove the count property from appearing in our json output.
# This only persists for the session
# See https://stackoverflow.com/questions/20848507/why-does-powershell-give-different-result-in-one-liner-than-two-liner-when-conve
Remove-TypeData System.Array -ErrorAction SilentlyContinue
$updatedSchemeObj = [pscustomobject]($combinedProperties)
$profile.schemes = $updatedSchemeObj
Write-Verbose "Updating profile.json with new schemes"
$profile |
ConvertTo-Json -Depth 8 |
Set-Content $profilePath
@AndrewPla
Copy link
Author

Thanks for that, @liuwenzhuang. I have updated the script to reflect that.

Paul try running the script in it's current form. It will remove any comments that you have added to your profiles.json file.

@paulbargaoanu
Copy link

@AndrewPla, thank you! It worked, I now have all the themes in my profiles.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment