-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.ps1
379 lines (328 loc) · 15.7 KB
/
generate.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#Requires -Version 6
[CmdletBinding()]
param(
[ValidateSet('Compact', 'Flat', 'Minimal')]
[string]$Layout = 'Compact',
[ValidateSet('No', 'Yes', 'Admin')]
[string]$Extended = 'No',
[switch]$NoAdmin,
[string]$Language = (Get-UICulture).IetfLanguageTag
)
function GetInstallationInfo() {
$edition = 0
if ($null -ne ($appx = (Get-AppxPackage Microsoft.WindowsTerminal))) {
$installDir = $appx.InstallLocation
Write-Host "Found Windows Terminal (Version $($appx.Version)): $installDir"
$edition = 1
}
if ($edition -eq 0 -and $null -ne ($appx = (Get-AppxPackage Microsoft.WindowsTerminalPreview))) {
$installDir = $appx.InstallLocation
Write-Host "Found Windows Terminal Preview (Version $($appx.Version)): $installDir"
$edition = 2
}
if ($edition -eq 0) {
Write-Error 'Not installed (the specified edition of) Windows Terminal.'
exit 1
}
return $edition, $installDir
}
function GetTranslations() {
$content = Get-Content -Path "$PSScriptRoot\translations.json" | ConvertFrom-Json -AsHashtable
$result = $null
foreach ($key in $content.Keys) {
if ($Language -match $key) {
$result = $content[$key]
break
}
}
if ($null -eq $result) {
$result = $content['^en']
Write-Warning 'There is no translation corresponding to the specified/system language, so English is used.'
}
return $result
}
function GenerateLaunchScript() {
if ($Layout -ne 'Minimal') {
Write-Output @'
If WScript.Arguments.Count > 1 Then
Set shell = WScript.CreateObject("Shell.Application")
dir = WScript.Arguments(0)
profile = WScript.Arguments(1)
If WScript.Arguments.Count = 2 Then
shell.ShellExecute "wt", "-p " & profile & " -d """ & dir & """", "", "", 1
ElseIf WScript.Arguments(2) = "-elevated" Then
shell.ShellExecute "wt", "-p " & profile & " -d """ & dir & """", "", "RunAs", 1
End If
End If
'@ > "$Storage\launch.vbs"
} else {
Write-Output @'
If WScript.Arguments.Count > 0 Then
Set shell = WScript.CreateObject("Shell.Application")
dir = WScript.Arguments(0)
If WScript.Arguments.Count = 1 Then
shell.ShellExecute "wt", " -d """ & dir & """", "", "", 1
ElseIf WScript.Arguments(1) = "-elevated" Then
shell.ShellExecute "wt", " -d """ & dir & """", "", "RunAs", 1
End If
End If
'@ > "$Storage\launch.vbs"
}
}
function GetTerminalSettings() {
if ($Edition -eq 1) {
$settingFile = "$env:LocalAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
} else {
$settingFile = "$env:LocalAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState\settings.json"
}
if (-not (Test-Path $settingFile)) {
Write-Error "The setting file isn't exist."
exit 1
}
return Get-Content $settingFile | Out-String | ConvertFrom-Json
}
function ExtractTerminalIcon() {
$iconFile = "$Storage\terminal.ico"
$tempPng = "$env:TEMP\temp.png"
[Drawing.Icon]::ExtractAssociatedIcon("$InstallDir\WindowsTerminal.exe").ToBitmap().Save($tempPng)
ConvertToIcon $tempPng $iconFile
Remove-Item $tempPng
return $iconFile
}
function GetActiveProfiles() {
if ($Settings.profiles.PSObject.Properties.name -eq 'list') {
$list = $Settings.profiles.list
} else {
$list = $Settings.profiles
}
return $list | Where-Object { -not $_.hidden }
}
function ConvertToIcon([Parameter(Mandatory)][string]$file, [Parameter(Mandatory)][string]$outputFile) {
$inputBitmap = [Drawing.Image]::FromFile($file)
$width = $inputBitmap.Width
$height = $inputBitmap.Height
$newBitmap = [Drawing.Bitmap]::new($inputBitmap, $width, $height)
$memoryStream = [IO.MemoryStream]::new()
$newBitmap.Save($memoryStream, [Drawing.Imaging.ImageFormat]::Png)
if ($width -gt 255 -or $height -gt 255) {
$ratio = ($height, $width | Measure-Object -Maximum).Maximum / 255
$width /= $ratio
$height /= $ratio
}
$output = [IO.File]::Create($outputFile)
$iconWriter = [IO.BinaryWriter]::new($output)
$iconWriter.Write([byte]0)
$iconWriter.Write([byte]0)
$iconWriter.Write([short]1)
$iconWriter.Write([short]1)
$iconWriter.Write([byte]$width)
$iconWriter.Write([byte]$height)
$iconWriter.Write([byte]0)
$iconWriter.Write([byte]0)
$iconWriter.Write([short]0)
$iconWriter.Write([short]32)
$iconWriter.Write([int]$memoryStream.Length)
$iconWriter.Write([int]22)
$iconWriter.Write($memoryStream.ToArray())
$iconWriter.Flush()
$output.Close()
$memoryStream.Dispose()
$newBitmap.Dispose()
$inputBitmap.Dispose()
}
function GetProfileIcon([Parameter(Mandatory)]$wtProfile) {
if ($null -ne $wtProfile.icon) {
if ($wtProfile.icon -match '^ms-appx:///.*') {
$iconFile = $InstallDir + '\' + ($wtProfile.icon -replace ('ms-appx:///', '') -replace ('/', '\'))
if (-not ($iconFile -match '^.*\.scale-.*\.png$')) {
$iconFile = $iconFile -replace ('\.png$', '.scale-200.png')
}
} elseif ($wtProfile.icon -match '^ms-appdata:///Local/.*') {
if ($Edition -eq 1) {
$iconFile = "$env:LocalAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\" +
($wtProfile.icon -replace ('ms-appdata:///Local/', '') -replace ('/', '\'))
} else {
$iconFile = "$env:LocalAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState\" +
($wtProfile.icon -replace ('ms-appdata:///Local/', '') -replace ('/', '\'))
}
} elseif ($wtProfile.icon -match '^ms-appdata:///Roaming/.*') {
if ($Edition -eq 1) {
$iconFile = "$env:LocalAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState\" +
($wtProfile.icon -replace ('ms-appdata:///Roaming/', '') -replace ('/', '\'))
} else {
$iconFile = "$env:LocalAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\RoamingState\" +
($wtProfile.icon -replace ('ms-appdata:///Roaming/', '') -replace ('/', '\'))
}
} else {
$iconFile = [Environment]::ExpandEnvironmentVariables($wtProfile.icon)
}
} else {
if ($wtProfile.source -eq 'Windows.Terminal.Wsl') {
$iconFile = "$InstallDir\ProfileIcons\{9acb9455-ca41-5af7-950f-6bca1bc9722f}.scale-200.png"
} elseif ($wtProfile.source -eq 'Git') {
$gitIcon = Convert-Path ((Get-Command git).Path + '\..\..\mingw64\share\git\git-for-windows.ico')
if (-not (Test-Path $gitIcon)) {
$gitIcon = Convert-Path ((Get-Command git).Path + '\..\..\mingw32\share\git\git-for-windows.ico')
}
$iconFile = $gitIcon
} else {
$iconFile = "$InstallDir\ProfileIcons\$guid.scale-200.png"
}
}
if (Test-Path $iconFile) {
if ($iconFile -match '\.ico$') {
return $iconFile
} elseif ($iconFile -match '\.png$') {
$iconCopy = "$Storage\icon-$guid.ico"
ConvertToIcon $iconFile $iconCopy
return $iconCopy
} elseif ($iconFile -match '\.exe$') {
$tempPng = "$env:TEMP\temp.png"
[Drawing.Icon]::ExtractAssociatedIcon($iconFile).ToBitmap().Save($tempPng)
$iconCopy = "$Storage\icon-$guid.ico"
ConvertToIcon $tempPng $iconCopy
Remove-Item $tempPng
return $iconCopy
} else {
Write-Warning "Unknown icon file type: $iconFile"
return $TerminalIcon
}
} else {
Write-Warning "Icon file not found: $iconFile"
return $TerminalIcon
}
}
function AddMenuItemForProfile([Parameter(Mandatory)]$wtProfile, [Parameter(Mandatory)][int]$index) {
$guid = $wtProfile.guid
$name = $wtProfile.name
Write-Host "Profile ${guid}: $name"
$digits = 1
for ($x = [Math]::DivRem($index, 10).Item1; $x -ne 0; $x = [Math]::DivRem($x, 10).Item1) {
++$digits
}
$order = '0' * (10 - $digits) + $index
$icon = GetProfileIcon $wtProfile
if ($Layout -ne 'Compact' -or $index -ge 36) {
$display = $name
} elseif ($index -ge 10) {
$display = "$name (&$([char]($index - 10 + 65)))"
} elseif ($index -eq 9) {
$display = "$name (&0)"
} else {
$display = "$name (&$([char]($index + 1 + 48)))"
}
if ($Layout -eq 'Compact') {
$key = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\WindowsTerminalContextMenu\shell\$order-$guid"
} elseif ($Layout -eq 'Flat') {
$key = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\WindowsTerminalContextMenu-$order"
}
$command = "wscript `"$Storage\launch.vbs`" `"%V\.`" $guid"
New-Item -Path $key -Force | Out-Null
if ($Layout -eq 'Flat') {
New-ItemProperty -Path $key -Name '(Default)' -PropertyType String -Value $display | Out-Null
} else {
New-ItemProperty -Path $key -Name 'MUIVerb' -PropertyType String -Value $display | Out-Null
}
New-ItemProperty -Path $key -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-Item -Path "$key\command" | Out-Null
New-ItemProperty -Path "$key\command" -Name '(Default)' -PropertyType String -Value $command | Out-Null
if ($Layout -eq 'Flat') {
Copy-Item -Recurse "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\WindowsTerminalContextMenu-$order" `
"Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\WindowsTerminalContextMenu-$order" | Out-Null
Copy-Item -Recurse "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\WindowsTerminalContextMenu-$order" `
"Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Drive\shell\WindowsTerminalContextMenu-$order" | Out-Null
Copy-Item -Recurse "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\WindowsTerminalContextMenu-$order" `
"Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\LibraryFolder\Background\shell\WindowsTerminalContextMenu-$order" | Out-Null
}
if (-not $NoAdmin) {
if ($Layout -eq 'Compact') {
$key = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\WindowsTerminalContextMenu-Elevated\shell\$order-$guid"
} elseif ($Layout -eq 'Flat') {
$key = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\WindowsTerminalContextMenu-Elevated-$order"
}
$command = "wscript `"$Storage\launch.vbs`" `"%V\.`" $guid -elevated"
New-Item -Path $key -Force | Out-Null
if ($Layout -eq 'Flat') {
New-ItemProperty -Path $key -Name '(Default)' -PropertyType String -Value ("$display" + $Translations['admin-sign']) | Out-Null
} else {
New-ItemProperty -Path $key -Name 'MUIVerb' -PropertyType String -Value $display | Out-Null
}
New-ItemProperty -Path $key -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path $key -Name 'HasLUAShield' -PropertyType String -Value '' | Out-Null
New-Item -Path "$key\command" | Out-Null
New-ItemProperty -Path "$key\command" -Name '(Default)' -PropertyType String -Value $command | Out-Null
if ($Layout -eq 'Flat') {
Copy-Item -Recurse "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\WindowsTerminalContextMenu-Elevated-$order" `
"Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Drive\shell\WindowsTerminalContextMenu-Elevated-$order" | Out-Null
Copy-Item -Recurse "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\WindowsTerminalContextMenu-Elevated-$order" `
"Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\WindowsTerminalContextMenu-Elevated-$order" | Out-Null
Copy-Item -Recurse "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\WindowsTerminalContextMenu-Elevated-$order" `
"Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\LibraryFolder\Background\shell\WindowsTerminalContextMenu-Elevated-$order" | Out-Null
}
}
}
function AddMenu([Parameter(Mandatory)][string]$key, [Parameter(Mandatory)][int]$elevated) {
New-Item -Path $key -Force | Out-Null
New-ItemProperty -Path $key -Name 'Icon' -PropertyType String -Value $TerminalIcon | Out-Null
if (-not $elevated) {
New-ItemProperty -Path $key -Name 'MUIVerb' -PropertyType String -Value ($Translations['standard']) | Out-Null
if ($Extended -eq 'Yes') {
New-ItemProperty -Path $key -Name 'Extended' -PropertyType String -Value '' | Out-Null
}
if ($Layout -eq 'Compact') {
New-ItemProperty -Path $key -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'WindowsTerminalContextMenu' | Out-Null
} elseif ($Layout -eq 'Minimal') {
New-Item -Path "$key\command" | Out-Null
New-ItemProperty -Path "$key\command" -Name '(Default)' -PropertyType String -Value "wscript `"$Storage\launch.vbs`" `"%V\.`"" | Out-Null
}
} else {
New-ItemProperty -Path $key -Name 'MUIVerb' -PropertyType String -Value ($Translations['admin']) | Out-Null
New-ItemProperty -Path $key -Name 'HasLUAShield' -PropertyType String -Value '' | Out-Null
if ($Extended -eq 'Yes' -or $Extended -eq 'Admin') {
New-ItemProperty -Path $key -Name 'Extended' -PropertyType String -Value '' | Out-Null
}
if ($Layout -eq 'Compact') {
New-ItemProperty -Path $key -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'WindowsTerminalContextMenu-Elevated' | Out-Null
} elseif ($Layout -eq 'Minimal') {
New-Item -Path "$key\command" | Out-Null
New-ItemProperty -Path "$key\command" -Name '(Default)' -PropertyType String -Value "wscript `"$Storage\launch.vbs`" `"%V\.`" -elevated" | Out-Null
}
}
}
function CreateMenus() {
if ($Layout -ne 'Flat') {
AddMenu 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\WindowsTerminalContextMenu' $false
AddMenu 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\WindowsTerminalContextMenu' $false
AddMenu 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Drive\shell\WindowsTerminalContextMenu' $false
AddMenu 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\LibraryFolder\Background\shell\WindowsTerminalContextMenu' $false
if (-not $NoAdmin) {
AddMenu 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\WindowsTerminalContextMenu-Elevated' $true
AddMenu 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\WindowsTerminalContextMenu-Elevated' $true
AddMenu 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Drive\shell\WindowsTerminalContextMenu-Elevated' $true
AddMenu 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\LibraryFolder\Background\shell\WindowsTerminalContextMenu-Elevated' $true
}
}
if ($Layout -ne 'Minimal') {
$wtProfiles = GetActiveProfiles
for ($index = 0; $index -lt $wtProfiles.Count; ++$index) {
AddMenuItemForProfile $wtProfiles[$index] $index
}
}
}
$Storage = "$env:LocalAppData\WindowsTerminalContextMenu"
if (Test-Path "$Storage\layout") {
Write-Error 'The context menus already exists'
exit 1
}
if (-not (Test-Path $Storage)) {
New-Item -Path $Storage -ItemType Directory | Out-Null
}
$Edition, $InstallDir = GetInstallationInfo
$Settings = GetTerminalSettings
$Translations = GetTranslations
$TerminalIcon = ExtractTerminalIcon
Write-Output $Layout > "$Storage\layout"
GenerateLaunchScript
CreateMenus
Write-Host 'Done'
exit 0