forked from lextm/windowsterminal-shell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.ps1
469 lines (404 loc) · 19.6 KB
/
install.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#Requires -RunAsAdministrator
#Requires -Version 6
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[ValidateSet('Default', 'Flat', 'Mini')]
[string] $Layout = 'Default',
[Parameter()]
[switch] $PreRelease
)
function Generate-HelperScript(
# The cache folder
[Parameter(Mandatory=$true)]
[string]$cache)
{
$content =
"Set shell = WScript.CreateObject(`"Shell.Application`")
executable = WSCript.Arguments(0)
folder = WScript.Arguments(1)
If Wscript.Arguments.Count > 2 Then
profile = WScript.Arguments(2)
' 0 at the end means to run this command silently
shell.ShellExecute `"powershell`", `"Start-Process \`"`"`" & executable & `"\`"`" -ArgumentList \`"`"-p \`"`"\`"`"`" & profile & `"\`"`"\`"`" -d \`"`"\`"`"`" & folder & `"\`"`"\`"`" \`"`" `", `"`", `"runas`", 0
Else
' 0 at the end means to run this command silently
shell.ShellExecute `"powershell`", `"Start-Process \`"`"`" & executable & `"\`"`" -ArgumentList \`"`"-d \`"`"\`"`"`" & folder & `"\`"`"\`"`" \`"`" `", `"`", `"runas`", 0
End If
"
Set-Content -Path "$cache/helper.vbs" -Value $content
}
# https://github.com/Duffney/PowerShell/blob/master/FileSystems/Get-Icon.ps1
Function Get-Icon {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True, Position=1, HelpMessage="Enter the location of the .EXE file")]
[string]$File,
# If provided, will output the icon to a location
[Parameter(Position=1, ValueFromPipelineByPropertyName=$true)]
[string]$OutputFile
)
[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing') | Out-Null
[System.Drawing.Icon]::ExtractAssociatedIcon($File).ToBitmap().Save($OutputFile)
}
# https://gist.github.com/darkfall/1656050
function ConvertTo-Icon
{
<#
.Synopsis
Converts image to icons
.Description
Converts an image to an icon
.Example
ConvertTo-Icon -File .\Logo.png -OutputFile .\Favicon.ico
#>
[CmdletBinding()]
param(
# The file
[Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
[Alias('Fullname')]
[string]$File,
# If provided, will output the icon to a location
[Parameter(Position=1, ValueFromPipelineByPropertyName=$true)]
[string]$OutputFile
)
begin {
Add-Type -AssemblyName System.Drawing
}
process {
#region Load Icon
$resolvedFile = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($file)
if (-not $resolvedFile) { return }
$inputBitmap = [Drawing.Image]::FromFile($resolvedFile)
$width = $inputBitmap.Width
$height = $inputBitmap.Height
$size = New-Object Drawing.Size $width, $height
$newBitmap = New-Object Drawing.Bitmap $inputBitmap, $size
#endregion Load Icon
#region Icon Size bound check
if ($width -gt 255 -or $height -gt 255) {
$ratio = ($height, $width | Measure-Object -Maximum).Maximum / 255
$width /= $ratio
$height /= $ratio
}
#endregion Icon Size bound check
#region Save Icon
$memoryStream = New-Object System.IO.MemoryStream
$newBitmap.Save($memoryStream, [System.Drawing.Imaging.ImageFormat]::Png)
$resolvedOutputFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($outputFile)
$output = [IO.File]::Create("$resolvedOutputFile")
$iconWriter = New-Object System.IO.BinaryWriter($output)
# 0-1 reserved, 0
$iconWriter.Write([byte]0)
$iconWriter.Write([byte]0)
# 2-3 image type, 1 = icon, 2 = cursor
$iconWriter.Write([short]1);
# 4-5 number of images
$iconWriter.Write([short]1);
# image entry 1
# 0 image width
$iconWriter.Write([byte]$width);
# 1 image height
$iconWriter.Write([byte]$height);
# 2 number of colors
$iconWriter.Write([byte]0);
# 3 reserved
$iconWriter.Write([byte]0);
# 4-5 color planes
$iconWriter.Write([short]0);
# 6-7 bits per pixel
$iconWriter.Write([short]32);
# 8-11 size of image data
$iconWriter.Write([int]$memoryStream.Length);
# 12-15 offset of image data
$iconWriter.Write([int](6 + 16));
# write image data
# png data must contain the whole png data file
$iconWriter.Write($memoryStream.ToArray());
$iconWriter.Flush();
$output.Close()
#endregion Save Icon
#region Cleanup
$memoryStream.Dispose()
$newBitmap.Dispose()
$inputBitmap.Dispose()
#endregion Cleanup
}
}
function GetProgramFilesFolder(
[Parameter(Mandatory=$true)]
[bool]$includePreview)
{
$root = "$Env:ProgramFiles\WindowsApps"
$versionFolders = (Get-ChildItem $root | Where-Object {
if ($includePreview) {
$_.Name -like "Microsoft.WindowsTerminal_*__*" -or
$_.Name -like "Microsoft.WindowsTerminalPreview_*__*"
} else {
$_.Name -like "Microsoft.WindowsTerminal_*__*"
}
})
$foundVersion = $null
$result = $null
foreach ($versionFolder in $versionFolders) {
if ($versionFolder.Name -match "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") {
$version = [version]$Matches.0
Write-Host "Found Windows Terminal version $version."
if ($null -eq $foundVersion -or $version -gt $foundVersion) {
$foundVersion = $version
$result = $versionFolder.FullName
}
} else {
Write-Warning "Found Windows Terminal unsupported version in $versionFolder."
}
}
if ($null -eq $result) {
Write-Error "Failed to find Windows Terminal actual folder under $root. To install menu items for Windows Terminal Preview, run with ""-Prerelease"" switch Exit."
exit 1
}
if ($foundVersion -lt [version]"0.11") {
Write-Warning "The latest version found is less than 0.11, which is not tested. The install script might fail in certain way."
}
return $result
}
function GetWindowsTerminalIcon(
[Parameter(Mandatory=$true)]
[string]$folder,
[Parameter(Mandatory=$true)]
[string]$localCache)
{
$icon = "$localCache\wt.ico"
$actual = $folder + "\WindowsTerminal.exe"
if (Test-Path $actual) {
# use app icon directly.
Write-Host "Found actual executable $actual."
$temp = "$localCache\wt.png"
Get-Icon -File $actual -OutputFile $temp
ConvertTo-Icon -File $temp -OutputFile $icon
} else {
# download from GitHub
Write-Warning "Didn't find actual executable $actual so download icon from GitHub."
Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/terminal/master/res/terminal.ico" -OutFile $icon
}
return $icon
}
function GetActiveProfiles(
[Parameter(Mandatory=$true)]
[bool]$isPreview)
{
if ($isPreview) {
$file = "$env:LocalAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState\settings.json"
} else {
$file = "$env:LocalAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
}
if (-not (Test-Path $file)) {
Write-Error "Couldn't find profiles. Please run Windows Terminal at least once after installing it. Exit."
exit 1
}
$settings = Get-Content $file | Out-String | ConvertFrom-Json
if ($settings.profiles.PSObject.Properties.name -match "list") {
$list = $settings.profiles.list
} else {
$list = $settings.profiles
}
return $list | Where-Object { -not $_.hidden} | Where-Object { ($null -eq $_.source) -or -not ($settings.disabledProfileSources -contains $_.source) }
}
function GetProfileIcon (
[Parameter(Mandatory=$true)]
$profile,
[Parameter(Mandatory=$true)]
[string]$folder,
[Parameter(Mandatory=$true)]
[string]$localCache,
[Parameter(Mandatory=$true)]
[string]$defaultIcon,
[Parameter(Mandatory=$true)]
[bool]$isPreview)
{
$guid = $profile.guid
$name = $profile.name
$result = $null
$profilePng = $null
$icon = $profile.icon
if ($null -ne $icon) {
if (Test-Path $icon) {
# use user setting
$profilePng = $icon
} elseif ($profile.icon -like "ms-appdata:///Roaming/*") {
#resolve roaming cache
if ($isPreview) {
$profilePng = $icon -replace "ms-appdata:///Roaming", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\RoamingState" -replace "/", "\"
} else {
$profilePng = $icon -replace "ms-appdata:///Roaming", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState" -replace "/", "\"
}
} elseif ($profile.icon -like "ms-appdata:///Local/*") {
#resolve local cache
if ($isPreview) {
$profilePng = $icon -replace "ms-appdata:///Local", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState" -replace "/", "\"
} else {
$profilePng = $icon -replace "ms-appdata:///Local", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState" -replace "/", "\"
}
} elseif ($profile.icon -like "ms-appx:///*") {
# resolve app cache
$profilePng = $icon -replace "ms-appx://", $folder -replace "/", "\"
} elseif ($profile.icon -like "*%*") {
$profilePng = [System.Environment]::ExpandEnvironmentVariables($icon)
} else {
Write-Host "Invalid profile icon found $icon. Please report an issue at https://github.com/lextm/windowsterminal-shell/issues ."
}
}
if (($null -eq $profilePng) -or -not (Test-Path $profilePng)) {
# fallback to profile PNG
$profilePng = "$folder\ProfileIcons\$guid.scale-200.png"
if (-not (Test-Path($profilePng))) {
if ($profile.source -eq "Windows.Terminal.Wsl") {
$profilePng = "$folder\ProfileIcons\{9acb9455-ca41-5af7-950f-6bca1bc9722f}.scale-200.png"
}
}
}
if (Test-Path $profilePng) {
if ($profilePng -like "*.png") {
# found PNG, convert to ICO
$result = "$localCache\$guid.ico"
ConvertTo-Icon -File $profilePng -OutputFile $result
} elseif ($profilePng -like "*.ico") {
$result = $profilePng
} else {
Write-Warning "Icon format is not supported by this script $profilePng. Please use PNG or ICO format."
}
} else {
Write-Warning "Didn't find icon for profile $name."
}
if ($null -eq $result) {
# final fallback
$result = $defaultIcon
}
return $result
}
function CreateMenuItem(
[Parameter(Mandatory=$true)]
[string]$rootKey,
[Parameter(Mandatory=$true)]
[string]$name,
[Parameter(Mandatory=$true)]
[string]$icon,
[Parameter(Mandatory=$true)]
[string]$command,
[Parameter(Mandatory=$true)]
[bool]$elevated
)
{
New-Item -Path $rootKey -Force | Out-Null
New-ItemProperty -Path $rootKey -Name 'MUIVerb' -PropertyType String -Value $name | Out-Null
New-ItemProperty -Path $rootKey -Name 'Icon' -PropertyType String -Value $icon | Out-Null
if ($elevated) {
New-ItemProperty -Path $rootKey -Name 'HasLUAShield' -PropertyType String -Value '' | Out-Null
}
New-Item -Path "$rootKey\command" -Force | Out-Null
New-ItemProperty -Path "$rootKey\command" -Name '(Default)' -PropertyType String -Value $command | Out-Null
}
function CreateProfileMenuItems(
[Parameter(Mandatory=$true)]
$profile,
[Parameter(Mandatory=$true)]
[string]$executable,
[Parameter(Mandatory=$true)]
[string]$folder,
[Parameter(Mandatory=$true)]
[string]$localCache,
[Parameter(Mandatory=$true)]
[string]$icon,
[Parameter(Mandatory=$true)]
[string]$layout,
[Parameter(Mandatory=$true)]
[bool]$isPreview)
{
$guid = $profile.guid
$name = $profile.name
$command = """$executable"" -p ""$name"" -d ""%V."""
$elevated = "wscript.exe ""$localCache/helper.vbs"" ""$executable"" ""%V."" ""$name"""
$profileIcon = GetProfileIcon $profile $folder $localCache $icon $isPreview
if ($layout -eq "Default") {
$rootKey = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell\$guid"
$rootKeyElevated = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell\$guid"
CreateMenuItem $rootKey $name $profileIcon $command $false
CreateMenuItem $rootKeyElevated $name $profileIcon $elevated $true
} elseif ($layout -eq "Flat") {
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true
}
}
function CreateMenuItems(
[Parameter(Mandatory=$true)]
[string]$executable,
[Parameter(Mandatory=$true)]
[string]$layout,
[Parameter(Mandatory=$true)]
[bool]$includePreview)
{
$folder = GetProgramFilesFolder $includePreview
$localCache = "$Env:LOCALAPPDATA\Microsoft\WindowsApps\Cache"
if (-not (Test-Path $localCache)) {
New-Item $localCache -ItemType Directory | Out-Null
}
Generate-HelperScript $localCache
$icon = GetWindowsTerminalIcon $folder $localCache
if ($layout -eq "Default") {
# defaut layout creates two menus
New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Force | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null
New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Force | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null
New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell' -Force | Out-Null
New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Force | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null
New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Force | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null
New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell' -Force | Out-Null
} elseif ($layout -eq "Mini") {
$command = """$executable"" -d ""%V."""
$elevated = "wscript.exe ""$localCache/helper.vbs"" ""$executable"" ""%V."""
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true
return
}
$isPreview = $folder -like "*WindowsTerminalPreview*"
$profiles = GetActiveProfiles $isPreview
foreach ($profile in $profiles) {
CreateProfileMenuItems $profile $executable $folder $localCache $icon $layout $isPreview
}
}
# Based on @nerdio01's version in https://github.com/microsoft/terminal/issues/1060
if ((Get-Process -Id $pid).Path -like "*WindowsApps*") {
Write-Error "PowerShell installed via Microsoft Store is not supported. Learn other ways to install it from https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows?view=powershell-7 . Exit.";
exit 1
}
if ((Test-Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\MenuTerminal") -and
-not (Test-Path "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal")) {
Write-Error "Please execute uninstall.old.ps1 to remove previous installation."
exit 1
}
if ($PSVersionTable.PSVersion.Major -lt 6) {
Write-Error "Must be executed in PowerShell 6 and above. Learn how to install it from https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows?view=powershell-7 . Exit."
exit 1
}
$executable = "$Env:LOCALAPPDATA\Microsoft\WindowsApps\wt.exe"
if (-not (Test-Path $executable)) {
Write-Error "Windows Terminal not detected at $executable. Learn how to install it from https://github.com/microsoft/terminal (via Microsoft Store is recommended). Exit."
exit 1
}
Write-Host "Use $Layout layout."
CreateMenuItems $executable $Layout $PreRelease
Write-Host "Windows Terminal installed to Windows Explorer context menu."