/ .pipelines / applyXamlStyling.ps1
applyXamlStyling.ps1
  1  <#
  2      .SYNOPSIS
  3      Modify XAML files to adhere to XAML Styler settings.
  4  
  5      .DESCRIPTION
  6      The Apply XAML Stying Script can be used to check or modify XAML files with the repo's XAML Styler settings.
  7      Learn more about XAML Styler at https://github.com/Xavalon/XamlStyler
  8  
  9      By default, uses git status to check all new or modified files.
 10  
 11      Use "PS> Help .\applyXamlStyling.ps1 -Full" for more details on parameters.
 12  
 13      .PARAMETER LastCommit
 14      Runs against last commit vs. current changes
 15  
 16      .PARAMETER Unstaged
 17      Runs against unstaged changed files
 18  
 19      .PARAMETER Staged
 20      Runs against staged files vs. current changes
 21  
 22      .PARAMETER Main
 23      Runs against main vs. current branch
 24  
 25      .PARAMETER Passive
 26      Runs a passive check against all files in the repo for the CI
 27  
 28      .EXAMPLE
 29      PS> .\applyXamlStyling.ps1 -Main
 30  #>
 31  param(
 32      [switch]$LastCommit = $false,
 33      [switch]$Unstaged = $false,
 34      [switch]$Staged = $false,
 35      [switch]$Main = $false,
 36      [switch]$Passive = $false
 37  )
 38  
 39  Write-Output "Use 'Help .\applyXamlStyling.ps1' for more info or '-Main' to run against all files."
 40  Write-Output ""
 41  Write-Output "Restoring dotnet tools..."
 42  dotnet tool restore --disable-parallel --no-cache
 43  
 44  # Use Regex syntax
 45  $PathExcludes = "(\\obj\\)|(\\bin\\)|(\\x64\\)|(\\Generated Files\\PowerRenameXAML\\)|(\\RegistryPreviewUILib\\Controls\\HexBox\\)"
 46  
 47  if (-not $Passive)
 48  {
 49      # Look for unstaged changed files by default
 50      $gitDiffCommand = "git status -s --porcelain"
 51  
 52      if ($Main)
 53      {
 54          Write-Output 'Checking Current Branch against `main` Files Only'
 55          $branch = git status | Select-String -Pattern "On branch (?<branch>.*)$"
 56          if ($null -eq $branch.Matches)
 57          {
 58              $branch = git status | Select-String -Pattern "HEAD detached at (?<branch>.*)$"
 59              if ($null -eq $branch.Matches)
 60              {
 61                  Write-Error 'Don''t know how to fetch branch from `git status`:'
 62                  git status | Write-Error
 63                  exit 1
 64              }
 65          }
 66          $branch = $branch.Matches.groups[1].Value
 67          $gitDiffCommand = "git diff origin/main $branch --name-only --diff-filter=ACM"
 68      }
 69      elseif ($Unstaged)
 70      {
 71          # Look for unstaged files
 72          Write-Output "Checking Unstaged Files"
 73          $gitDiffCommand = "git diff --name-only --diff-filter=ACM"
 74      }
 75      elseif ($Staged)
 76      {
 77          # Look for staged files
 78          Write-Output "Checking Staged Files Only"
 79          $gitDiffCommand = "git diff --cached --name-only --diff-filter=ACM"
 80      }
 81      elseif ($LastCommit)
 82      {
 83          # Look at last commit files
 84          Write-Output "Checking the Last Commit's Files Only"
 85          $gitDiffCommand = "git diff HEAD^ HEAD --name-only --diff-filter=ACM"
 86      }
 87      else 
 88      {
 89          Write-Output "Checking Git Status Files Only"    
 90      }
 91  
 92      Write-Output "Running Git Diff: $gitDiffCommand"
 93      $files = Invoke-Expression $gitDiffCommand | Select-String -Pattern "\.xaml$" | Where-Object { $_ -notmatch $PathExcludes }
 94  
 95      if (-not $Passive -and -not $Main -and -not $Unstaged -and -not $Staged -and -not $LastCommit)
 96      {
 97          # Remove 'status' column of 3 characters at beginning of lines
 98          $files = $files | ForEach-Object { $_.ToString().Substring(3) }
 99      }
100  
101      if ($files.count -gt 0)
102      {
103          dotnet tool run xstyler -c "$PSScriptRoot\..\src\Settings.XamlStyler" -f $files
104      }
105      else
106      {
107          Write-Output "No XAML Files found to style..."
108      }
109  }
110  else 
111  {
112      Write-Output "Checking all files (passively)"
113      $files = Get-ChildItem -Path "$PSScriptRoot\..\src\*.xaml" -Recurse | Select-Object -ExpandProperty FullName | Where-Object { $_ -notmatch $PathExcludes }
114  
115      if ($files.count -gt 0)
116      {
117          dotnet tool run xstyler -p -c "$PSScriptRoot\..\src\Settings.XamlStyler" -f $files
118  
119          if ($lastExitCode -eq 1)
120          {
121              Write-Error 'XAML Styling is incorrect, please run `.\.pipelines\applyXamlStyling.ps1 -Main` locally.'
122          }
123          if ($lastExitCode -lt 0)
124          {
125              Write-Error "Error running dotnet tool run, with the exit code $lastExitCode. Please verify logs and running environment."
126          }
127          # Return XAML Styler Status
128          exit $lastExitCode
129      }
130      else
131      {
132          exit 0
133      }
134  }