From 6482e9b0de906689b0bf2f4f0b95bc9e16ffa92d Mon Sep 17 00:00:00 2001 From: Davide Giacometti Date: Mon, 9 Oct 2023 16:58:13 +0200 Subject: [PATCH] [Build]Use XamlStyler to check XAML formatting (#28643) * Use XamlStyler to check XAML formatting * fix spellcheck * fix * format XAML FLS, Hosts, ImageResizer, MeasureTool, PowerRename * format XAML Peek * exclude settings XAML and make CI pass * doc --- .config/dotnet-tools.json | 10 +- .github/actions/spell-check/expect.txt | 4 + .pipelines/applyXamlStyling.ps1 | 128 ++++++ .../ci/templates/build-powertoys-steps.yml | 11 +- Settings.XamlStyler | 42 ++ doc/devdocs/style.md | 1 + .../FileLocksmithXAML/MainWindow.xaml | 25 +- .../Hosts/Hosts/HostsXAML/MainWindow.xaml | 27 +- .../Hosts/Hosts/HostsXAML/Views/MainPage.xaml | 33 +- .../MeasureToolUI/MeasureToolXAML/App.xaml | 4 +- src/modules/imageresizer/ui/App.xaml | 51 +-- .../imageresizer/ui/Views/InputPage.xaml | 9 +- .../Controls/ArchiveControl.xaml | 121 +++--- .../Controls/BrowserControl.xaml | 14 +- .../InformationalPreviewControl.xaml | 10 +- .../peek/Peek.FilePreviewer/FilePreview.xaml | 8 +- src/modules/peek/Peek.UI/PeekXAML/App.xaml | 8 +- .../peek/Peek.UI/PeekXAML/Views/TitleBar.xaml | 4 +- .../PowerRenameUILib/PowerRenameXAML/App.xaml | 391 +++++++++--------- .../PowerRenameXAML/ExplorerItem.xaml | 42 +- .../PowerRenameXAML/MainWindow.xaml | 62 +-- 21 files changed, 594 insertions(+), 411 deletions(-) create mode 100644 .pipelines/applyXamlStyling.ps1 create mode 100644 Settings.XamlStyler diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json index f53b78c270..44a31d4200 100644 --- a/.config/dotnet-tools.json +++ b/.config/dotnet-tools.json @@ -7,6 +7,12 @@ "commands": [ "dotnet-consolidate" ] - } + }, + "xamlstyler.console": { + "version": "3.2206.4", + "commands": [ + "xstyler" + ] + } } -} \ No newline at end of file +} diff --git a/.github/actions/spell-check/expect.txt b/.github/actions/spell-check/expect.txt index f0795e524b..a9c9c3fc3d 100644 --- a/.github/actions/spell-check/expect.txt +++ b/.github/actions/spell-check/expect.txt @@ -2083,6 +2083,7 @@ vscdb vsconfig VSCROLL vsetq +VSM vsonline vstemplate VSTHRD @@ -2229,6 +2230,8 @@ Wubi WVC Wwan Wwanpp +xamlstyler +Xavalon XAxis xbf Xbox @@ -2249,6 +2252,7 @@ XPels XPixel XResource xsi +xstyler XStr XUP XVIRTUALSCREEN diff --git a/.pipelines/applyXamlStyling.ps1 b/.pipelines/applyXamlStyling.ps1 new file mode 100644 index 0000000000..354abf351d --- /dev/null +++ b/.pipelines/applyXamlStyling.ps1 @@ -0,0 +1,128 @@ +<# + .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 (?.*)$" + if ($null -eq $branch.Matches) + { + $branch = git status | Select-String -Pattern "HEAD detached at (?.*)$" + 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 "$PSScriptRoot\..\Settings.XamlStyler" -f $files + } + else + { + Write-Output "No XAML Files found to style..." + } +} +else +{ + Write-Output "Checking all files (passively)" + $files = Get-ChildItem -Path "$PSScriptRoot\..\src\*.xaml" -Recurse | Select-Object -ExpandProperty FullName | Where-Object { $_ -notmatch "(\\obj\\)|(\\bin\\)|(\\x64\\)|(\\launcher\\PowerLauncher\\)|(\\launcher\\Wox.Plugin\\)|(\\colorPicker\\ColorPickerUI\\)|(\\editor\\FancyZonesEditor\\)|(\\settings-ui\\Settings.UI\\)" } + + if ($files.count -gt 0) + { + dotnet tool run xstyler -p -c "$PSScriptRoot\..\Settings.XamlStyler" -f $files + + if ($lastExitCode -eq 1) + { + Write-Error 'XAML Styling is incorrect, please run `.\.pipelines\applyXamlStyling.ps1 -Main` locally.' + } + + # Return XAML Styler Status + exit $lastExitCode + } + else + { + exit 0 + } +} diff --git a/.pipelines/ci/templates/build-powertoys-steps.yml b/.pipelines/ci/templates/build-powertoys-steps.yml index 43358f3cc0..b9bb635f88 100644 --- a/.pipelines/ci/templates/build-powertoys-steps.yml +++ b/.pipelines/ci/templates/build-powertoys-steps.yml @@ -8,7 +8,14 @@ steps: clean: true - task: PowerShell@2 - displayName: Verifying Nuget package versions for PowerToys.sln + displayName: Verify XAML formatting + inputs: + filePath: '$(build.sourcesdirectory)\.pipelines\applyXamlStyling.ps1' + arguments: -Passive + pwsh: true + +- task: PowerShell@2 + displayName: Verify Nuget package versions for PowerToys.sln inputs: filePath: '$(build.sourcesdirectory)\.pipelines\verifyNugetPackages.ps1' arguments: -solution '$(build.sourcesdirectory)\PowerToys.sln' @@ -240,7 +247,7 @@ steps: dotnet list $(build.sourcesdirectory)\src\common\Common.UI\Common.UI.csproj package - task: PowerShell@2 - displayName: Verifying Notice.md and Nuget packages match + displayName: Verify Notice.md and Nuget packages match inputs: filePath: '$(build.sourcesdirectory)\.pipelines\verifyNoticeMdAgainstNugetPackages.ps1' arguments: -path '$(build.sourcesdirectory)\' diff --git a/Settings.XamlStyler b/Settings.XamlStyler new file mode 100644 index 0000000000..080186d70a --- /dev/null +++ b/Settings.XamlStyler @@ -0,0 +1,42 @@ +{ + "AttributesTolerance": 2, + "KeepFirstAttributeOnSameLine": false, + "MaxAttributeCharactersPerLine": 0, + "MaxAttributesPerLine": 1, + "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter", + "SeparateByGroups": false, + "AttributeIndentation": 0, + "AttributeIndentationStyle": 1, + "RemoveDesignTimeReferences": false, + "IgnoreDesignTimeReferencePrefix": false, + "EnableAttributeReordering": true, + "AttributeOrderingRuleGroups": [ + "x:Class", + "xmlns, xmlns:x", + "xmlns:*", + "x:Key, Key, x:Name, Name, x:Uid, Uid, Title", + "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom", + "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight", + "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex", + "*:*, *", + "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint", + "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText", + "Storyboard.*, From, To, Duration" + ], + "FirstLineAttributes": "", + "OrderAttributesByName": true, + "PutEndingBracketOnNewLine": false, + "RemoveEndingTagOfEmptyElement": true, + "SpaceBeforeClosingSlash": true, + "RootElementLineBreakRule": 0, + "ReorderVSM": 2, + "ReorderGridChildren": false, + "ReorderCanvasChildren": false, + "ReorderSetters": 0, + "FormatMarkupExtension": true, + "NoNewLineMarkupExtensions": "x:Bind, Binding", + "ThicknessSeparator": 2, + "ThicknessAttributes": "Margin, Padding, BorderThickness, ThumbnailClipMargin", + "FormatOnSave": true, + "CommentPadding": 2, +} diff --git a/doc/devdocs/style.md b/doc/devdocs/style.md index f63e962982..b63819d2c1 100644 --- a/doc/devdocs/style.md +++ b/doc/devdocs/style.md @@ -6,6 +6,7 @@ ## Formatting +- We use [XamlStyler](https://github.com/Xavalon/XamlStyler/) to format XAML files. You can use the [Visual Studio Extension](https://marketplace.visualstudio.com/items?itemName=TeamXavalon.XAMLStyler2022) or apply formatting executing `.\.pipelines\applyXamlStyling.ps1 -Main`. - We use [`.clang-format`](https://github.com/microsoft/PowerToys/blob/main/src/.clang-format) style file to enable automatic code formatting. You can [easily format source files from Visual Studio](https://devblogs.microsoft.com/cppblog/clangformat-support-in-visual-studio-2017-15-7-preview-1/). For example, `CTRL+K CTRL+D` formats the current document. - If you prefer another text editor or have ClangFormat disabled in Visual Studio, you could invoke [`format_sources`](https://github.com/microsoft/PowerToys/blob/main/src/codeAnalysis/format_sources.ps1) powershell script from command line. It gets a list of all currently modified files from `git` and invokes clang-format on them. Please note that you should also have `clang-format.exe` in `%PATH%` for it to work. The script can infer the path of `clang-format.exe` version which is shipped with Visual Studio at `%VCINSTALLDIR%\Tools\Llvm\bin\`, if you launch it from the *Native Tools Command Prompt for VS*. diff --git a/src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithXAML/MainWindow.xaml b/src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithXAML/MainWindow.xaml index 1c2be61a88..ddca6691c4 100644 --- a/src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithXAML/MainWindow.xaml +++ b/src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithXAML/MainWindow.xaml @@ -20,29 +20,30 @@ - + - - - - - + + + + + + Height="16" + VerticalAlignment="Center" + Source="../Assets/FileLocksmith/Icon.ico" /> - + diff --git a/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml b/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml index e0c5ee97e3..37c43fa4fc 100644 --- a/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml +++ b/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml @@ -1,4 +1,4 @@ - - + - - - - + + + + + Height="16" + VerticalAlignment="Center" + Source="../Assets/Hosts/Hosts.ico" /> + VerticalAlignment="Center" + Style="{StaticResource CaptionTextBlockStyle}" /> - + diff --git a/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml b/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml index f62eef0a98..2486b708c0 100644 --- a/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml +++ b/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml @@ -1,4 +1,4 @@ - @@ -180,8 +180,8 @@ Icon="Edit"> @@ -191,8 +191,8 @@ Icon="TwoBars"> @@ -218,9 +218,7 @@ Click="Delete_Click" Icon="Delete"> - + @@ -232,11 +230,16 @@ Background="Transparent" ColumnSpacing="8"> - - - - - + + + + + + + + + + diff --git a/src/modules/MeasureTool/MeasureToolUI/MeasureToolXAML/App.xaml b/src/modules/MeasureTool/MeasureToolUI/MeasureToolXAML/App.xaml index fdc4b7f0f8..86703563bd 100644 --- a/src/modules/MeasureTool/MeasureToolUI/MeasureToolXAML/App.xaml +++ b/src/modules/MeasureTool/MeasureToolUI/MeasureToolXAML/App.xaml @@ -7,9 +7,9 @@ - + - + diff --git a/src/modules/imageresizer/ui/App.xaml b/src/modules/imageresizer/ui/App.xaml index 00fa42a212..b11e65520b 100644 --- a/src/modules/imageresizer/ui/App.xaml +++ b/src/modules/imageresizer/ui/App.xaml @@ -1,10 +1,11 @@ - + @@ -12,27 +13,29 @@ - + - - - - - - - + - - - - - + + + + + + + + + + + diff --git a/src/modules/imageresizer/ui/Views/InputPage.xaml b/src/modules/imageresizer/ui/Views/InputPage.xaml index 5eeb3e5e09..81f23238ed 100644 --- a/src/modules/imageresizer/ui/Views/InputPage.xaml +++ b/src/modules/imageresizer/ui/Views/InputPage.xaml @@ -25,7 +25,10 @@ SelectedIndex="{Binding Settings.SelectedSizeIndex}"> - + @@ -67,9 +70,7 @@ - + diff --git a/src/modules/peek/Peek.FilePreviewer/Controls/ArchiveControl.xaml b/src/modules/peek/Peek.FilePreviewer/Controls/ArchiveControl.xaml index 346d0e82c8..fde4a6d171 100644 --- a/src/modules/peek/Peek.FilePreviewer/Controls/ArchiveControl.xaml +++ b/src/modules/peek/Peek.FilePreviewer/Controls/ArchiveControl.xaml @@ -1,72 +1,63 @@ - - - + + + - + + IsExpanded="{x:Bind IsExpanded}" + ItemsSource="{x:Bind Children}"> - + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + @@ -75,29 +66,25 @@ - + + ItemTemplateSelector="{StaticResource ArchiveItemTemplateSelector}" + ItemsSource="{x:Bind Source, Mode=OneWay}" + SelectionMode="None" /> - + CornerRadius="8"> + @@ -107,29 +94,29 @@ + BorderThickness="0,0,1,0" /> + BorderThickness="0,0,1,0" /> diff --git a/src/modules/peek/Peek.FilePreviewer/Controls/BrowserControl.xaml b/src/modules/peek/Peek.FilePreviewer/Controls/BrowserControl.xaml index 1ddfee1c6f..bed3409c99 100644 --- a/src/modules/peek/Peek.FilePreviewer/Controls/BrowserControl.xaml +++ b/src/modules/peek/Peek.FilePreviewer/Controls/BrowserControl.xaml @@ -1,22 +1,22 @@ - - + + + NavigationCompleted="PreviewWV2_NavigationCompleted" + NavigationStarting="PreviewBrowser_NavigationStarting" /> + - + - + - + - + diff --git a/src/modules/peek/Peek.FilePreviewer/FilePreview.xaml b/src/modules/peek/Peek.FilePreviewer/FilePreview.xaml index 9d1539cdd9..90d9d8544b 100644 --- a/src/modules/peek/Peek.FilePreviewer/FilePreview.xaml +++ b/src/modules/peek/Peek.FilePreviewer/FilePreview.xaml @@ -53,13 +53,13 @@ - + - + + - + - + diff --git a/src/modules/peek/Peek.UI/PeekXAML/Views/TitleBar.xaml b/src/modules/peek/Peek.UI/PeekXAML/Views/TitleBar.xaml index 209b8c9982..7332884799 100644 --- a/src/modules/peek/Peek.UI/PeekXAML/Views/TitleBar.xaml +++ b/src/modules/peek/Peek.UI/PeekXAML/Views/TitleBar.xaml @@ -1,4 +1,4 @@ - + diff --git a/src/modules/powerrename/PowerRenameUILib/PowerRenameXAML/App.xaml b/src/modules/powerrename/PowerRenameUILib/PowerRenameXAML/App.xaml index 7d42868d19..8ee568b2a6 100644 --- a/src/modules/powerrename/PowerRenameUILib/PowerRenameXAML/App.xaml +++ b/src/modules/powerrename/PowerRenameUILib/PowerRenameXAML/App.xaml @@ -34,7 +34,6 @@ x:Name="RootGrid" Background="{ThemeResource AccentButtonBackground}" CornerRadius="{TemplateBinding CornerRadius}"> -