-
Notifications
You must be signed in to change notification settings - Fork 13
/
WTToolBox.psm1
133 lines (110 loc) · 5.39 KB
/
WTToolBox.psm1
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
#region main code
#Turn on Verbose output if module was imported with -Verbose
if ($myinvocation.line -match "Verbose") {
$VerbosePreference = "Continue"
}
if ($IsWindows -OR $PSEdition -eq 'Desktop') {
Write-Verbose "Dot source the module script files"
(Get-ChildItem $PSScriptRoot\functions\*.ps1).foreach( { .$_.fullname })
Write-Verbose "Testing for Microsoft.WindowsTerminal"
<#
need to take into account that a user might have a preview version also installed
of maybe they are only using the preview version.
8/1/2020 jdh
#>
#use the settings of the currently running Windows Terminal
<#
Sept. 22, 2020 JH
PowerShell 7.1 previews are based on a newer version of .NET Core which breaks the AppX cmdlets. I'll use remoting to Windows PowerShell.
#>
if ($PSVersionTable.PSVersion.ToString() -match "^7\.[1-9]") {
Write-Verbose "PowerShell 7.x detected. Using implicit remoting to get the Appx package."
$app = Invoke-Command -ScriptBlock { Get-AppxPackage Microsoft.WindowsTerminal* } -ConfigurationName Microsoft.PowerShell -ComputerName localhost
}
else {
$app = Get-AppxPackage Microsoft.WindowsTerminal*
}
if ($app) {
Write-Verbose "Windows Terminal is installed"
Write-Verbose "Testing for a current WindowsTerminal Process"
$wt = Get-CimInstance -ClassName Win32_process -Filter "ProcessID=$pid"
#validate the parent process is Windows Terminal
$parent = Get-Process -Id $wt.parentProcessID
#modified to take into account PowerShell previews which launch from a cmd script 12/18/2020 jdh
if ($parent.processname -match "WindowsTerminal" -OR $parent.parent.processname -match "WindowsTerminal") {
Write-Verbose "Testing for settings.json"
<#
#build path from process path
$wtPath = Split-Path -Path $parent.path
#pull the release name from $wtpath -parsing out version number
$release = Split-Path -path $wtpath -leaf
#$trimmed = $Release -replace "_.*_", "_"
#>
#The application name should be static 12/18/2020 jdh
#need to account if using Preview 12/21/2020 jdh (Issue #13)
if ($parent.path -match "WindowsTerminalPreview") {
$jsonPath = "$ENV:Userprofile\AppData\Local\Packages\Microsoft.WindowsTerminalPreview*_8wekyb3d8bbwe\LocalState\settings.json"
}
else {
$jsonPath = "$ENV:Userprofile\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
}
Write-Verbose "Using path $jsonPath"
If (Test-Path $jsonPath) {
#Export settings path to a global variable
Write-Verbose "Creating settings variable"
$global:WTSettingsPath = $jsonPath
#create a custom object with the settings.json values saved as $WTSettings
AddWTSettingsVariable
Write-Verbose "Creating a global variable with defaults"
$defaults = Join-Path -Path $app.installLocation -ChildPath defaults.json
$global:WTDefaultsPath = $defaults
#need to account for preview and stable releases
$global:WTDefaults = $defaults.foreach( {
$wtPath = $_
(Get-Content -Path $_).where( { $_ -notmatch "(\/{2})(?=\s+)" }) | ConvertFrom-Json |
Add-Member -MemberType NoteProperty -Name DefaultPath -Value $wtPath -PassThru
})
} #if json file is found
else {
Write-Verbose "Failed to find $jsonPath"
}
} #if parent process is WT
else {
Write-Verbose "Failed to find WindowsTerminal as the parent process."
}
} #if $app
else {
Write-Warning "Windows Terminal was not found on this system so not all commands in this module will work."
}
} #if Windows
else {
Write-Warning "This module requires a Windows platform."
}
#reset -Verbose if turned on
if ($VerbosePreference -eq 'Continue') {
Write-Verbose "Resetting verbose preference"
$VerbosePreference = "SilentlyContinue"
}
#endregion
#region completers
Register-ArgumentCompleter -CommandName Get-WTProfile -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
#PowerShell code to populate $wordtoComplete
$global:wtsettings.profiles.list |
where-object {$_.name -like "*$wordtoComplete*"} |
ForEach-Object {
# completion text,listitem text,result type,Tooltip
[System.Management.Automation.CompletionResult]::new("'$($_.name)'", $_.name, 'ParameterValue', $(if ($_.hidden) {"Hidden"} else {"Listed"}))
}
}
Register-ArgumentCompleter -CommandName Get-WTColorScheme -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
#PowerShell code to populate $wordtoComplete
$global:wtsettings.schemes |
where-object {$_.name -like "*$wordtoComplete*"} |
ForEach-Object {
# completion text,listitem text,result type,Tooltip
[System.Management.Automation.CompletionResult]::new("'$($_.name)'", $_.name, 'ParameterValue', $_.name)
}
}
#endregion