forked from microsoft/microsoft-ui-xaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStripConditionalXaml.ps1
189 lines (157 loc) · 6.01 KB
/
StripConditionalXaml.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE in the project root for license information.
Param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$ThemeResourceInputFile,
[string]$ThemeResourceOutputFile,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateSet('RS1', 'RS2', 'RS3', 'RS4', 'RS5', '19H1', 'OS')]
[String]$ForVersion
)
# exit whenever see a exception in the script
trap
{
write-output $_
exit 1
}
$ForVersion = $ForVersion.ToUpper()
$ApiContractMapping = @{RS1=3; RS2=4; RS3=5; RS4=6; RS5=7; '19H1'=8} # OS always max+1
$ApiContractMaxVersion = ($ApiContractMapping.Values | measure -Maximum).Maximum
$ApiContractMapping['OS'] = $ApiContractMaxVersion + 1
function Get-ApiContractVersion
{
Param($key)
if ($ApiContractMapping.ContainsKey($key))
{
return $ApiContractMapping[$key]
}
Write-Error "Can't find Key: " + $key
eixt 2
}
function Build-ApiContractPresentPrefix
{
Param($apiVersion)
return [string]::Format('contract{0}Present', $apiVersion)
}
function Build-ApiContractNotPresentPrefix
{
Param($apiVersion)
return [string]::Format('contract{0}NotPresent', $apiVersion)
}
function Build-MuxOnlyPrefix
{
return 'MUXOnly';
}
# xmlDocument.Load/Save converted  to uncode character and it's hard to recover it to the orignal string
# replace the "escape characters" in the form  in loading and convert them back in saving
function Get-XmlFile
{
Param($file)
return (Get-Content $file -Raw).Replace("&", "&")
}
function Save-XmlFile
{
Param($file,$content)
$content.Replace("&","&") |Set-Content $file
}
function Remove-ElementsOrPropertiesRelatedToNamespacePrefixes
{
Param($content, $prefixesToRemove)
foreach ($prefixToRemove in $prefixesToRemove)
{
# xmlns:contract5Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,5)"
$content = $content -replace "(?m)xmlns:$prefixToRemove=`"[^`"]+`"", ""
# <Rectangle x:Name="colorRectangle" Width="200" Height="200"
# contract5NotPresent:Fill="{x:Bind ((SolidColorBrush)((FrameworkElement)colorComboBox.SelectedItem).Tag), Mode=OneWay}">
#
$content = $content -replace "(?m)\s$prefixToRemove\:\w+?=`"[^`"]+`"", ""
# <contract5Present:ColorPicker x:Name="colorPicker"
# Grid.Column="1"
# VerticalAlignment="Center"/>
$content = $content -replace "(?m)<$prefixToRemove\:[^>]*/>",""
# <contract5NotPresent:ComboBox x:Name="colorComboBox"
# PlaceholderText="Pick a color"
# Grid.Column="1"
# VerticalAlignment="Center">
# </contract5NotPresent:ComboBox>
$content = $content -replace "(?m)<$prefixToRemove\:[.\S\s]*?</$prefixToRemove\:[^>]*>",""
}
return $content
}
function Remove-NamespacePrefixes
{
Param($content, $prefixesToRemove)
foreach ($prefixToRemove in $prefixesToRemove)
{
# xmlns:contract5Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,5)"
$content = $content -replace "(?m)xmlns:$prefixToRemove=`"[^`"]+`"", ""
# <contract5NotPresent:ComboBox
# ...
# </contract5NotPresent:ComboBox>
$content = $content -replace "$prefixToRemove\:", ""
}
return $content
}
function Main
{
Write-Host "StripConditionalXaml $ThemeResourceInputFile for $ForVersion"
if (!(Test-Path $ThemeResourceInputFile))
{
Write-Error "File $ThemeResourceInputFile doesn't exist"
exit 1
}
if (!$ThemeResourceOutputFile)
{
$ThemeResourceOutputFile = $ThemeResourceInputFile
}
if (Test-Path $ThemeResourceOutputFile)
{
Write-Host "File $ThemeResourceOutputFile is overwritten"
}
$curVersion = Get-ApiContractVersion $ForVersion
# generate prefixes which nodes or properties will be removed
$prefixesForNodesOrPropertiesToBeRemoved = New-Object System.Collections.ArrayList
# ContractPresent for release > cur
for ($i = $curVersion + 1; $i -le $ApiContractMaxVersion; $i++)
{
$prefixesForNodesOrPropertiesToBeRemoved.Add((Build-ApiContractPresentPrefix $i)) | Out-Null;
}
# NotContractPresent for release <= cur
for ($i = 3; $i -le $curVersion; $i++)
{
$prefixesForNodesOrPropertiesToBeRemoved.Add((Build-ApiContractNotPresentPrefix $i)) | Out-Null;
}
# generate prefixes which will be replaced with empty
$prefixesToBeRemoved = New-Object System.Collections.ArrayList
# ContractPresent for release <= cur
for ($i = 3; $i -le $curVersion; $i++)
{
$prefixesToBeRemoved.Add((Build-ApiContractPresentPrefix $i)) | Out-Null;
}
# NotContractPresent for release > cur
for ($i = $curVersion + 1; $i -le $ApiContractMaxVersion; $i++)
{
$prefixesToBeRemoved.Add((Build-ApiContractNotPresentPrefix $i)) | Out-Null;
}
# In Os side, MUXOnly
$MUXOnlyNamespacePrefix = Build-MuxOnlyPrefix
if ("OS" -ne $ForVersion)
{
$prefixesToBeRemoved.Add($MUXOnlyNamespacePrefix) | Out-Null;
}
else
{
$prefixesForNodesOrPropertiesToBeRemoved.Add($MUXOnlyNamespacePrefix) | Out-Null;
}
Write-Host "Nodes or properties related to below prefixes will be removed"
write-Host -Separator " " " " $prefixesForNodesOrPropertiesToBeRemoved
Write-Host "Below prefixes will be removed"
write-Host -Separator " " " " $prefixesToBeRemoved
$content = Get-XmlFile $ThemeResourceInputFile
$content = Remove-ElementsOrPropertiesRelatedToNamespacePrefixes $content $prefixesForNodesOrPropertiesToBeRemoved
$content = Remove-NamespacePrefixes $content $prefixesToBeRemoved
Save-XmlFile $ThemeResourceOutputFile $content
}
Main