Skip to content

Instantly share code, notes, and snippets.

@dlidstrom
Created August 18, 2014 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlidstrom/eb398a24614740c5d051 to your computer and use it in GitHub Desktop.
Save dlidstrom/eb398a24614740c5d051 to your computer and use it in GitHub Desktop.
param([switch]$verbose, [switch]$v)
$TeamCity = $env:TEAMCITY_PROJECT_NAME
# read list of files to ignore
$ignoredFiles = @{}
$filesWithoutIssues = @{}
Get-Content .\StyleCop.IgnoredFiles.txt | % {
if (Test-Path($_))
{
$item = Get-ChildItem $_
$ignoredFiles[$item.FullName.ToLower()] = $true
# assume all files have issues
$filesWithoutIssues[$item.FullName.ToLower()] = $_
}
else
{
if ($TeamCity)
{
Write-Host "##teamcity[message text='$($_) does not exist, remove from StyleCop.IgnoreFiles.txt']"
}
else
{
Write-Host $_ 'does not exist, remove from StyleCop.IgnoreFiles.txt'
}
}
}
if ($TeamCity)
{
Write-Host "##teamcity[message text='Running StyleCop...']"
Write-Host "##teamcity[message text='$($ignoredFiles.Count) files excluded by ignore list']"
$issueCount = 0
$command = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe StyleCop.build /nologo /verbosity:m /p:StyleCopMaxViolationCount=50000"
$issues = Invoke-Expression -Command $command
$totalIssueCount = $issues.length
$groupedIssues = $issues | group { $_ -replace "\(.*", "" }
$filteredIssues = $groupedIssues | where { $ignoredFiles.ContainsKey($_.Name.ToLower()) -eq $false }
# exclude files with issues from $filesWithoutIssues
$issues | % { $_ -replace "\(.*", "" } | % { $filesWithoutIssues.Remove($_.ToLower()) }
foreach ($issue in $filteredIssues)
{
$blockName = $issue.Name
Write-Host "##teamcity[blockOpened name='$($blockName)']"
foreach ($item in $issue.Group | % { $null = $_ -match '.*(?<filename>dev\\src\\Order.*)\((?<line>\d*),.*\): warning : (?<warning>SA\d{4}.*) \[.*\]'; $matches })
{
$issueCount = $issueCount + 1
$message = "$($item.filename)($($item.line)): $($item.warning -replace "'", "|'")"
Write-Host "##teamcity[message text='$($message)' status='ERROR']"
Write-Host "##teamcity[buildProblem description='$($message)']"
}
Write-Host "##teamcity[blockClosed name='$($blockName)']"
}
if ($issueCount -gt 0)
{
Write-Host "##teamcity[buildStatus status='FAILURE' text='$($issueCount) StyleCop issues detected']"
}
else
{
Write-Host "##teamcity[buildStatus status='SUCCESS' text='$($issueCount) StyleCop issues detected']"
}
Write-Host "##teamcity[buildStatisticValue key='StyleCopWarnings' value='$($totalIssueCount)']"
if ($filesWithoutIssues.Count -ne 0)
{
Write-Host "Remove these files from ignore list, they do not contain any issues and should be included in analysis for the future"
$filesWithoutIssues.Values | sort | % { Write-Host $_ }
}
$lowHangingFruits = $groupedIssues | where { $_.Count -le 1 }
$lowHangingFruitsLimited = $lowHangingFruits | Sort-Object Count, Name | select -first 100
if ($lowHangingFruitsLimited.Count -gt 0)
{
Write-Host "Low hanging fruits (first $($lowHangingFruitsLimited.Count) of $($lowHangingFruits.Count) files with 1 or less issues):"
$lowHangingFruitsLimited | % {
$null = $_.Name -match '.*(?<filename>dev\\src\\Order.*)'
Write-Host $Matches.filename, $_.Count
}
}
}
else
{
MODE CON COLS=160 LINES=60
cls
Write-Host "Running StyleCop..."
$command = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe StyleCop.build /nologo /verbosity:m /p:StyleCopMaxViolationCount=50000"
$filesWithErrors = Invoke-Expression -Command $command | group { $_ -replace "\(.*$", "" } | where { $ignoredFiles.ContainsKey($_.Name.ToLower()) -eq $false }
if ($filesWithErrors.length -eq 0)
{
Write-Host "No style errors were reported."
}
foreach ($file in $filesWithErrors)
{
Write-Host -NoNewline "File "
Write-Host -NoNewLine $file.Name -ForegroundColor Yellow
Write-Host -NoNewLine " reported "
Write-Host -NoNewLine $file.Count
Write-Host " error(s)."
if ($verbose -or $v)
{
Write-Host " Detailed breakdown:"
foreach ($styleErrorInfo in $file.Group | sort)
{
$justStyleErrorAndLocation = $styleErrorInfo -replace "^.*\.cs", "" -replace "\[.*$", ""
Write-Host -NoNewLine " "
Write-Host $justStyleErrorAndLocation
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment