This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathbuild.ps1
259 lines (214 loc) · 7.76 KB
/
build.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
properties {
$product = 'FluentSecurity'
$version = '2.1.0'
$label = ''
$configuration = 'release'
$useVerbose = $false
$rootDir = '.'
$sourceDir = "$rootDir"
$buildDir = "$rootDir\Build"
$outputDir = "$buildDir\Output"
$reportsDir = "$buildDir\Reports"
$artifactsDir = "$buildDir\Artifacts"
$artifactsName = "$product-$version-$configuration" -replace "\.","_"
$buildNumber = $null
$branch = $null
$dotCover = $null
$mygetApiKey = $null
$nugetApiKey = $null
$copyright = 'Copyright (c) 2009-2013, Kristoffer Ahl'
$setupMessage = 'Executed Setup!'
$cleanMessage = 'Executed Clean!'
$compileMessage = 'Executed Compile!'
$testMessage = 'Executed Test!'
$packMessage = 'Executed Pack!'
$deployMessage = 'Executed Deploy!'
}
task default -depends Run
task Run {
if ($branch -ne $null) {
$branch = $branch -replace "refs/heads/",""
} else {
try { $branch = (git rev-parse --abbrev-ref HEAD) } catch { $branch = 'unknown' }
}
$script:buildId = ([guid]::NewGuid().ToString().Substring(0,5))
$script:buildVersion = ?: {$buildNumber -ne $null} {"$version.$buildNumber"} {"$version"}
$script:buildLabel = ?: {$label -ne $null -and $label -ne ''} {"$version-$label"} {"$version"}
if ($branch -ne 'master') {
$script:buildLabel = ?: {$buildNumber -ne $null} {"$buildLabel-build$buildNumber"} {"$buildLabel-buildx$buildId"}
}
# SemVer : 2.0.0-alpha.4+build.3 | 2.0.0-alpha.4
# NuGet : 2.0.0-alpha4-build3 | 2.0.0-alpha4
# SemVer : 2.0.0+build.3 | 2.0.0
# NuGet : 2.0.0-build3 | 2.0.0
Write-Host "Running build" -fore Yellow
Write-Host "Product: $product" -fore Yellow
Write-Host "Version: $version" -fore Yellow
Write-Host "Label: $label" -fore Yellow
Write-Host "Build version: $buildVersion" -fore Yellow
Write-Host "Build label: $buildLabel" -fore Yellow
Write-Host "Branch: $branch" -fore Yellow
Invoke-Task Deploy
}
task Setup {
nuget_exe install ".nuget\packages.config" -outputdirectory "packages"
generate_assemblyinfo `
-file "$sourceDir\SharedAssemblyInfo.cs" `
-description "$product ($configuration)" `
-company $company `
-product $product `
-version $version `
-buildVersion $buildVersion `
-buildLabel $buildLabel `
-clsCompliant "false" `
-copyright $copyright
$setupMessage
}
task Clean {
delete_directory $artifactsDir
delete_directory $outputDir
delete_directory $reportsDir
create_directory $artifactsDir
create_directory $outputDir
create_directory $reportsDir
$cleanMessage
}
task Compile -depends Setup, Clean {
build_solution "$sourceDir\$product.sln"
$compileMessage
}
task Test -depends Compile {
run_tests $outputDir "*.Specification.dll" $dotCover
$testMessage
}
task Pack -depends Test {
create_directory "$artifactsDir\$artifactsName"
$fluentDir = (join-path -path (resolve-path "$artifactsDir\$artifactsName") -childpath "\FluentSecurity")
create_directory $fluentDir
copy-item "$outputDir\FluentSecurity.dll" $fluentDir
copy-item "$outputDir\FluentSecurity.TestHelper.dll" $fluentDir
copy-item "$rootDir\License.txt" $fluentDir
copy-item "$rootDir\Readme.md" $fluentDir
get-content "$buildDir\NuGet\FluentSecurity.nuspec" | % { $_ -replace "@CURRENT-VERSION@", $buildLabel -replace "@ARTIFACT-PATH@", $fluentDir } | set-content "$buildDir\FluentSecurity.nuspec"
get-content "$buildDir\NuGet\FluentSecurity.TestHelper.nuspec" | % { $_ -replace "@CURRENT-VERSION@", $buildLabel -replace "@ARTIFACT-PATH@", $fluentDir } | set-content "$buildDir\FluentSecurity.TestHelper.nuspec"
exec {
nuget_exe pack "$buildDir\FluentSecurity.nuspec" -OutputDirectory "$artifactsDir\$artifactsName"
nuget_exe pack "$buildDir\FluentSecurity.TestHelper.nuspec" -OutputDirectory "$artifactsDir\$artifactsName"
}
$packMessage
}
task Deploy -depends Pack {
$localFeedDir = "C:\Develop\LocalNugetFeed"
if (test-path $localFeedDir) {
copy_files "$artifactsDir\$artifactsName" $localFeedDir "*.nupkg"
}
$isNightlyBuild = (((get-date) -gt "02:55") -and ((get-date) -lt "03:55"))
if ($branch -eq 'develop' -and $mygetApiKey -ne $null -and $isNightlyBuild) {
$feed = 'https://www.myget.org/F/fluentsecurity/api/v2/package'
$apiKey = $mygetApiKey
}
if ($branch -eq 'master' -and $nugetApiKey -ne $null) {
$feed = 'https://nuget.org/'
$apiKey = $nugetApiKey
}
if ($feed -ne $null -and $apiKey -ne $null) {
get-childitem -path "$artifactsDir\$artifactsName" -filter "*.nupkg" | % {
write-host "Pushing nuget package $_ to $feed" -fore cyan
nuget_exe push $_.FullName -apikey $apiKey -source $feed
}
}
$deployMessage
}
task ? -Description "Help" {
Write-Documentation
}
#------------------------------------------------------------
# Reusable functions
#------------------------------------------------------------
function generate_assemblyinfo {
param(
[string]$clsCompliant = "true",
[string]$description,
[string]$company,
[string]$product,
[string]$copyright,
[string]$version,
[string]$buildVersion,
[string]$buildLabel,
[string]$file = $(throw "file is a required parameter.")
)
$asmInfo = "using System;
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: CLSCompliantAttribute($clsCompliant)]
[assembly: ComVisibleAttribute(false)]
[assembly: AssemblyDescriptionAttribute(""$description"")]
[assembly: AssemblyCompanyAttribute(""$company"")]
[assembly: AssemblyProductAttribute(""$product"")]
[assembly: AssemblyCopyrightAttribute(""$copyright"")]
[assembly: AssemblyVersionAttribute(""$version"")]
[assembly: AssemblyInformationalVersionAttribute(""$buildLabel"")]
[assembly: AssemblyFileVersionAttribute(""$buildVersion"")]"
$dir = [System.IO.Path]::GetDirectoryName($file)
if ([System.IO.Directory]::Exists($dir) -eq $false)
{
Write-Host "Creating directory $dir"
[System.IO.Directory]::CreateDirectory($dir)
}
Write-Host "Generating assembly info file: $file"
out-file -filePath $file -encoding UTF8 -inputObject $asmInfo
}
function run_tests($source, $include=@("*.dll"), $dotCover) {
$runner = "$sourceDir\packages\NUnit.Runners.2.6.2\tools\nunit-console-x86.exe"
if ($dotCover -eq $null) {
$dotCover = "C:\Program Files (x86)\JetBrains\dotCover\v2.2\Bin\dotcover.exe"
if (!(test-path $dotCover)) {
$dotCover = $null
} else {
write-host "Using dotCover from $dotCover."
}
} else {
write-host "Using dotCover from TeamCity."
}
$fullReportsDir = (resolve-path $reportsDir)
$testassemblies = get-childitem $source -recurse -include $include
$testassemblies | % {
$assembly = $_
$assemblyName = $_.BaseName
$projectName = $assemblyName -replace ".Specification", ""
$testReportName = "$projectName-Test-results.xml"
$coverageReportName = "$projectName-CodeCoverage-results.dcvr"
?: {$dotCover -ne $null} {"Running test in $assembly with code coverage."} {"Running test in $assembly."} | write-host
exec {
if ($dotCover -ne $null) {
& $dotCover cover `
/TargetExecutable=$runner `
/TargetArguments="$assembly /framework:net-4.5 /xml=$fullReportsDir\$testReportName" `
/TargetWorkingDir="$buildDir\Output" `
/Filters="+:$projectName;-:*.Specification;" `
/Output="$reportsDir\$coverageReportName"
} else {
& $runner $assembly /nologo /framework:net-4.5 /xml="$fullReportsDir\$testReportName"
}
}
}
if ($dotCover -ne $null) {
write-host "Creating code coverage reports"
$reports = get-childitem -path $reportsDir -filter "*.dcvr" | % { $_.FullName }
$reports | % {
$coverageReport = $_
$htmlReport = $_ -replace ".dcvr", ".html"
write-host "##teamcity[importData type='dotNetCoverage' tool='dotcover' path='$coverageReport']"
exec {
& $dotCover report `
/Source=$coverageReport `
/Output=$htmlReport `
/ReportType=HTML
}
}
}
}
function read_xml($file) {
[xml]$xml = Get-Content $file
return $xml
}