-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathGenerateThemesWikiMarkdown.ps1
179 lines (158 loc) · 6.5 KB
/
GenerateThemesWikiMarkdown.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
$defaultStyleText = "(default style)"
$headerMarkdown = "##"
$listMarkdown = "-"
$themesDirectory = "..\src\MaterialDesignThemes.Wpf\Themes\"
$latestHash = git log -1 --pretty=format:"%H"
$baseURL = "https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob"
$filePathURL = "src/MaterialDesignThemes.Wpf/Themes"
$outputFileName = "ControlStyleList.md"
$themesFullDir = Join-Path $PSScriptRoot $themesDirectory
$outputFullDir = Join-Path $PSScriptRoot $outputFileName
$DebugPreference = 'Continue' # Log debug messages to terminal.
$discoverdStyles = New-Object System.Collections.ArrayList
$defaults = New-Object System.Collections.ArrayList
Function Main {
# Get xaml files and loop through.
Get-ChildItem $themesFullDir -Filter *.xaml |
Foreach-Object {
$xamlLines = Get-Content -Path $_.FullName
$file = Select-ControlNameFromFile($_.Name)
Read-XamlStyles -xamlLines $xamlLines -file $file
}
Set-Defaults
Format-Output
}
Function Format-Output {
Write-OutputFile "[//]: <> (AUTO GENERATED FILE; DO NOT EDIT)"
foreach($style in $discoverdStyles | Sort-Object -Property File,@{Expression = {$_.IsDefault}; Ascending = $false}) {
if ($previousFile -ne $style.File) {
Write-OutputFile "`n$headerMarkdown $($style.File)"
}
$previousFile = $style.File;
$styleLink = "$($baseURL)/$($latestHash)/$($filePathURL)/MaterialDesignTheme.$($style.File).xaml"
if ($style.LineNumber) {
$styleLink += "#L$($style.LineNumber)"
}
$linkAndStyleName = "[$($style.Style)]($styleLink)";
if ($style.IsDefault) {
Write-OutputFile ("$listMarkdown $($linkAndStyleName) $defaultStyleText" -replace '\s+', ' ')
}
else {
Write-OutputFile "$listMarkdown $($linkAndStyleName)"
}
}
}
Function Write-OutputFile{
Param ($output)
Add-content $outputFullDir -value $output
Write-Debug $output #debug
}
Function Set-Defaults{
ForEach ($default in $defaults) {
$style = $discoverdStyles.Where({$_.style -match $default.style -and $_.Control -match $default.Type})
if ($null -ne $style[0]) {
$style[0].IsDefault = $true
}
else {
$temp = Get-Style -targetType $default.Type -styleName $default.Style -fileName $default.Type
$discoverdStyles.Add($temp) | Out-Null
}
}
$discoverdStyles | Format-Table #debug
}
Function Select-ControlNameFromFile {
Param ($fileName)
return $fileName -replace ".xaml" -replace "MaterialDesignTheme."
}
Function Read-XamlStyles {
Param ($xamlLines, $file)
[xml]$xaml = $xamlLines
$lineNum = 1
$xaml.ResourceDictionary.Style |
Foreach-Object {
Write-Output $_
# Get line number by Key or TargetType
$styleLineNumber = $null
$searchKey = if ($_.Key) { $_.Key } else { $_.TargetType }
for ($i = 0; $i -lt $xamlLines.Length; $i++) {
if ($xamlLines[$i] -match [regex]::Escape($searchKey)) {
$styleLineNumber = $i + 1
break
}
}
if ($file -eq "Defaults") {
New-Default -style $_ -file $file -lineNumber $styleLineNumber
}
elseif ($file -eq "Generic") {
New-GenericDefault -style $_ -file $file -lineNumber $styleLineNumber
}
else {
New-Style -style $_ -file $file -lineNumber $styleLineNumber
}
$lineNum++
}
}
Function New-GenericDefault {
Param ($style, $file, $lineNumber)
$targetType = Read-TargetType($style | Select-Object TargetType)
$basedOn = Read-BasedOn($style | Select-Object BasedOn)
$styleNameValue = ($style | Select-Object Key).Key
$defaultStyleName = if ($null -eq $styleNameValue) { $basedOn } else { $styleNameValue }
Write-Debug "[$file] [Type: $targetType] [StyleNameValue: $styleNameValue] [BasedOn: $basedOn] [DefaultStyleName: $defaultStyleName]"
Add-DefaultStyle -file $file -targetType $targetType -styleName $defaultStyleName -lineNumber $lineNumber
}
Function New-Default {
Param ($style, $file, $lineNumber)
$targetType = Read-TargetType($style | Select-Object TargetType)
$basedOn = Read-BasedOn($style | Select-Object BasedOn)
$styleNameValue = ($style | Select-Object Key).Key
$defaultStyleName = if ($null -eq $styleNameValue) { $basedOn } else { $styleNameValue }
Write-Debug "[$file] [Type: $targetType] [StyleNameValue: $styleNameValue] [BasedOn: $basedOn] [DefaultStyleName: $defaultStyleName]"
Add-DefaultStyle -file $file -targetType $targetType -styleName $defaultStyleName -lineNumber $lineNumber
}
Function New-Style {
Param ($style, $file, $lineNumber)
$targetType = Read-TargetType($style | Select-Object TargetType)
$styleName = ($style | Select-Object Key).Key
$splittedFile = $file.split('.') # Support for "nested" file names like DataGrid.ComboBox
if ($targetType -eq $splittedFile[-1]) {
Write-Debug "[Match ] [File: $file] [Type: $targetType] [Style: $styleName]"
Add-Style -targetType $targetType -styleName $styleName -fileName $file -lineNumber $lineNumber
}
else {
Write-Debug "[Skipped] [File: $file] [Type: $targetType] [Style: $styleName]"
}
}
Function Add-Style {
Param ($targetType, $styleName, $fileName, $lineNumber)
$temp = Get-Style -targetType $targetType -styleName $styleName -fileName $file -lineNumber $lineNumber
$discoverdStyles.Add($temp) | Out-Null
}
Function Get-Style {
Param ($targetType, $styleName, $fileName, $lineNumber)
$temp = "" | Select-Object "Control", "Style", "IsDefault", "File", "LineNumber"
$temp.Control = $targetType
$temp.Style = $styleName
$temp.IsDefault = !$styleName
$temp.File = $fileName
$temp.LineNumber = $lineNumber
return $temp
}
Function Add-DefaultStyle {
Param ($file, $targetType, $styleName, $lineNumber)
$temp = "" | Select-Object "File", "Type", "Style", "LineNumber"
$temp.File = $file
$temp.Type = $targetType
$temp.Style = $styleName
$temp.LineNumber = $lineNumber
$defaults.Add($temp) | Out-Null
}
Function Read-TargetType {
Param ($targetTypeText)
return ($targetTypeText.TargetType -replace "{x:Type" -replace "{x:Type" -replace ".*:" -replace "}*" -replace "Base").Trim()
}
Function Read-BasedOn {
Param ($targetTypeText)
return ($targetTypeText.BasedOn -replace "{StaticResource" -replace ".*:" -replace "}*").Trim()
}
Main