-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply_global.ps1
More file actions
133 lines (111 loc) · 4.53 KB
/
Copy pathapply_global.ps1
File metadata and controls
133 lines (111 loc) · 4.53 KB
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
param(
[switch]$Yes
)
$ErrorActionPreference = "Stop"
$ProjectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$SnapshotsDir = Join-Path $ProjectRoot "snapshots"
$GeneratedShader = Join-Path $ProjectRoot "shaders\retrocore.generated.hlsl"
$FallbackShader = Join-Path $ProjectRoot "shaders\crt-subtle.hlsl"
$LocalStateDir = Join-Path $env:LOCALAPPDATA "Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState"
$SettingsPath = Join-Path $LocalStateDir "settings.json"
$TargetShaderName = "retrocore.generated.hlsl"
$TargetShaderPath = Join-Path $LocalStateDir $TargetShaderName
function Ask-YesNo {
param(
[string]$Prompt,
[bool]$Default = $true
)
if ($Yes) {
return $true
}
$suffix = if ($Default) { "[Y/n]" } else { "[y/N]" }
$response = Read-Host "$Prompt $suffix"
if ([string]::IsNullOrWhiteSpace($response)) {
return $Default
}
switch ($response.Trim().ToLowerInvariant()) {
"y" { return $true }
"yes" { return $true }
"n" { return $false }
"no" { return $false }
default {
Write-Host "Please answer y or n." -ForegroundColor Yellow
return Ask-YesNo -Prompt $Prompt -Default $Default
}
}
}
function Ensure-ObjectProperty {
param(
[Parameter(Mandatory = $true)][object]$Object,
[Parameter(Mandatory = $true)][string]$Name
)
$property = $Object.PSObject.Properties[$Name]
if ($property) {
if ($null -eq $property.Value) {
$property.Value = [pscustomobject]@{}
}
return $property.Value
}
$child = [pscustomobject]@{}
$Object | Add-Member -NotePropertyName $Name -NotePropertyValue $child
return $child
}
function Set-ObjectProperty {
param(
[Parameter(Mandatory = $true)][object]$Object,
[Parameter(Mandatory = $true)][string]$Name,
[Parameter(Mandatory = $true)][object]$Value
)
$property = $Object.PSObject.Properties[$Name]
if ($property) {
$property.Value = $Value
}
else {
$Object | Add-Member -NotePropertyName $Name -NotePropertyValue $Value
}
}
function ConvertFrom-JsonC {
param(
[Parameter(Mandatory = $true)][string]$Text
)
$withoutBlockComments = [regex]::Replace($Text, '/\*.*?\*/', '', [System.Text.RegularExpressions.RegexOptions]::Singleline)
$withoutLineComments = [regex]::Replace($withoutBlockComments, '(?m)(?<!https:)(?<!http:)//.*$', '')
$withoutTrailingCommas = [regex]::Replace($withoutLineComments, ',(\s*[\}\]])', '$1')
return $withoutTrailingCommas | ConvertFrom-Json
}
if (Test-Path -LiteralPath $GeneratedShader) {
$SourceShader = $GeneratedShader
$SourceKind = "generated Retrocore shader"
}
elseif (Test-Path -LiteralPath $FallbackShader) {
$SourceShader = $FallbackShader
$SourceKind = "fallback shader snapshot"
}
else {
throw "No source shader was found in the project."
}
if (-not (Test-Path -LiteralPath $SettingsPath)) {
throw "Windows Terminal settings were not found at $SettingsPath"
}
Write-Host "Retrocore standalone global apply" -ForegroundColor Green
Write-Host "This will copy a shader into Windows Terminal LocalState and enable it globally through profiles.defaults." -ForegroundColor DarkGray
Write-Host "Source shader: $SourceShader ($SourceKind)" -ForegroundColor DarkGray
Write-Host "Target shader: $TargetShaderPath" -ForegroundColor DarkGray
if (-not (Ask-YesNo -Prompt "Apply Retrocore globally now?" -Default $true)) {
Write-Host "No changes were made." -ForegroundColor Yellow
exit 0
}
New-Item -ItemType Directory -Force -Path $LocalStateDir, $SnapshotsDir | Out-Null
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$backupPath = Join-Path $SnapshotsDir "settings.standalone.apply.$timestamp.json"
Copy-Item -LiteralPath $SettingsPath -Destination $backupPath -Force
Copy-Item -LiteralPath $SourceShader -Destination $TargetShaderPath -Force
$settings = ConvertFrom-JsonC -Text (Get-Content -LiteralPath $SettingsPath -Raw)
$profiles = Ensure-ObjectProperty -Object $settings -Name "profiles"
$defaults = Ensure-ObjectProperty -Object $profiles -Name "defaults"
Set-ObjectProperty -Object $defaults -Name "experimental.pixelShaderPath" -Value $TargetShaderName
$settings | ConvertTo-Json -Depth 100 | Set-Content -LiteralPath $SettingsPath -Encoding UTF8
Write-Host ""
Write-Host "Retrocore was applied globally." -ForegroundColor Green
Write-Host "Settings backup: $backupPath" -ForegroundColor DarkGray
Write-Host "Restart Windows Terminal to see the change." -ForegroundColor DarkGray