Skip to content

Instantly share code, notes, and snippets.

@wachulski
Last active January 7, 2019 11:47
Show Gist options
  • Save wachulski/7c9799e075d6827ba2accbcd99061683 to your computer and use it in GitHub Desktop.
Save wachulski/7c9799e075d6827ba2accbcd99061683 to your computer and use it in GitHub Desktop.
# Template analysis script that runs SonarQube analysis for .NET projects
# More details in http://blog.pragmasoft.pl/software/2018-10-10-sonarqube-2-setup-environment/
param(
$version = '', # SQ records analysis history, each having its version label
$authToken = '' # SonarCloud.io auth, generated in SQ administration
)
# retrieve all solution files that exist in the project subtree
$slns = Get-ChildItem *.sln -r
foreach ($sln in $slns) {
$sqProj = $sln.Name -replace '\.sln$',''
Write-Host "Analysing $sqProj"
# initiating - passing analysis parameters to 'begin' step
SonarScanner.MSBuild begin `
/key:"$($sqProj -replace '\.', '_')" `
/name:"$($sqProj -replace '\.', ' ')" `
/version:"$version" `
/d:$("sonar.cs.dotcover.reportsPaths=dotCover.html")
# the following is needed for https://SonarCloud.io
#/d:"sonar.host.url=https://sonarqube.com" `
#/d:"sonar.organization=ORGANIZATION_WE_CREATED_AT_SONARCLOUD" `
#/d:"sonar.login=$authToken"
# actual building
nuget restore $sln
MSBuild $sln /p:Configuration=Release
# running unit tests and calculating code coverage
$testPathPattern = "$($sln.Directory)\**\bin\Release\net452\*.Tests.dll"
$dlls = Get-ChildItem $testPathPattern -r
$xUnitRelativePath = ".\xunit.runner.console\tools\net452\xunit.console.exe"
$runner = Resolve-Path $xUnitRelativePath
$coverConfig = Get-ChildItem $("$($sln.Directory)\*.Tests\coverage.xml")
dotCover analyse $coverConfig.FullName `
/TargetExecutable="$runner" `
/TargetArguments="$dlls" `
/ReportType=HTML `
/Output=dotCover.html
# collecting results and sending to SonarQube server
SonarScanner.MSBuild end
Write-Host "Analysing $sqProj completed!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment