forked from ericnewton76/gmaps-api-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-psake.ps1
398 lines (314 loc) · 16.4 KB
/
build-psake.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
properties {
$signAssembliesEnabled = $false # global override for signing
$signKeyPath = "D:\Development\Releases\newtonsoft.snk" #path for whomever can sign the assemblies
$buildDocumentation = $false
$treatWarningsAsErrors = $false
$baseDir = resolve-path "."
$buildDir = "$baseDir\Build"
$sourceDir = "$baseDir\src"
$toolsDir = "$baseDir\Tools"
$testsDir = "$baseDir\Build\Testing"
$docDir = "$baseDir\Doc"
$workingDir = "$baseDir\Build\Working"
$nugetBaseDir = "$baseDir\Build\NuGet"
$versionInfo = Load-VersionInfo -path "$sourceDir\AssemblyVersion_Master.cs"
$nuget_executible = "$sourceDir\.nuget\NuGet.exe"
$builds = @(
@{Project="GoogleMapsForNET.sln"; Tests="Google.Maps.Test.csproj"; Constants=""; FinalDir="Net"; NuGetDir = "net"; Framework="net-4.0"; Sign=$false}
# @{Project="TestingHelpers.csproj"; Tests="TestHelpers.Tests.csproj"; Constants=""; FinalDir="Net"; NuGetDir = ""; Framework="net-4.0"; Sign=$false}
#when some 4.0 specific things come about:
#@{Project = "System.IO.Abstractions.net40.csproj"; Tests = ""; Constants=""; FinalDir="Net40"; NuGetDir = "net40"; Framework="net-4.0"; Sign=$false}
#@{Project = "TestingHelpers.net40.csproj"; Tests = "TestHelpers.Tests.csproj"; Constants=""; FinalDir="Net40"; NuGetDir = "net40"; Framework="net-4.0"; Sign=$false}
#@{Project = "Newtonsoft.Json"; TestsName = "Newtonsoft.Json.Tests"; Constants=""; FinalDir="Net40"; NuGetDir = "net40"; Framework="net-4.0"; Sign=$true},
#@{Project = "Newtonsoft.Json.Portable"; TestsName = "Newtonsoft.Json.Tests.Portable"; Constants="PORTABLE"; FinalDir="Portable"; NuGetDir = "portable-net40+sl4+wp7+win8"; Framework="net-4.0"; Sign=$true},
#@{Project = "Newtonsoft.Json.WinRT"; TestsName = $null; Constants="NETFX_CORE"; FinalDir="WinRT"; NuGetDir = "winrt45"; Framework="net-4.5"; Sign=$true},
#@{Project = "Newtonsoft.Json.WindowsPhone"; TestsName = $null; Constants="SILVERLIGHT;WINDOWS_PHONE"; FinalDir="WindowsPhone"; NuGetDir = "sl3-wp,sl4-windowsphone71"; Framework="net-4.0"; Sign=$true},
#@{Project = "Newtonsoft.Json.Silverlight"; TestsName = "Newtonsoft.Json.Tests.Silverlight"; Constants="SILVERLIGHT"; FinalDir="Silverlight"; NuGetDir = "sl4"; Framework="net-4.0"; Sign=$true},
#@{Project = "Newtonsoft.Json.Net35"; TestsName = "Newtonsoft.Json.Tests.Net35"; Constants="NET35"; FinalDir="Net35"; NuGetDir = "net35"; Framework="net-2.0"; Sign=$true},
#@{Project = "Newtonsoft.Json.Net20"; TestsName = "Newtonsoft.Json.Tests.Net20"; Constants="NET20"; FinalDir="Net20"; NuGetDir = "net20"; Framework="net-2.0"; Sign=$true}
)
}
$framework = '4.0x86'
# Set up the default task, when calling build-psake with no -task parameter
#
task default -depends Test
#additional tasks that simply combine actual tasks can go here:
#task DebugBuild -depends SetConfigDebug, Build
#task DebugTest -depends SetConfigDebug, Build, Test
# Ensure a clean working directory
task Clean {
Set-Location $baseDir
Write-Verbose "-Working Directory: $workingDir"
if (Test-Path -path $workingDir)
{
del $workingDir -Recurse -Force
Write-Host -ForegroundColor Green "-Deleted Working Directory: $workingDir"
}
Write-Verbose "-Testing Directory: $workingDir"
if (Test-Path -path $testsDir)
{
del $testsDir -Recurse -Force
Write-Host -ForegroundColor Green "-Deleted Testing directory: $testsDir"
}
Write-Verbose "-NuGet Directory: $workingDir"
if (Test-Path -path $nugetBaseDir)
{
del $nugetBaseDir -Recurse -Force
Write-Host -ForegroundColor Green "-Deleted NuGet base directory: $nugetBaseDir"
}
}
# Build each solution, optionally signed
task Build -depends Clean,UpdateAssemblyInfoVersions {
Write-Verbose "-Ensure Working Directory exists: $workingDir"
New-Item -Path $workingDir -ItemType Directory | Out-Null
foreach ($build in $builds)
{
$nameInfo = new-object System.IO.FileInfo([string]$build.Project)
$projname = $nameInfo.Name.Replace($nameInfo.Extension,"") # chop off the extension
if ($nameInfo.Extension -eq ".sln") {
$name = $nameInfo.Name
} else {
$name = (Join-Path $projname $nameInfo.Name) # gives [name]\[name.csproj]
}
$finalDir = $build.FinalDir
$sign = ($build.Sign -and $signAssembliesEnabled)
$outputPath = "$workingDir\$projname\bin\Release\$finalDir"
Write-Host -ForegroundColor DarkCyan "-Building " $name
Write-Host -ForegroundColor DarkCyan "-Signed " $sign
exec { msbuild /v:m "/t:Clean;Rebuild" /p:Configuration=Release "/p:Platform=Any CPU" "/p:OutputPath=$outputPath\" /p:AssemblyOriginatorKeyFile=$signKeyPath "/p:SignAssembly=$sign" "/p:TreatWarningsAsErrors=$treatWarningsAsErrors" (GetConstants $build.Constants $sign) "$sourceDir\$name" } "Error building $name"
Write-Host -ForegroundColor Green "-MSBuild succeeded for" $build.Project #"->" $outputPath
}
}
task UpdateAssemblyInfoVersions {
$assemblyVer = new-object Version($versionInfo.FullString)
$fileVer = new-object Version($versionInfo.FullString)
Write-Host -ForegroundColor DarkCyan "-Updating assembly versions:"
Write-Host -ForegroundColor DarkCyan "-AssemblyVersion: $assemblyVer"
Write-Host -ForegroundColor DarkCyan "-AssemblyFileVersion: $fileVer"
Update-AssemblyInfoFiles $sourceDir $assemblyVer $fileVer
}
# Run tests on deployed files
#task Test -depends Deploy {
task Test -depends Build {
Write-Verbose "Ensuring Testing Directory exists: $testsDir"
if ((Test-Path -path $testsDir) -eq $false)
{
New-Item -Path $testsDir -ItemType Directory | Out-Null
}
foreach ($build in $builds)
{
if ([System.String]::IsNullOrEmpty($build.Tests) -ne $true )
{
$nameInfo = new-object System.IO.FileInfo([string]$build.Tests)
$projname = $nameInfo.Name.Replace($nameInfo.Extension,"") # chop off the extension
if ($nameInfo.Extension -eq ".sln") {
$name = $nameInfo.Name
} else { #if ($nameInfo.Extension -eq ".csproj") {
$name = (Join-Path $projname $nameInfo.Name) # gives [name]\[name.csproj]
}
$finalDir = $build.FinalDir
$frameworkDirs = $build.Framework.Split(",")
foreach ($frameworkDir in $frameworkDirs)
{
$outputPath = "$testsDir\$projname\bin\$frameworkDir"
Write-Host -ForegroundColor DarkCyan "-Building tests assembly $name for $frameworkDir"
Write-Host -ForegroundColor DarkCyan "-OutputPath=$testsDir\$projname\bin\$frameworkDir"
Write-Host
exec { & msbuild /v:m /p:Configuration=Release "/p:OutputPath=$outputPath\" "$sourceDir\$name" }
Write-Host -ForegroundColor Green "-MSBuild succeeded for" $build.Project #"->" $outputPath
Write-Host -ForegroundColor DarkCyan "-Running tests $projname for $frameworkDir"
& .\Tools\NUnit.Runners\tools\nunit-console.exe "$testsDir\$projname\bin\$frameworkDir\$projname.dll" /xml:$workingDir\$projname.xml
Write-Host "lastexitcode: $lastexitcode"
Write-Host -ForegroundColor Green "-Finished tests. TestResults: $workingDir\$projname.xml"
}
}
}
}
# Optional build documentation, add files to final zip
task Package -depends Test {
foreach ($build in $builds)
{
$name = $build.TestsName
$finalDir = $build.FinalDir
#robocopy "$sourceDir\Google.Maps\bin\Release\$finalDir" $workingDir\Package\Bin\$finalDir /NP /XO /XF *.pri | Out-Default
}
# if ($buildDocumentation)
# {
# $mainBuild = $builds | where { $_.Name -eq "Newtonsoft.Json" } | select -first 1
# $mainBuildFinalDir = $mainBuild.FinalDir
# $documentationSourcePath = "$workingDir\Package\Bin\$mainBuildFinalDir"
# Write-Host -ForegroundColor DarkCyan "Building documentation from $documentationSourcePath"
#
# # Sandcastle has issues when compiling with .NET 4 MSBuild - http://shfb.codeplex.com/Thread/View.aspx?ThreadId=50652
# exec { msbuild "/t:Clean;Rebuild" /p:Configuration=Release "/p:DocumentationSourcePath=$documentationSourcePath" $docDir\doc.shfbproj | Out-Default } "Error building documentation. Check that you have Sandcastle, Sandcastle Help File Builder and HTML Help Workshop installed."
#
# move -Path $workingDir\Documentation\LastBuild.log -Destination $workingDir\Documentation.log
# }
#
# Copy-Item -Path $docDir\readme.txt -Destination $workingDir\Package\
# Copy-Item -Path $docDir\versions.txt -Destination $workingDir\Package\Bin\
#
# robocopy $sourceDir $workingDir\Package\Source\Src /MIR /NP /XD .svn bin obj TestResults AppPackages /XF *.suo *.user | Out-Default
# robocopy $buildDir $workingDir\Package\Source\Build /MIR /NP /XD .svn | Out-Default
# robocopy $docDir $workingDir\Package\Source\Doc /MIR /NP /XD .svn | Out-Default
# robocopy $toolsDir $workingDir\Package\Source\Tools /MIR /NP /XD .svn | Out-Default
# exec { .\Tools\7-zip\7za.exe a -tzip $workingDir\$zipFileName $workingDir\Package\* | Out-Default } "Error zipping"
}
# Unzip package to a location
task Deploy -depends Package {
exec { .\Tools\7-zip\7za.exe x -y "-o$workingDir\Deployed" $workingDir\$zipFileName | Out-Default } "Error unzipping"
}
task PrepareNuspecFiles {
Write-Verbose "-Ensure Nuget directory exists: $nugetBaseDir"
New-Item -Path $nugetBaseDir -ItemType Directory -force | Out-Null
foreach ($build in $builds) {
if($build.NuGetDir -ne $null) {
$nameInfo = new-object System.IO.FileInfo([string]$build.Project)
$projname = $nameInfo.Name.Replace($nameInfo.Extension,"") # chop off the extension
if ($nameInfo.Extension -eq ".sln") {
$name = $nameInfo.Name
} else { #if ($nameInfo.Extension -eq ".csproj") {
$name = (Join-Path $projname $nameInfo.Name) # gives [name]\[name.csproj]
}
#-Path (Join-Path $sourceDir $projname)
Get-ChildItem -filter "*.nuspec" | Foreach-Object {
$nugetPackDir = Join-Path $nugetBaseDir $projname
Write-Verbose "-Ensure Directory: $nugetPackDir"
New-Item -Path $nugetPackDir -ItemType Directory -force | Out-Null
$nuspecFile = $_.Name
Write-Host -ForegroundColor DarkCyan "-Copy $nuspecFile into pack directory: $nugetPackDir"
Copy-Item -Path (Join-Path ($_.Directory.ToString()) ($_.Name.ToString())) -Destination $nugetPackDir
$filename = "$nugetPackDir\$nuspecFile"
#Write-Verbose ($versionInfo|Out-String)
#Write-Verbose $versionInfo.MajorMinorRevision
$versionStr = [String]::Concat($versionInfo.MajorMinorRevision,".0")
#$versionNodePattern1 = "<version>[0-9]+(\.([0-9]+|\*)){1,3}</version>"
$versionNodePattern1 = '<version>$version$</version>'
$versionNodeOut = "<version>" + $versionStr + "</version>"
Write-Verbose ("-PrepareNugetSpec: Replacing ["+$versionNodePattern1+"] with ["+$versionNodeOut+"]")
$versionAttrPattern1 = 'version="$version$"'
$versionAttrOut = 'version="' + $versionStr + '"'
Write-Verbose ("-PrepareNugetSpec: Replacing ["+$versionAttrPattern1+"] with ["+$versionAttrOut+"]")
(Get-Content $filename) | Foreach-Object {
% { $_.Replace($versionNodePattern1, $versionNodeOut) } |
% { $_.Replace($versionAttrPattern1, $versionAttrOut) }
} | Set-Content $filename
Write-Host -ForegroundColor Green "-Updated version(s) in $filename"
}
}
}
}
task NugetPackage -depends Test,PrepareNuspecFiles {
Write-Verbose "-Ensure Nuget directory exists: $nugetBaseDir"
New-Item -Path $nugetBaseDir -ItemType Directory -force | Out-Null
foreach ($build in $builds)
{
Write-Verbose ("build.NuGetDir="+$build.NuGetDir)
if ($build.NuGetDir -ne $null)
{
$nameInfo = new-object System.IO.FileInfo([string]$build.Project)
$projname = $nameInfo.Name.Replace($nameInfo.Extension,"") # chop off the extension
if ($nameInfo.Extension -eq ".sln") {
$name = $nameInfo.Name
} else { #if ($nameInfo.Extension -eq ".csproj") {
$name = (Join-Path $projname $nameInfo.Name) # gives [name]\[name.csproj]
}
$nugetPackDir = "$nugetBaseDir\$projname"
$finalDir = $build.FinalDir
$frameworkDirs = $build.NuGetDir.Split(",")
foreach ($frameworkDir in $frameworkDirs)
{
Write-Host -ForegroundColor DarkCyan "-Copy files into $nugetPackDir\lib\$frameworkDir"
robocopy "$workingDir\$projname\bin\Release\$finalDir" "$nugetPackDir\lib\$frameworkDir" /NP /XO /XF *.pri | Out-Null
}
Get-ChildItem -Path $nugetPackDir -Filter *.nuspec | Foreach-Object {
$nuspecFile = (Join-Path $nugetPackDir $_.Name)
Write-Host -ForegroundColor DarkCyan "-NuGet pack $_"
exec { & $nuget_executible pack $nuspecFile -Symbols -OutputDirectory $nugetPackDir } "Failed during NuGet Pack phase."
Write-Host -ForegroundColor Green "-NuGet pack succeeded. $nuspecFile"
}
Write-Host -ForegroundColor DarkCyan "-Copy nupkg files into Nuget base: $nugetBaseDir"
copy -Path $nugetPackDir\*.nupkg -Destination $nugetBaseDir
}
}
Write-Host -ForegroundColor Green "-NuGet Packages Built:"
Get-ChildItem -Path $nugetBaseDir -Filter *.nupkg | Foreach-Object {
Write-Host -ForegroundColor Green "-Package: " $_.Name
}
}
task NugetPackageDeploy -depends NugetPackage
{
Write-Output "NotImplementedException"
}
#########################################################
#########################################################
#########################################################
function GetConstants($constants, $includeSigned)
{
$signed = switch($includeSigned) { $true { ";SIGNED" } default { "" } }
return "/p:DefineConstants=`"CODE_ANALYSIS;TRACE;$constants$signed`""
}
# Gets an integer revision number based on the date
function Get-BuildTiming()
{
$now = [DateTime]::Now
[TimeSpan]$span = $now - (new-object DateTime($now.Year,1,1))
Return [int]($span.TotalMinutes/10)
}
function Load-VersionInfo([string] $path)
{
[hashtable]$ver = @{}
if( (Test-Path $path) -eq $true) {
Write-Verbose "Reading from file $path"
$matches = (Get-Content $path) | Select-String 'AssemblyFileVersion\("(?<V1>([0-9]+)\.?){1,4}"\)' -AllMatches
$caps = $matches.Matches[0].Groups[1].Captures
$ver.MajorMinor = [String]::Concat($caps[0].Value,".",$caps[1].Value)
$ver.MajorMinorRevision = [String]::Concat($ver.MajorMinor, ".", $caps[2].Value)
}
$ver.Build = Get-BuildTiming
$ver.FullString = [String]::Concat($ver.MajorMinorRevision,".",[string]$ver.Build)
Write-verbose ("Version (full string)=" + $ver.FullString)
Return $ver
}
# Update any (recursively found under $sourceDir) AssemblyInfo.cs files
function Update-AssemblyInfoFiles ([string] $sourceDir, [string] $assemblyVersionNumber, [string] $fileVersionNumber)
{
$assemblyVersionPattern = 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$fileVersionPattern = 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$assemblyVersion = 'AssemblyVersion("' + $assemblyVersionNumber + '")';
$fileVersion = 'AssemblyFileVersion("' + $fileVersionNumber + '")';
Get-ChildItem -Path $sourceDir -r -filter AssemblyInfo.cs | ForEach-Object {
$filename = $_.Directory.ToString() + '\' + $_.Name
(Get-Content $filename) | ForEach-Object {
% {$_ -replace $assemblyVersionPattern, $assemblyVersion } |
% {$_ -replace $fileVersionPattern, $fileVersion }
} | Set-Content $filename
Write-Host -ForegroundColor Green "-Updated versions in:" $filename
}
}
#[CmdletBinding()]
function Set-AssemblyInfoBuildNumbers(
[Parameter(Position=1,Mandatory=$true)] [Version] $version,
[Parameter(Position=2,Mandatory=$false)] [string] $fileSpec = ".\*AssemblyInfo.cs",
[Parameter(Position=3,Mandatory=$false)] [Version] $fileVersion
)
{
$assemblyPattern = "[0-9]+(\.([0-9]+|\*)){1,3}"
$assemblyVersionPattern = 'Assembly(File)?Version\("([0-9]+(\.([0-9]+|\*)){1,3})"\)'
$foundFiles = get-childitem $fileSpec -recurse
#$rawVersionNumberGroup = get-content $foundFiles | select-string -pattern $assemblyVersionPattern | select -first 1 | % { $_.Matches }
#$rawVersionNumber = $rawVersionNumberGroup.Groups[1].Value
#$versionParts = $rawVersionNumber.Split('.')
#$versionParts[3] = ([int]$versionParts[3]) + 1
#$updatedAssemblyVersion = "{0}.{1}.{2}.{3}" -f $versionParts[0], $versionParts[1], $versionParts[2], $versionParts[3]
#$updatedAssemblyVersion = "{0}.{1}.{2}.{3}" -f $version.Major, $version.Minor, $version.Revision, $version.Build
$updatedAssemblyVersion = $version.ToString()
#$assemblyVersion
$regex = new RegEx($assemblyVersionPattern,[RegexOptions]::SingleLine+[RegexOptions]::IgnoreCase)
foreach( $file in $foundFiles )
{
(Get-Content $file) | ForEach-Object {
% {$_ -replace $assemblyPattern, $updatedAssemblyVersion } |
} | Set-Content $file
}
}