-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdd-WTProfile.ps.ps1
106 lines (100 loc) · 4.1 KB
/
Add-WTProfile.ps.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
function Add-WTProfile
{
<#
.Synopsis
Adds a new Windows Terminal profile
.Description
Adds a new Windows Terminal tab profile.
.Link
New-WTProfile
.Example
Add-WTProfile -Name kali-linux-pwsh -CommandLine 'wsl -d kali-linux -e pwsh'
#>
[OutputType('Nullable','WindowsTerminal.Profile')]
[CmdletBinding(SupportsShouldProcess)]
[JSONSchema(SchemaUri='https://aka.ms/terminal-profiles-schema#Profile',RemovePropertyPrefix='Experimental')]
param(
# Name of the profile. Displays in the dropdown menu.
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Alias('ProfileName')]
[ComponentModel.DefaultBindingProperty("name")]
[string]
$Name
)
begin {
$myParameters = $MyInvocation.MyCommand.Parameters.Values
$myProfileProperties = :nextParam foreach ($param in $myParameters) {
foreach ($attr in $param.Attributes) {
if ($attr -is [ComponentModel.DefaultBindingPropertyAttribute]) {
$param
continue nextParam
}
}
}
}
process {
$myParameters = [Ordered]@{} + $PSBoundParameters
if (-not $myParameters["Guid"]) {
$myParameters["Guid"] = "{$([GUID]::NewGuid())}"
}
$myProfileParameters = [Ordered]@{}
foreach ($myProfileProperty in $myProfileProperties) {
if ($myParameters.Contains($myProfileProperty.Name)) {
$propertyName = foreach ($attr in $myProfileProperty.Attributes) {
if ($attr -is [System.ComponentModel.DefaultBindingPropertyAttribute]) {
$attr.Name
break
}
}
$paramValue = $myParameters[$myProfileProperty.Name]
foreach ($attr in $myProfileProperty.Attributes) {
if ($attr -is [ComponentModel.AmbientValueAttribute] -and $attr.Value -is [scriptblock]) {
$_ = $this = $PSItem = $paramValue
$paramValue = & $attr.Value
break
}
if ($attr -is [ValidateSet]) {
foreach ($validValue in $attr.Validvalues) {
if ($paramValue -eq $validValue) {
$paramValue = $validValue
}
}
}
}
if ($paramValue -is [switch]) {
$paramValue = $paramValue -as [bool]
}
$myProfileParameters[$propertyName] = $paramValue
}
}
#region Create Profile Object
$newProfile = [PSCustomObject]$myProfileParameters
#endregion Profile Object
#region Update Windows Terminal Profile
$wtProfile = Get-WTProfile -Global # Get our profile
$wtProfile.profiles | # Add out new profile to the list,
Add-Member NoteProperty list @(
foreach ($_ in $wtProfile.profiles.list) {
if ($guid -ne $_.Guid -and $name -ne $_.Name) # and don't forget all of the other ones.
{
$_
}
}
$newProfile
) -Force
$wtPath = $wtProfile.Path
if ($PSCmdlet.ShouldProcess("Add Profile $Name to $wtPath")) # If we ShouldProcess
{
$wtProfile.psobject.properties.Remove('Path')
$wtProfile | # turn the profile
ConvertTo-Json -Depth 100 | # back into JSON
Set-Content -Path $wtPath -Encoding UTF8 # and write it to disk.
$wtProfile.psobject.properties.add([PSNoteProperty]::new('Path', $wtPath))
}
elseif ($WhatIfPreference) # if we wanted to know -WhatIf
{
return $wtProfile # return the JSON object
}
#endregion Update Windows Terminal Profile
}
}