/
psakefile.ps1
310 lines (283 loc) · 12.7 KB
/
psakefile.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
Properties {
$rootDir = Split-Path $psake.build_script_file
$solutionFileCS = "$rootDir\CodeCracker.CSharp.sln"
$solutionFileVB = "$rootDir\CodeCracker.VisualBasic.sln"
$srcDir = "$rootDir\src"
$testDir = "$rootDir\test"
$isAppVeyor = $env:APPVEYOR -eq $true
$slns = ls "$rootDir\*.sln"
$packagesDir = "$rootDir\packages"
$buildNumber = [Convert]::ToInt32($env:APPVEYOR_BUILD_NUMBER).ToString("0000")
$nuspecPathCS = "$rootDir\src\CSharp\CodeCracker\CodeCracker.nuspec"
$nuspecPathVB = "$rootDir\src\VisualBasic\CodeCracker\CodeCracker.nuspec"
$nugetPackagesExe = "$packagesDir\NuGet.CommandLine.4.6.2\tools\NuGet.exe"
$nugetExe = if (Test-Path $nugetPackagesExe) { $nugetPackagesExe } else { 'nuget' }
$nupkgPathCS = "$rootDir\src\CSharp\CodeCracker.CSharp.{0}.nupkg"
$nupkgPathVB = "$rootDir\src\VisualBasic\CodeCracker.VisualBasic.{0}.nupkg"
$xunitConsoleExe = "$packagesDir\xunit.runner.console.2.3.1\tools\net452\xunit.console.x86.exe"
$openCoverExe = "$packagesDir\OpenCover.4.6.519\tools\OpenCover.Console.exe"
$dllCS = "CodeCracker.CSharp.dll"
$dllVB = "CodeCracker.VisualBasic.dll"
$dllCommon = "CodeCracker.Common.dll"
$testDllCS = "CodeCracker.Test.CSharp.dll"
$testDllVB = "CodeCracker.Test.VisualBasic.dll"
$testDirCS = "$testDir\CSharp\CodeCracker.Test\bin\Release"
$testDirVB = "$testDir\VisualBasic\CodeCracker.Test\bin\Release"
$projectDirVB = "$srcDir\VisualBasic\CodeCracker"
$projectFileVB = "$projectDirVB\CodeCracker.vbproj"
$releaseDirVB = "$projectDirVB\bin\Release"
$projectDirCS = "$srcDir\CSharp\CodeCracker"
$projectFileCS = "$projectDirCS\CodeCracker.csproj"
$releaseDirCS = "$projectDirCS\bin\Release"
$logDir = "$rootDir\log"
$outputXml = "$logDir\CodeCoverageResults.xml"
$reportGeneratorExe = "$packagesDir\ReportGenerator.3.1.2\tools\ReportGenerator.exe"
$coverageReportDir = "$logDir\codecoverage\"
$coverallsNetExe = "$packagesDir\coveralls.io.1.4.2\tools\coveralls.net.exe"
$ilmergeExe = "$packagesDir\ilmerge.2.14.1208\tools\ILMerge.exe"
$isRelease = $isAppVeyor -and (($env:APPVEYOR_REPO_BRANCH -eq "release") -or ($env:APPVEYOR_REPO_TAG -eq "true"))
$isPullRequest = $env:APPVEYOR_PULL_REQUEST_NUMBER -ne $null
$tempDir = Join-Path "$([System.IO.Path]::GetTempPath())" "CodeCracker"
# msbuild hack necessary until https://github.com/psake/psake/issues/201 is fixed:
$msbuild64 = Resolve-Path "$(if (${env:ProgramFiles(x86)}) { ${env:ProgramFiles(x86)} } else { $env:ProgramFiles } )\Microsoft Visual Studio\2017\*\MSBuild\15.0\Bin\msbuild.exe"
$msbuild32 = Resolve-Path "$(if (${env:ProgramFiles(x86)}) { ${env:ProgramFiles(x86)} } else { $env:ProgramFiles } )\Microsoft Visual Studio\2017\*\MSBuild\15.0\Bin\msbuild.exe"
$msbuild = if ($msbuild64) { $msbuild64 } else { $msbuild32 }
}
FormatTaskName (("-"*25) + "[{0}]" + ("-"*25))
Task Default -Depends Build, Test
Task Rebuild -Depends Clean, Build
Task Restore {
Foreach($sln in $slns) {
RestorePkgs $sln
}
}
Task Prepare-Build -depends Restore, Update-Nuspec
Task Build -depends Prepare-Build, Build-Only
Task Build-CS -depends Prepare-Build, Build-Only-CS
Task Build-VB -depends Prepare-Build, Build-Only-VB
Task Build-Only -depends Build-Only-CS, Build-Only-VB
Task Build-Only-CS -depends Build-MSBuild-CS, ILMerge-CS
Task Build-MSBuild-CS {
if ($isAppVeyor) {
Exec { . $msbuild $solutionFileCS /m /verbosity:minimal /p:Configuration=ReleaseNoVsix /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" }
} else {
Exec { . $msbuild $solutionFileCS /m /verbosity:minimal /p:Configuration=ReleaseNoVsix }
}
}
Task Build-Only-VB -depends Build-MSBuild-VB, ILMerge-VB
Task Build-MSBuild-VB {
if ($isAppVeyor) {
Exec { . $msbuild $solutionFileVB /m /verbosity:minimal /p:Configuration=ReleaseNoVsix /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" }
} else {
Exec { . $msbuild $solutionFileVB /m /verbosity:minimal /p:Configuration=ReleaseNoVsix }
}
}
Task ILMerge-VB { ILMerge $releaseDirVB $dllVB $projectFileVB $projectDirVB }
Task ILMerge-CS { ILMerge $releaseDirCS $dllCS $projectFileCS $projectDirCS }
function ILMerge($releaseDir, $dll, $projectFile, $projectDir) {
Write-Host "IL Merge:"
$mergedDir = $tempDir
if (!(Test-Path $mergedDir)) { mkdir "$mergedDir" }
$inputDll = "$releaseDir\$dll"
$inputDllCommon = "$releaseDir\$dllCommon"
$pdbCommon = Change-Extension $inputDllCommon "pdb"
if (Test-Path $inputDllCommon) {
if ((ls $inputDllCommon).LastWriteTime -gt (ls $inputDll).LastWriteTime) {
# common is newer, but no changes on main dll
Write-Host "Common dll is newer than $inputDll, stopping IL merge."
return
}
} else {
# no common dll, can't merge
Write-Host "Can't find common dll, stopping IL merge."
return
}
$mergedDll = "$mergedDir\$dll"
[xml]$proj = cat $projectFile
$libs = @()
foreach ($ref in $proj.Project.ItemGroup.Reference.HintPath) {
$dir += [System.IO.Path]::GetDirectoryName("$projectDir\$ref")
$libs += "/lib:`"$([System.IO.Path]::GetDirectoryName("$projectDir\$ref"))`" "
}
Exec { . $ilmergeExe $libs /out:"$mergedDll" "$inputDll" "$inputDllCommon" }
$releaseMergedDir = "$releaseDir\merged"
if (!(Test-Path $releaseMergedDir)) { mkdir $releaseMergedDir | Out-Null }
cp $mergedDll "$releaseMergedDir\" -Force
Write-Host " $dll -> $releaseMergedDir\$dll"
$mergedPdb = Change-Extension $mergedDll "pdb"
cp $mergedPdb "$releaseMergedDir\" -Force
$pdb = (ls $mergedPdb).Name
Write-Host " $pdb -> $releaseMergedDir\$pdb"
}
function Change-Extension ($filename, $extension) {
Join-Path "$([System.IO.Path]::GetDirectoryName($filename))" "$([System.IO.Path]::GetFileNameWithoutExtension($filename)).$extension"
}
Task Clean {
Exec { . $msbuild $solutionFileCS /t:Clean /v:quiet }
Exec { . $msbuild $solutionFileVB /t:Clean /v:quiet }
}
Task Set-Log {
if ((Test-Path $logDir) -eq $false)
{
Write-Host -ForegroundColor DarkBlue "Creating log directory $logDir"
mkdir $logDir | Out-Null
}
}
Task Test-Acceptance -depends Test {
. "$rootDir\test\CSharp\AnalyzeCoreFx.ps1"
}
Task Test -depends Set-Log {
RunTestWithCoverage "$testDirCS\$testDllCS", "$testDirVB\$testDllVB"
}
Task Test-VB -depends Set-Log {
RunTestWithCoverage "$testDirVB\$testDllVB"
}
Task Test-CSharp -depends Set-Log {
RunTestWithCoverage "$testDirCS\$testDllCS"
}
Task Test-No-Coverage -depends Test-No-Coverage-CSharp, Test-No-Coverage-VB
Task Test-No-Coverage-VB {
RunTest "$testDirVB\$testDllVB"
}
Task Test-No-Coverage-CSharp {
RunTest "$testDirCS\$testDllCS"
}
Task Update-Nuspec -precondition { return $isAppVeyor -and ($isRelease -ne $true) } -depends Update-Nuspec-CSharp, Update-Nuspec-VB
Task Update-Nuspec-CSharp -precondition { return $isAppVeyor -and ($isRelease -ne $true) } {
UpdateNuspec $nuspecPathCS "C#"
}
Task Update-Nuspec-VB -precondition { return $isAppVeyor -and ($isRelease -ne $true) } {
UpdateNuspec $nuspecPathVB "VB"
}
Task Pack-Nuget -precondition { return $isAppVeyor } -depends Pack-Nuget-Csharp, Pack-Nuget-VB
Task Pack-Nuget-CSharp -precondition { return $isAppVeyor } {
PackNuget "C#" "$rootDir\src\CSharp" $nuspecPathCS $nupkgPathCS
}
Task Pack-Nuget-VB -precondition { return $isAppVeyor } {
PackNuget "VB" "$rootDir\src\VisualBasic" $nuspecPathVB $nupkgPathVB
}
Task Pack-Nuget-Force -depends Pack-Nuget-Csharp-Force, Pack-Nuget-VB-Force
Task Pack-Nuget-Csharp-Force {
PackNuget "C#" "$rootDir\src\CSharp" $nuspecPathCS $nupkgPathCS
}
Task Pack-Nuget-VB-Force {
PackNuget "VB" "$rootDir\src\VisualBasic" $nuspecPathVB $nupkgPathVB
}
Task Count-Analyzers {
$count = $(ls $rootDir\src\*.cs -Recurse | ? { $_.Name.contains('Analyzer') } | ? { !((cat $_) -match 'abstract class') }).count
Write-Host "Found $count C# Analyzers"
$count = $(ls $rootDir\src\*.cs -Recurse | ? { $_.Name.contains('CodeFix') } | ? { !((cat $_) -match 'abstract class') }).count
Write-Host "Found $count C# Code Fixes"
$count = $(ls $rootDir\src\*.cs -Recurse | ? { $_.Name.contains('FixAll') } | ? { !((cat $_) -match 'abstract class') }).count
Write-Host "Found $count C# Code Fixes All"
$count = $(ls $rootDir\src\*.vb -Recurse | ? { $_.Name.contains('Analyzer') } | ? { !((cat $_) -match 'mustinherit class') }).count
Write-Host "Found $count VB Analyzers"
$count = $(ls $rootDir\src\*.vb -Recurse | ? { $_.Name.contains('CodeFix') } | ? { !((cat $_) -match 'mustinherit class') }).count
Write-Host "Found $count VB Code Fixes"
$count = $(ls $rootDir\src\*.vb -Recurse | ? { $_.Name.contains('FixAll') } | ? { !((cat $_) -match 'mustinherit class') }).count
Write-Host "Found $count VB Code Fixes All"
}
Task Update-ChangeLog {
# invoke-psake default.ps1 -tasklist update-changelog -parameters @{"token"="<token>"}
echo $token
return
Exec {
github_changelog_generator code-cracker/code-cracker --no-pull-requests --no-issues-wo-labels --exclude-labels "Can't repro","update readme",decision,docs,duplicate,question,invalid,wontfix,Duplicate,Question,Invalid,Wontfix -t $token
}
}
Task Echo { echo echo }
function PackNuget($language, $dir, $nuspecFile, $nupkgFile) {
Write-Host "Packing nuget for $language..."
[xml]$xml = cat "$nuspecFile"
$nupkgFile = $nupkgFile -f $xml.package.metadata.version
. $nugetExe pack "$nuspecFile" -OutputDirectory "$dir"
$nuspecFileName = (ls $nuspecFile).Name
Write-Host " $nuspecFileName ($language/$($xml.package.metadata.version)) -> $nupkgFile"
if ($isAppVeyor) {
Write-Host "Pushing nuget artifact for $language..."
appveyor PushArtifact $nupkgFile
Write-Host "Nupkg pushed for $language!"
}
}
function UpdateNuspec($nuspecPath, $language) {
write-host "Updating version in nuspec file for $language to $buildNumber"
[xml]$xml = cat $nuspecPath
$xml.package.metadata.version+="-z$buildNumber"
write-host "Nuspec version will be $($xml.package.metadata.version)"
$xml.Save($nuspecPath)
write-host "Nuspec saved for $language!"
}
function RestorePkgs($sln) {
Write-Host "Restoring $sln..." -ForegroundColor Green
Retry {
. $nugetExe restore "$sln" -NonInteractive -ConfigFile "$rootDir\nuget.config"
if ($LASTEXITCODE) { throw "Nuget restore for $sln failed." }
}
}
function Retry {
Param (
[parameter(Position=0,Mandatory=1)]
[ScriptBlock]$cmd,
[parameter(Position=1,Mandatory=0)]
[int]$times = 3
)
$retrycount = 0
while ($retrycount -lt $times){
try {
& $cmd
if (!$?) {
throw "Command failed."
}
return
}
catch {
Write-Host -ForegroundColor Red "Failed: ($($_.Exception.Message)), retrying."
}
$retrycount++
}
throw "Command '$($cmd.ToString())' failed."
}
function TestPath($paths) {
$notFound = @()
foreach($path in $paths) {
if ((Test-Path $path) -eq $false)
{
$notFound += $path
}
}
$notFound
}
function RunTest($fullTestDllPath) {
if ($isAppVeyor) {
. $xunitConsoleExe $fullTestDllPath -appveyor -nologo -quiet
} else {
. $xunitConsoleExe $fullTestDllPath -nologo -quiet
}
}
function RunTestWithCoverage($fullTestDllPaths) {
$notFoundPaths = TestPath $openCoverExe, $xunitConsoleExe, $reportGeneratorExe
if ($notFoundPaths.length -ne 0) {
Write-Host -ForegroundColor DarkRed "Paths not found: "
foreach($path in $notFoundPaths) {
Write-Host -ForegroundColor DarkRed " $path"
}
throw "Paths for test executables not found"
}
$targetArgs = ""
Foreach($fullTestDllPath in $fullTestDllPaths) {
$targetArgs += $fullTestDllPath + " "
}
$targetArgs = $targetArgs.Substring(0, $targetArgs.Length - 1)
$appVeyor = ""
if ($isAppVeyor) {
$appVeyor = " -appveyor"
}
$arguments = '-register:user', "`"-target:$xunitConsoleExe`"", "`"-targetargs:$targetArgs $appVeyor -noshadow -parallel none -nologo`"", "`"-filter:+[CodeCracker*]* -[CodeCracker.Test*]*`"", "`"-output:$outputXml`"", '-coverbytest:*.Test.*.dll', '-log:All', '-returntargetcode'
Exec { . $openCoverExe $arguments }
Write-Host -ForegroundColor DarkBlue "Exporting code coverage report"
Exec { . $reportGeneratorExe -verbosity:Info -reports:$outputXml -targetdir:$coverageReportDir }
if ($env:COVERALLS_REPO_TOKEN -ne $null) {
Write-Host -ForegroundColor DarkBlue "Uploading coverage report to Coveralls.io"
Exec { . $coverallsNetExe --opencover $outputXml --full-sources }
}
}