Skip to content

Instantly share code, notes, and snippets.

@k0stya
Created November 22, 2013 19:04
Show Gist options
  • Save k0stya/7605121 to your computer and use it in GitHub Desktop.
Save k0stya/7605121 to your computer and use it in GitHub Desktop.
Param(
[parameter(Mandatory=$true)]
[alias("f")]
$FilePath,
[alias("b")]
$BuildTypeId,
[alias("s")]
$ServerUrl,
[parameter()]
[alias("o")]
$RawOutputPath
)
$warnings = @(Get-Content -ErrorAction Stop $FilePath | # Get the file content
Where {$_ -match '^.*warning CS.*$'} | # Extract lines that match warnings
%{ $_.trim() -replace "^\s*\d+>","" } | # Strip out any project number and caret prefixes
sort-object | Get-Unique -asString) # remove duplicates by sorting and filtering for unique strings
$count = $warnings.Count
# raw output
Write-Host "MSBuild Warnings - $count warnings ==================================================="
$warnings | % { Write-Host " * $_" }
try{
$previousWarnings = ((new-object Net.WebClient).DownloadString("$ServerUrl/repository/download/$BuildTypeId/.lastSuccessful/$RawOutputPath" + "?guest=1").Trim()) -split "`r`n"
}catch{
$previousWarnings = @()
Write-Host $Error
}
$wcount = 0
$new = @()
$old = @()
while($warnings.Length -ne $wcount){
if($previousWarnings -notcontains $warnings[$wcount])
{
$new+=$warnings[$wcount]
} else{
$old+=$warnings[$wcount]
}
$wcount += 1
}
#TeamCity output
Write-Host ("##teamcity[buildStatus text='{build.status.text}, Build warnings: $count (+" + $new.Length + "/-" + ($previousWarnings.Length - $old.Length) + ")']")
Write-Host "##teamcity[buildStatisticValue key='buildWarnings' value='$count']"
Write-Host $RawOutputPath
# file output
if($RawOutputPath){
Write-Host "Writing to row file"
$stream = [System.IO.StreamWriter] $RawOutputPath
$warnings | % { $stream.WriteLine("$_") }
$stream.Close()
}
# html report output
$check = Test-Path -PathType Container BuildWarningReport
if($check -eq $false){
New-Item 'BuildWarningReport' -type Directory
}
$stream = [System.IO.StreamWriter] "BuildWarningReport/index.html"
$stream.WriteLine("<html><head></head><body><h1>$count Build Warnings</h1>")
if($new.Length -gt 0){
$stream.WriteLine("New warnings:<ul>")
$new | % { $stream.WriteLine("<li style='color:red'>$_</li>") }
$stream.WriteLine("</ul>")
}
if($old.Length -gt 0){
$stream.WriteLine("Old warnings:<ul>")
$stream.WriteLine("<ul>")
$old | % { $stream.WriteLine("<li>$_</li>") }
$stream.WriteLine("</ul>")
}
$stream.WriteLine("</body></html>")
$stream.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment