-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathRunResharperCodeAnalysisTool.ps1
More file actions
197 lines (148 loc) · 7.27 KB
/
RunResharperCodeAnalysisTool.ps1
File metadata and controls
197 lines (148 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
$solutionOrProjectPath = Get-VstsInput -Name "solutionOrProjectPath"
$additionalArguments = Get-VstsInput -Name "additionalArguments"
$commandLineInterfacePath = Get-VstsInput -Name "commandLineInterfacePath"
$failBuildLevelSelector = Get-VstsInput -Name "failBuildLevelSelector"
$failBuildOnCodeIssues = Get-VstsInput -Name "failBuildOnCodeIssues"
$buildId = Get-VstsInput -Name "buildId"
$inspectCodeResultsPathOverride = Get-VstsInput -Name "resultsOutputFilePath"
$resharperNugetVersion = Get-VstsInput -Name "resharperNugetVersion"
if(!$solutionOrProjectPath) {
Throw [System.IO.FileNotFoundException] "solutionOrProjectPath is mandatory, please provide a value."
}
if(!$failBuildLevelSelector) {
$failBuildLevelSelector = "Warning"
}
if(!$failBuildOnCodeIssues) {
$failBuildOnCodeIssues = "true"
}
if(!$buildId) {
$buildId = "Unlabeled_Build"
}
if(!$resharperNugetVersion) {
$resharperNugetVersion = "Latest"
}
function Set-Results {
param(
[string]
$summaryMessage,
[ValidateSet("Succeeded", "Failed")]
[string]
$buildResult
)
Write-Output ("##vso[task.complete result={0};]{1}" -f $buildResult, $summaryMessage)
Add-Content $summaryFilePath ($summaryMessage)
}
# Gather inputs
$inspectCodeExePath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($commandLineInterfacePath, "InspectCode.exe"));
$tempDownloadFolder = $Env:BUILD_STAGINGDIRECTORY
if(!(Test-Path $inspectCodeExePath)) {
# Download Resharper from nuget
$useSpecificNuGetVersion = $resharperNugetVersion -and $resharperNugetVersion -ne "Latest"
$downloadMessage = "No pre-installed Resharper CLT was found"
if($useSpecificNuGetVersion){
$downloadMessage += ", downloading version $resharperNugetVersion from nuget.org..."
} else {
$downloadMessage += ", downloading the latest from nuget.org..."
}
Write-Output $downloadMessage
$nugetExeLocation = [System.IO.Path]::Combine($PSScriptRoot, ".nuget")
Copy-Item $nugetExeLocation\* $tempDownloadFolder
$nugetExeLocation = [System.IO.Path]::Combine($tempDownloadFolder, "nuget.exe")
$nugetArguments = "install JetBrains.ReSharper.CommandLineTools -source https://api.nuget.org/v3/index.json"
if($useSpecificNuGetVersion){
$nugetArguments += " -Version $resharperNugetVersion"
}
Start-Process -FilePath "$nugetExeLocation" -ArgumentList $nugetArguments -WorkingDirectory $tempDownloadFolder -Wait
$resharperPreInstalledDirectoryPath = [System.IO.Directory]::EnumerateDirectories($tempDownloadFolder, "*JetBrains*")[0]
if(!(Test-Path $resharperPreInstalledDirectoryPath)) {
Throw [System.IO.FileNotFoundException] "InspectCode.exe was not found at $inspectCodeExePath or $resharperPreInstalledDirectoryPath"
}
Write-Output "Resharper CLT downloaded"
$commandLineInterfacePath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($resharperPreInstalledDirectoryPath, "tools"));
$inspectCodeExePath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($commandLineInterfacePath, "InspectCode.exe"));
}
if(!(Test-Path $inspectCodeExePath)) {
Throw [System.IO.FileNotFoundException] "InspectCode.exe was not found at $inspectCodeExePath"
}
[string] $solutionOrProjectFullPath = [System.IO.Path]::GetFullPath($solutionOrProjectPath.Replace("`"",""));
if(!(Test-Path $solutionOrProjectFullPath)) {
Throw [System.IO.FileNotFoundException] "No solution or project found at $solutionOrProjectFullPath"
}
[string] $inspectCodeResultsPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($commandLineInterfacePath, "Reports\CodeInspection_$buildId.xml"));
if($inspectCodeResultsPathOverride){
if(!$inspectCodeResultsPathOverride.EndsWith(".xml")) {
$inspectCodeResultsPathOverride += ".xml"
}
$inspectCodeResultsPath = $inspectCodeResultsPathOverride
}
$severityLevels = @{"Hint" = 0; "Suggestion" = 1; "Warning" = 2; "Error" = 3}
Write-Verbose "Using Resharper Code Analysis found at '$inspectCodeExePath'"
Write-Output "Inspecting code for $solutionOrProjectPath"
# Run code analysis
$additionalArguments = $additionalArguments -replace '"',''''
$arguments = """$solutionOrProjectFullPath"" /o:""$inspectCodeResultsPath"" ""$additionalArguments"""
Write-Output "Invoking InspectCode.exe using arguments $arguments"
$stdOutputFile = "./stdout.txt"
Start-Process -FilePath $inspectCodeExePath -ArgumentList $arguments -Wait -RedirectStandardOutput $stdOutputFile
if(Test-Path $stdOutputFile) {
$stdOutputFileContent = Get-Content "$stdOutputFile"
Write-Output $stdOutputFileContent
}
# Analyse results
$xmlContent = [xml] (Get-Content "$inspectCodeResultsPath")
$issuesTypesXpath = "/Report/IssueTypes//IssueType"
$issuesTypesElements = $xmlContent | Select-Xml $issuesTypesXpath | Select -Expand Node
$issuesXpath = "/Report/Issues//Issue"
$issuesElements = $xmlContent | Select-Xml $issuesXpath | Select -Expand Node
$filteredElements = New-Object System.Collections.Generic.List[System.Object]
$issuesTypesHashTable = @{ }
foreach($issueType in $issuesTypesElements) {
$issuesTypesHashTable.Add($issueType.Attributes["Id"].Value, $issueType.Attributes["Severity"].Value);
}
foreach($issue in $issuesElements) {
$severity = $issuesTypesHashTable[$issue.Attributes["TypeId"].Value]
if($severity -eq "INVALID_SEVERITY") {
$severity = $issue.Attributes["Severity"].Value
}
$severityLevel = $severityLevels[$severity]
if($severityLevel -ge $severityLevels[$failBuildLevelSelector]) {
$item = New-Object -TypeName PSObject -Property @{
'Severity' = $severity
'Message' = $issue.Attributes["Message"].Value
'File' = $issue.Attributes["File"].Value
'Line' = $issue.Attributes["Line"].Value
}
$filteredElements.Add($item)
}
}
# Report results output
foreach ($issue in $filteredElements | Sort-Object Severity -Descending) {
$errorType = "warning"
if($issue.Severity -eq "Error"){
$errorType = "error"
}
Write-Output ("##vso[task.logissue type={0};sourcepath={1};linenumber={2};columnnumber=1]R# {3}" -f $errorType, $issue.File, $issue.Line, $issue.Message)
}
$summaryFilePath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($tempDownloadFolder, "Summary.md"));
New-Item $summaryFilePath -type file -force
$summaryMessage = ""
[bool] $failBuildOnCodeIssuesBool = [System.Convert]::ToBoolean($failBuildOnCodeIssues)
if ($failBuildOnCodeIssuesBool) {
if($filteredElements.Count -eq 0) {
Set-Results -summaryMessage "No code quality issues found" -buildResult "Succeeded"
} elseif($filteredElements.Count -eq 1) {
Set-Results -summaryMessage "One code quality issue found" -buildResult "Failed"
} else {
$summaryMessage = "{0} code quality issues found" -f $filteredElements.Count
Set-Results -summaryMessage $summaryMessage -buildResult "Failed"
}
} else {
$summaryMessage = "{0} code quality issues found" -f $filteredElements.Count
Set-Results -summaryMessage $summaryMessage -buildResult "Succeeded"
}
Write-Output "##vso[task.addattachment type=Distributedtask.Core.Summary;name=Code Quality Analysis;]$summaryFilePath"
If (Test-Path $inspectCodeResultsPath) {
If (!$inspectCodeResultsPathOverride) {
Remove-Item $inspectCodeResultsPath
}
}