forked from microsoft/terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generate-FeatureStagingHeader.ps1
206 lines (164 loc) · 5.86 KB
/
Generate-FeatureStagingHeader.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
################################################################################
# This script generates a header describing which Terminal/Console features
# should be compiled-in, based on an XML document describing them.
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$True)]
[ValidateScript({ Test-Path $_ })]
[string]$Path,
[ValidateSet("Dev", "Preview", "Release", "WindowsInbox")]
[string]$Branding = "Dev",
[string]$BranchOverride = $Null,
[string]$OutputPath
)
Enum Stage {
AlwaysDisabled;
AlwaysEnabled;
}
Function ConvertTo-FeatureStage([string]$stage) {
Switch($stage) {
"AlwaysEnabled" { [Stage]::AlwaysEnabled; Return }
"AlwaysDisabled" { [Stage]::AlwaysDisabled; Return }
}
Throw "Invalid feature stage $stage"
}
Class Feature {
[string]$Name
[Stage]$Stage
[System.Collections.Generic.Dictionary[string, Stage]]$BranchTokenStages
[System.Collections.Generic.Dictionary[string, Stage]]$BrandingTokenStages
[bool]$DisabledReleaseToken
Feature([System.Xml.XmlElement]$entry) {
$this.Name = $entry.name
$this.Stage = ConvertTo-FeatureStage $entry.stage
$this.BranchTokenStages = [System.Collections.Generic.Dictionary[string, Stage]]::new()
$this.BrandingTokenStages = [System.Collections.Generic.Dictionary[string, Stage]]::new()
$this.DisabledReleaseToken = $Null -Ne $entry.alwaysDisabledReleaseTokens
ForEach ($b in $entry.alwaysDisabledBranchTokens.branchToken) {
$this.BranchTokenStages[$b] = [Stage]::AlwaysDisabled
}
# AlwaysEnabled branches win over AlwaysDisabled branches
ForEach ($b in $entry.alwaysEnabledBranchTokens.branchToken) {
$this.BranchTokenStages[$b] = [Stage]::AlwaysEnabled
}
ForEach ($b in $entry.alwaysDisabledBrandingTokens.brandingToken) {
$this.BrandingTokenStages[$b] = [Stage]::AlwaysDisabled
}
# AlwaysEnabled brandings win over AlwaysDisabled brandings
ForEach ($b in $entry.alwaysEnabledBrandingTokens.brandingToken) {
$this.BrandingTokenStages[$b] = [Stage]::AlwaysEnabled
}
}
[string] PreprocessorName() {
return "TIL_$($this.Name.ToUpper())_ENABLED"
}
}
class FeatureComparer : System.Collections.Generic.IComparer[Feature] {
[int] Compare([Feature]$a, [Feature]$b) {
If ($a.Name -lt $b.Name) {
Return -1
} ElseIf ($a.Name -gt $b.Name) {
Return 1
} Else {
Return 0
}
}
}
Function Resolve-FinalFeatureStage {
Param(
[Feature]$Feature,
[string]$Branch,
[string]$Branding
)
# RELEASE=DISABLED wins all checks
# Then, branch match by most-specific branch
# Then, branding type (if no overriding branch match)
If ($Branding -Eq "Release" -And $Feature.DisabledReleaseToken) {
[Stage]::AlwaysDisabled
Return
}
If (-Not [String]::IsNullOrEmpty($Branch)) {
$lastMatchLen = 0
$branchStage = $Null
ForEach ($branchToken in $Feature.BranchTokenStages.Keys) {
# Match the longest branch token -- it should be the most specific
If ($Branch -Like $branchToken -And $branchToken.Length -Gt $lastMatchLen) {
$lastMatchLen = $branchToken.Length
$branchStage = $Feature.BranchTokenStages[$branchToken]
}
}
If ($Null -Ne $branchStage) {
$branchStage
Return
}
}
$BrandingStage = $Feature.BrandingTokenStages[$Branding]
If ($Null -Ne $BrandingStage) {
$BrandingStage
Return
}
$Feature.Stage
}
$ErrorActionPreference = "Stop"
$x = [xml](Get-Content $Path -EA:Stop)
$x.Schemas.Add('http://microsoft.com/TilFeatureStaging-Schema.xsd', (Resolve-Path (Join-Path $PSScriptRoot "FeatureStagingSchema.xsd")).Path) | Out-Null
$x.Validate($null)
$featureComparer = [FeatureComparer]::new()
$features = [System.Collections.Generic.List[Feature]]::new(16)
ForEach ($entry in $x.featureStaging.feature) {
$features.Add([Feature]::new($entry))
}
$features.Sort($featureComparer)
$featureFinalStages = [System.Collections.Generic.Dictionary[string, Stage]]::new(16)
$branch = $BranchOverride
If ([String]::IsNullOrEmpty($branch)) {
Try {
$branch = & git branch --show-current 2>$Null
} Catch {
Try {
$branch = & git rev-parse --abbrev-ref HEAD 2>$Null
} Catch {
Write-Verbose "Cannot determine current Git branch; skipping branch validation"
}
}
}
ForEach ($feature in $features) {
$featureFinalStages[$feature.Name] = Resolve-FinalFeatureStage -Feature $feature -Branch $branch -Branding $Branding
}
### CODE GENERATION
$script:Output = ""
Function AddOutput($s) {
$script:Output += $s
}
AddOutput @"
// THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT IT
// INPUT FILE: $Path
"@
ForEach ($feature in $features) {
$stage = $featureFinalStages[$feature.Name]
AddOutput @"
#define $($feature.PreprocessorName()) $(If ($stage -eq [Stage]::AlwaysEnabled) { "1" } Else { "0" })
"@
}
AddOutput @"
#if defined(__cplusplus)
"@
ForEach ($feature in $features) {
AddOutput @"
__pragma(detect_mismatch("ODR_violation_$($feature.PreprocessorName())_mismatch", "$($feature.Stage)"))
struct $($feature.Name)
{
static constexpr bool IsEnabled() { return $($feature.PreprocessorName()) == 1; }
};
"@
}
AddOutput @"
#endif
"@
If ([String]::IsNullOrEmpty($OutputPath)) {
$script:Output
} Else {
Out-File -Encoding UTF8 -FilePath $OutputPath -InputObject $script:Output
}