#!/usr/bin/env pwsh <# .SYNOPSIS Fixes StyleCop warnings in C# files. .DESCRIPTION Automatically fixes common StyleCop warnings: - SA1413: Adds trailing commas to multi-line initializers - SA1515: Adds blank lines before single-line comments - SA1518: Ensures files end with single newline .PARAMETER Path The root path to search for C# files. Defaults to "MediaBrowser.Model" .PARAMETER DryRun If specified, shows what would be changed without actually modifying files .EXAMPLE .\FixStyleCopWarnings.ps1 -DryRun #> param( [string]$Path = "MediaBrowser.Model", [switch]$DryRun ) $ErrorActionPreference = "Stop" function Fix-TrailingCommas { param([string]$Content) $modified = $false $lines = $content -split '\r?\n' for ($i = 0; $i -lt $lines.Length - 1; $i++) { $currentLine = $lines[$i] $nextLine = $lines[$i + 1] # Check if current line ends with a property/value without comma # and next line closes the initializer if ($currentLine -match '^\s+\w+\s*=\s*.+[^,]\s*$' -and $nextLine -match '^\s*[}\)]') { $lines[$i] = $currentLine.TrimEnd() + ',' $modified = $true } } if ($modified) { return ($lines -join "`n") } return $Content } function Fix-BlankLinesBeforeComments { param([string]$Content) $modified = $false $lines = $content -split '\r?\n' $newLines = @() for ($i = 0; $i -lt $lines.Length; $i++) { $currentLine = $lines[$i] # Check if this is a single-line comment if ($currentLine -match '^\s+//\s+\w') { # Check if previous line exists and is not blank if ($i -gt 0) { $prevLine = $lines[$i - 1] $prevLineIsBlank = [string]::IsNullOrWhiteSpace($prevLine) $prevLineIsOpenBrace = $prevLine -match '\{\s*$' $prevLineIsComment = $prevLine -match '^\s*//' # Add blank line if previous line is not blank, not open brace, not another comment if (-not $prevLineIsBlank -and -not $prevLineIsOpenBrace -and -not $prevLineIsComment) { $newLines += "" $modified = $true } } } $newLines += $currentLine } if ($modified) { return ($newLines -join "`n") } return $Content } function Fix-FileEnding { param([string]$Content) # Ensure file ends with exactly one newline $trimmed = $Content.TrimEnd("`r", "`n", " ", "`t") return $trimmed + "`n" } function Fix-StyleCopWarnings { param( [string]$FilePath, [switch]$DryRun ) $content = Get-Content -Path $FilePath -Raw $originalContent = $content # Apply fixes $content = Fix-TrailingCommas -Content $content $content = Fix-BlankLinesBeforeComments -Content $content $content = Fix-FileEnding -Content $content # Restore Windows line endings if original had them if ($originalContent -match '\r\n') { $content = $content -replace '(?