-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.ps1
209 lines (171 loc) · 5.24 KB
/
analyze.ps1
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
198
199
200
201
202
203
204
205
206
207
208
209
# param
# (
# [Parameter(Mandatory=$True)][string]$AWSAccessKey,
# [Parameter(Mandatory=$True)][string]$AWSSecretKey,
# [Parameter(Mandatory=$True)][string]$AWSSessionToken
# )
param
(
[Parameter(Mandatory=$False)][bool]$Baseline,
[Parameter(Mandatory=$False)][bool]$Diagnostics=$true,
[Parameter(Mandatory=$False)][bool]$Build=$false,
[Parameter(Mandatory=$False)][string]$nDepend=".\NDepend.Console.exe"
[Parameter(Mandatory=$False)][string]$targetFile=".\NDepend.Console.exe"
)
$AWSAccessKey = ''
$AWSSecretKey = ''
$AWSSessionToken = ''
# Validate the targetFile is an absolute path
if ([System.IO.Path]::IsPathRooted($targetFile) -eq $false)
{
$targetFile = [System.IO.Path]::GetFullPath($targetFile)
}
$projectFolder = Split-Path -Path $targetFile
$outputFolder = "nDepend.Reports"
$targetBucket = "ndepend-reports"
$absoluteReportPath = "$projectFolder\$outputFolder\"
$previous = ""
Clear-Host
Import-Module AWSPowershell
Set-AWSCredentials -AccessKey $AWSAccessKey -SecretKey $AWSSecretKey -SessionToken $AWSSessionToken
function BackupBaselineReportToS3()
{
$latestReport = GetLatestReport
BackupReportToS3 "$absoluteReportPath$latestReport" "baseline"
}
function BackupSuccessfulReportToS3()
{
if (Test-Path $projectFolder\$outputFolder\*.ndar)
{
$latestReport = GetLatestReport
$targetFileKey = iex "git rev-parse head"
BackupReportToS3 "$absoluteReportPath$latestReport" $targetFileKey
}
}
function GetLatestReport()
{
$relativeReportPath = Resolve-Path -Relative $absoluteReportPath
return (Get-ChildItem -Filter "$relativeReportPath\*.ndar" | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1).Name
}
function BackupReportToS3([Parameter(Mandatory=$True)][string]$sourceFilePath, [Parameter(Mandatory=$True)][string]$targetFileKey)
{
Write-Host "Backing up NDAR report to S3 by filepath [$sourceFilePath] to [$targetFileKey]"
Write-Host "Source file: $sourceFilePath"
Write-Host "Target bucket: $targetBucket"
Write-Host "Target file key: $targetFileKey"
$keyPrefix = GetRepositoryName
Write-Host "Key prefix: $keyPrefix"
Write-S3Object -BucketName $targetBucket -File $sourceFilePath -Key "$keyPrefix/$targetFileKey" -Region ap-southeast-2
Write-Host "File backed up to S3 successfully"
}
function GetRepositoryName
{
$repositoryUrl = iex "git config --get remote.origin.url"
Write-Host "Repository url: $repositoryUrl"
$parts = $repositoryUrl.Split('/')
$last = $parts[$parts.Length-1]
$name = $last.Split('.')[0];
return $name;
}
function RestoreLatestReportFromS3()
{
try
{
$sourceFileKey = iex "git rev-parse head~1"
return RestoreReportFromS3 $sourceFileKey
}
catch
{
if ($_.Exception.Message -eq "The specified key does not exist.")
{
$sourceFileKey = "baseline"
return RestoreReportFromS3 $sourceFileKey
}
Write-Host $_.Exception.Message
Exit
}
}
function RestoreReportFromS3([Parameter(Mandatory=$True)][string]$sourceFileKey)
{
Write-Host "Restoring NDAR report from S3 by key [$sourceFileKey]"
Write-Host "Source bucket: $targetBucket"
$keyPrefix = GetRepositoryName
Write-Host "Key prefix: $keyPrefix"
$sourceFilename = "$sourceFileKey.ndar"
Write-Host "Source file: $sourceFilename"
#Read-S3Object -BucketName $targetBucket -File "$absoluteReportPath$sourceFilename" -Key "$sourceFileKey" -Region ap-southeast-2 | out-null
Read-S3Object -BucketName $targetBucket -File $sourceFilename -Key "$keyPrefix/$sourceFileKey" -Region ap-southeast-2 | out-null
return $sourceFilename
}
function WriteChildProcessOutput
{
PROCESS
{
if ([bool]$Diagnostics -eq $true)
{
ForEach-Object {
if ($_ -is [System.Management.Automation.ErrorRecord])
{
Write-Error $_
}
else
{
Write-Host $_ -ForegroundColor Green
}
}
}
}
}
function AnalyseSolution([Parameter(Mandatory=$True)][string]$previousFilename)
{
Write-Host "Analysing Solution and comparing to:- $previousFilename"
& $nDepend $targetFile /OutDir .\$outputFolder /AnalysisResultToCompareWith ..\$previousFilename 2>&1 | WriteChildProcessOutput
return $LASTEXITCODE
}
function AnalyseBaseline()
{
Write-Host "Analysing Baseline"
& $nDepend $targetFile /OutDir .\$outputFolder | WriteChildProcessOutput
return $LASTEXITCODE
}
function ClearOutput
{
Write-Host "Cleaning up previous analysis results from: $absoluteReportPath"
Remove-Item -Recurse -Force $absoluteReportPath -ErrorAction SilentlyContinue
}
ClearOutput
if ([bool]$Build -eq $true)
{
Write-Host "Building solution..."
.\build.ps1 | out-null
}
if ([bool]$Baseline -eq $true)
{
$x = AnalyseBaseline
if ($x -eq 0)
{
Write-Host "Baseline analysis complete, all checks passed."
BackupBaselineReportToS3
}
else
{
Write-Error "Baseline analysis complete, check(s) failed. See log for further details."
exit
}
}
#Download the previous comparison file (or get baseline if there isn't one)
$previous = RestoreLatestReportFromS3
# Run the comparison
Write-Host "Analysing: - $targetFile"
Write-Host "ProjectFolder: - $projectFolder"
$result = AnalyseSolution $previous
# If success then copy current.ndar over previous.ndar and backup history
if ($result -eq 0)
{
Write-Host "Analysis complete, all checks passed."
BackupSuccessfulReportToS3
}
else
{
Write-Error "Analysis complete, check(s) failed. See log for further details."
}