forked from CommunityToolkit/Labs-Windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplyXamlStyling.ps1
162 lines (143 loc) · 5.79 KB
/
ApplyXamlStyling.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
<#
.SYNOPSIS
Modify XAML files to adhere to XAML Styler settings.
.DESCRIPTION
The Apply XAML Stying Script can be used to check or modify XAML files with the repo's XAML Styler settings.
Learn more about XAML Styler at https://github.com/Xavalon/XamlStyler
By default, uses git status to check all new or modified files.
Use "PS> Help .\ApplyXamlStyling.ps1 -Full" for more details on parameters.
.PARAMETER LastCommit
Runs against last commit vs. current changes
.PARAMETER Unstaged
Runs against unstaged changed files
.PARAMETER Staged
Runs against staged files vs. current changes
.PARAMETER Main
Runs against main vs. current branch
.PARAMETER Passive
Runs a passive check against all files in the repo for the CI
.EXAMPLE
PS> .\ApplyXamlStyling.ps1 -Main
#>
param(
[switch]$LastCommit = $false,
[switch]$Unstaged = $false,
[switch]$Staged = $false,
[switch]$Main = $false,
[switch]$Passive = $false
)
Write-Output "Use 'Help .\ApplyXamlStyling.ps1' for more info or '-Main' to run against all files."
Write-Output ""
Write-Output "Restoring dotnet tools..."
dotnet tool restore
if (-not $Passive)
{
# Look for unstaged changed files by default
$gitDiffCommand = "git status -s --porcelain"
if ($Main)
{
Write-Output 'Checking Current Branch against `main` Files Only'
$branch = git status | Select-String -Pattern "On branch (?<branch>.*)$"
if ($null -eq $branch.Matches)
{
$branch = git status | Select-String -Pattern "HEAD detached at (?<branch>.*)$"
if ($null -eq $branch.Matches)
{
Write-Error 'Don''t know how to fetch branch from `git status`:'
git status | Write-Error
exit 1
}
}
$branch = $branch.Matches.groups[1].Value
$gitDiffCommand = "git diff origin/main $branch --name-only --diff-filter=ACM"
}
elseif ($Unstaged)
{
# Look for unstaged files
Write-Output "Checking Unstaged Files"
$gitDiffCommand = "git diff --name-only --diff-filter=ACM"
}
elseif ($Staged)
{
# Look for staged files
Write-Output "Checking Staged Files Only"
$gitDiffCommand = "git diff --cached --name-only --diff-filter=ACM"
}
elseif ($LastCommit)
{
# Look at last commit files
Write-Output "Checking the Last Commit's Files Only"
$gitDiffCommand = "git diff HEAD^ HEAD --name-only --diff-filter=ACM"
}
else
{
Write-Output "Checking Git Status Files Only"
}
Write-Output "Running Git Diff: $gitDiffCommand"
$files = Invoke-Expression $gitDiffCommand | Select-String -Pattern "\.xaml$"
if (-not $Passive -and -not $Main -and -not $Unstaged -and -not $Staged -and -not $LastCommit)
{
# Remove 'status' column of 3 characters at beginning of lines
$files = $files | ForEach-Object { $_.ToString().Substring(3) }
}
if ($files.count -gt 0)
{
dotnet tool run xstyler -c .\settings.xamlstyler -f $files
}
else
{
Write-Output "No XAML Files found to style..."
}
}
else
{
Write-Output "Checking all files (passively)"
$files = Get-ChildItem *.xaml -Recurse | Select-Object -ExpandProperty FullName | Where-Object { $_ -notmatch "(\\obj\\)|(\\bin\\)" }
if ($files.count -gt 0)
{
dotnet tool run xstyler -p -c .\settings.xamlstyler -f $files
if ($lastExitCode -eq 1)
{
Write-Error 'XAML Styling is incorrect, please run `ApplyXamlStyling.ps1 -Main` locally.'
}
# Return XAML Styler Status
exit $lastExitCode
}
else
{
exit 0
}
}
# SIG # Begin signature block
# MIIFwwYJKoZIhvcNAQcCoIIFtDCCBbACAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4w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