Files
build_infra/lizard/Lizard.ps1.in
T
2026-07-28 17:55:29 +08:00

72 lines
3.7 KiB
Plaintext

$ErrorActionPreference = "Stop"
$inputDirs = @PSC_LIZARD_INPUT_DIRS@
$excludeDirs = @PSC_LIZARD_EXCLUDE_DIRS@
$outputDir = "@PSC_LIZARD_OUTPUT_DIR@"
$configuredLizard = "@PSC_LIZARD_EXECUTABLE@"
$topN = [int]"@PSC_LIZARD_TOP_N@"
$ccnWarning = [int]"@PSC_LIZARD_CCN_WARNING@"
$lengthWarning = [int]"@PSC_LIZARD_LENGTH_WARNING@"
$argumentWarning = [int]"@PSC_LIZARD_ARGUMENT_WARNING@"
function Resolve-Lizard {
if ($configuredLizard -and (Test-Path -LiteralPath $configuredLizard)) {
return (Resolve-Path -LiteralPath $configuredLizard).Path
}
$command = Get-Command lizard -ErrorAction SilentlyContinue | Select-Object -First 1
if ($command) {
return $command.Source
}
$fallback = Join-Path $env:APPDATA "Python\Python310\Scripts\lizard.exe"
if (Test-Path -LiteralPath $fallback) {
return (Resolve-Path -LiteralPath $fallback).Path
}
throw "lizard executable was not found"
}
$lizard = Resolve-Lizard
New-Item -ItemType Directory -Force -Path $outputDir | Out-Null
$baseArguments = @("-C", $ccnWarning, "-L", $lengthWarning, "-a", $argumentWarning)
foreach ($exclude in $excludeDirs) {
$pattern = (Join-Path $exclude "*").Replace("\", "/")
$baseArguments += @("-x", $pattern)
}
$csvPath = Join-Path $outputDir "lizard_raw.csv"
$xmlPath = Join-Path $outputDir "lizard.xml"
$textPath = Join-Path $outputDir "lizard.txt"
& $lizard @baseArguments "--csv" @inputDirs | Set-Content -LiteralPath $csvPath -Encoding UTF8
$csvExitCode = $LASTEXITCODE
& $lizard @baseArguments "--xml" @inputDirs | Set-Content -LiteralPath $xmlPath -Encoding UTF8
& $lizard @baseArguments @inputDirs | Set-Content -LiteralPath $textPath -Encoding UTF8
$headers = @("NLOC", "CCN", "TokenCount", "ParameterCount", "Length", "Location", "File", "QualifiedName", "Signature", "StartLine", "EndLine")
$records = @(Get-Content -LiteralPath $csvPath | ConvertFrom-Csv -Header $headers | ForEach-Object {
[pscustomobject]@{
NLOC = [int]$_.NLOC
CCN = [int]$_.CCN
TokenCount = [int]$_.TokenCount
ParameterCount = [int]$_.ParameterCount
Length = [int]$_.Length
File = $_.File
Function = $_.QualifiedName
Signature = $_.Signature
StartLine = [int]$_.StartLine
EndLine = [int]$_.EndLine
}
})
$cyclomaticTop = @($records | Sort-Object @{Expression = "CCN"; Descending = $true}, @{Expression = "NLOC"; Descending = $true} | Select-Object -First $topN)
$lengthTop = @($records | Sort-Object @{Expression = "NLOC"; Descending = $true}, @{Expression = "CCN"; Descending = $true} | Select-Object -First $topN)
$parameterTop = @($records | Sort-Object @{Expression = "ParameterCount"; Descending = $true}, @{Expression = "CCN"; Descending = $true} | Select-Object -First $topN)
$cyclomaticTop | Export-Csv -LiteralPath (Join-Path $outputDir "cyclomatic_complexity.csv") -NoTypeInformation -Encoding UTF8
$lengthTop | Export-Csv -LiteralPath (Join-Path $outputDir "function_length.csv") -NoTypeInformation -Encoding UTF8
$parameterTop | Export-Csv -LiteralPath (Join-Path $outputDir "parameter_count.csv") -NoTypeInformation -Encoding UTF8
$summary = [ordered]@{
tool = "lizard"
version = (& $lizard --version | Select-Object -First 1).Trim()
tool_exit_code = $csvExitCode
functions = $records.Count
average_ccn = if ($records.Count) { [Math]::Round(($records | Measure-Object CCN -Average).Average, 3) } else { 0 }
thresholds = [ordered]@{ ccn = $ccnWarning; length = $lengthWarning; arguments = $argumentWarning }
cyclomatic_top = $cyclomaticTop
function_length_top = $lengthTop
parameter_top = $parameterTop
}
[IO.File]::WriteAllText((Join-Path $outputDir "summary.json"), ($summary | ConvertTo-Json -Depth 8), [Text.UTF8Encoding]::new($false))
Write-Output "Lizard report: $outputDir"