[CmdletBinding()] param( [string[]]$SourceRoots = @((Join-Path (Split-Path -Parent $PSScriptRoot) 'src/main/java')), [double]$MinimumPercent = 20, [switch]$Detailed ) $ErrorActionPreference = 'Stop' $rows = foreach ($root in $SourceRoots) { foreach ($file in Get-ChildItem -LiteralPath $root -Filter '*.java' -File -Recurse) { $lines = [IO.File]::ReadAllLines($file.FullName) $nonEmpty = @($lines | Where-Object { $_.Trim().Length -gt 0 }) $comments = @($nonEmpty | Where-Object { $_ -match '^\s*(//|/\*|\*|\*/)' }) [pscustomobject]@{ File = $file.FullName NonEmpty = $nonEmpty.Count CommentLines = $comments.Count Percent = if ($nonEmpty.Count) { [Math]::Round(100.0 * $comments.Count / $nonEmpty.Count, 2) } else { 0 } } } } $total = ($rows | Measure-Object NonEmpty -Sum).Sum $commentTotal = ($rows | Measure-Object CommentLines -Sum).Sum $percent = if ($total) { 100.0 * $commentTotal / $total } else { 0 } if ($Detailed) { $rows | Format-Table -AutoSize } Write-Output ('Java 注释占比:{0:N2}%({1} 注释行 / {2} 非空源码行)' -f $percent, $commentTotal, $total) if ($percent -lt $MinimumPercent) { throw ('尚未达到约定的 {0}% 注释行要求。' -f $MinimumPercent) }