-
Notifications
You must be signed in to change notification settings - Fork 10
/
Install-ConEmuTheme.ps1
78 lines (64 loc) · 2.59 KB
/
Install-ConEmuTheme.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[ValidateScript({Test-Path -Path $_})]
[string]
$ConfigPath = $env:APPDATA + "\ConEmu.xml",
[Parameter(Position = 1, Mandatory = $true)]
[ValidateSet("Add", "Remove")]
[string]
$Operation = "Add",
[Parameter(Position = 2, Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({($Operation -eq "Remove") -or (Test-Path -Path $_)})]
[string]
$ThemePathOrName
)
try {
[Xml]$config = Get-Content -Path $ConfigPath -Encoding UTF8
$config.Save([System.IO.Path]::ChangeExtension($ConfigPath, ".backup.xml"))
$vanilla = $config.key.key.key | Where-Object { $_.name -eq ".Vanilla" }
$colors = $vanilla.key | Where-Object { $_.name -eq "Colors" }
switch ($Operation) {
"Add" {
[Xml]$theme = Get-Content -Path $ThemePathOrName
if ($colors -eq $null) {
[Xml]$emptyColors = "<key name='Colors'><value name='Count' type='long' data='0'/></key>"
$vanilla.AppendChild($config.ImportNode($emptyColors.DocumentElement, $true)) | Out-Null
$colors = $vanilla.key | Where-Object { $_.name -eq "Colors" }
} else {
$themeName = ($theme.key.value | Where-Object { $_.name -eq "Name" }).data
$existingTheme = $colors.key | Where-Object { $_.value | Where-Object { $_.name -eq "Name" -and $_.data -eq $themeName } }
if ($existingTheme -ne $null) {
throw "Theme was already added to config"
}
}
$colors.AppendChild($config.ImportNode($theme.DocumentElement, $true)) | Out-Null
}
"Remove" {
if ($colors -eq $null -or $colors.key -eq $null) {
throw "No themes in config"
}
$theme = $colors.key | Where-Object { $_.value | Where-Object { $_.name -eq "Name" -and $_.data -eq $ThemePathOrName } }
if ($theme -eq $null) {
throw "Theme not found in config"
}
$colors.RemoveChild($theme) | Out-Null
}
}
if ($colors.key -eq $null) {
$colors.value.data = "0"
} elseif ($colors.key -is [System.Array]) {
$colors.value.data = $colors.key.Count.ToString()
for ($i = 0; $i -lt $colors.key.Count; $i++) {
$colors.key[$i].name = "Palette$($i + 1)"
}
} else {
$colors.value.data = "1"
$colors.key.name = "Palette1"
}
$config.Save($ConfigPath)
} catch {
Write-Error -Message $_
}