Files
xtrhrj/scripts/Measure-Comments.ps1
T

27 lines
1.3 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
[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) }