-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsakefile.ps1
481 lines (395 loc) · 16.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# Copied from https://github.com/JamesNK/Jali/blob/master/Build/build.ps1
properties {
$zipFileName = "Jali00r1.zip"
$majorVersion = "0.0"
$majorWithReleaseVersion = "0.1.0"
# $nugetPrelease = "prealpha611"
$version = GetVersion $majorWithReleaseVersion
$signAssemblies = $false
# $signKeyPath = "C:\Development\Releases\newtonsoft.snk"
$buildDocumentation = $false
$buildNuGet = $true
$treatWarningsAsErrors = $false
$workingName = if ($workingName) {$workingName} else {"njali-working"}
# $dnvmVersion = "1.0.0-beta8-15618"
# $dnvmVersion = "1.0.0-beta7"
$dnvmVersion = "1.0.0-rc2-16258"
$baseDir = Resolve-Path $PSScriptRoot
$buildDir = "$baseDir\build"
$sourceDir = "$baseDir\src"
$toolsDir = "$baseDir\tools"
# $docDir = "$baseDir\doc"
$releaseDir = "$baseDir\dist"
$workingDir = "$baseDir\..\$workingName"
$workingSourceDir = "$workingDir\src"
$builds = @(
# @{Name = "Jali.Dnx"; ClrType = "Dnx"; SeparateProjectFolder = $false; TestsName = "Jali.Tests.Dnx"; BuildFunction = "DnxBuild"; TestsFunction = "DnxTests"; Constants="DNX"; FinalDir="Dnx"; NuGetDir = "dnx"; Framework=$null},
@{Name = "Jali.Pcl"; ClrType = "Pcl"; SeparateProjectFolder = $true; TestsName = "Jali.Tests.Pcl"; BuildFunction = "MSBuildBuild"; TestsFunction = "XUnitTests"; Constants="PCL"; FinalDir="Pcl"; NuGetDir = "portable-net40+sl5+wp80+win8+wpa81"; Framework="net-4.0"}
)
$packages = @(
@{Name = "Jali.Core"}
@{Name = "Jali"}
@{Name = "Jali.Serve"}
@{Name = "Jali.Serve.Server"}
@{Name = "Jali.Serve.AspNet.Mvc"; ClrType = "Net45"}
)
}
framework '4.6x86'
#task default -depends Test
task default -depends Package
# Ensure a clean working directory
task Clean {
Write-Host "Setting location to $baseDir"
Set-Location $baseDir
if (Test-Path -path $workingDir)
{
Write-Host "Deleting existing working directory $workingDir"
Execute-Command -command { del $workingDir -Recurse -Force }
}
Write-Host "Creating working directory $workingDir"
New-Item -Path $workingDir -ItemType Directory
}
# Build each solution, optionally signed
task Build -depends Clean {
Write-Host "Copying source to working source directory $workingSourceDir"
# robocopy $sourceDir $workingSourceDir /MIR /NP /XD bin obj TestResults AppPackages $packageDirs /XF *.suo *.user *.project.lock.json | Out-Default
robocopy $baseDir $workingDir /MIR /NP /XD bin obj TestResults AppPackages $packageDirs /XF *.suo *.user *.project.lock.json | Out-Default
Write-Host -ForegroundColor Green "Updating assembly version"
Write-Host
Update-AssemblyInfoFiles $workingSourceDir ($majorVersion + '.0.0') $version
Update-Project $workingSourceDir\Jali\project.json $signAssemblies
foreach ($build in $builds)
{
$name = $build.Name
if ($name -ne $null)
{
Write-Host -ForegroundColor Green "Building " $name
Write-Host -ForegroundColor Green "Signed " $signAssemblies
Write-Host -ForegroundColor Green "Key " $signKeyPath
& $build.BuildFunction $build
}
}
}
# Optional build documentation, add files to final zip
task Package -depends Build {
foreach ($build in $builds)
{
$finalDir = $build.FinalDir
foreach ($package in $packages)
{
if ($build.BuildFunction -eq 'DnxBuild')
{
continue
}
if ($package.ClrType -ne $null)
{
$projectFolder = $package.Name + "." + $package.ClrType
}
elseif ($build.SeparateProjectFolder)
{
$projectFolder = $package.Name + "." + $build.ClrType
}
else
{
$projectFolder = $package.Name
}
$artifactPath = "$workingDir\$($build.Name)\src\$projectFolder\bin\Release\$finalDir"
if ($package.ClrType -ne $null)
{
$artifactRoot = $package.Name + "." + $package.ClrType
}
else
{
$artifactRoot = $package.Name + "." + $finalDir
}
robocopy $artifactPath $workingDir\Package\Bin\$finalDir "$artifactRoot.dll" "$artifactRoot.pdb" "$artifactRoot.xml" /NFL /NDL /NJS /NC /NS /NP /XO /XF *.CodeAnalysisLog.xml | Out-Default
}
}
if ($buildNuGet)
{
$nugetVersion = $majorWithReleaseVersion
if ($nugetPrelease -ne $null)
{
$nugetVersion = $nugetVersion + "-" + $nugetPrelease
}
foreach ($package in $packages)
{
$packageId = $package.Name
$packageDropPath = "$workingDir\NuGet\$packageId"
$nuspecName = "$packageId.nuspec"
$nuspecPath = "$packageDropPath\$nuspecName"
$buildNuspecPath = "$buildDir\$packageId\$nuspecName"
New-Item -Path $packageDropPath -ItemType Directory
Write-Host -ForegroundColor DarkRed "PackageID: '$packageId'"
Write-Host -ForegroundColor DarkRed "Nuspec Name: '$nuspecName'"
Write-Host -ForegroundColor DarkRed "Build Dir: '$buildDir'"
Write-Host -ForegroundColor DarkRed "BuildNuspecPath: '$buildNuspecPath'"
Copy-Item -Path "$buildNuspecPath" -Destination $nuspecPath -recurse
Write-Host "Updating nuspec file at $nuspecPath" -ForegroundColor Green
Write-Host
$xml = [xml](Get-Content $nuspecPath)
Edit-XmlNodes -doc $xml -xpath "//*[local-name() = 'id']" -value $packageId
Edit-XmlNodes -doc $xml -xpath "//*[local-name() = 'version']" -value $nugetVersion
Write-Host $xml.OuterXml
$xml.save($nuspecPath)
#New-Item -Path $packageDropPath\tools -ItemType Directory
#Copy-Item -Path "$buildDir\$packageId\install.ps1" -Destination $packageDropPath\install.ps1 -recurse
foreach ($build in $builds)
{
if ($build.NuGetDir)
{
$finalDir = $build.FinalDir
$frameworkDirs = $build.NuGetDir.Split(",")
$projectLinkFolder = $package.Name
if ($package.ClrType -ne $null)
{
$projectFolder = $package.Name + "." + $package.ClrType
}
elseif ($build.SeparateProjectFolder)
{
$projectFolder = $package.Name + "." + $build.ClrType
}
else
{
$projectFolder = $package.Name
}
$projectLinkPath = "$workingDir\src\$projectLinkFolder"
$projectPath = "$workingDir\$($build.Name)\src\$projectFolder"
$artifactPath = "$projectPath\bin\Release\$finalDir"
if ($package.ClrType -ne $null)
{
$artifactRoot = $package.Name + "." + $package.ClrType
}
else
{
$artifactRoot = $package.Name + "." + $finalDir
}
if ($build.BuildFunction -ne 'DnxBuild')
{
foreach ($frameworkDir in $frameworkDirs)
{
robocopy $artifactPath $packageDropPath\lib\$frameworkDir "$artifactRoot.dll" "$artifactRoot.pdb" "$artifactRoot.xml" /NFL /NDL /NJS /NC /NS /NP /XO /XF *.CodeAnalysisLog.xml | Out-Default
}
}
}
robocopy $projectLinkPath $packageDropPath\src *.cs /S /NFL /NDL /NJS /NC /NS /NP /XD Jali.Tests Jali.TestConsole obj .vs artifacts | Out-Default
robocopy $projectPath $packageDropPath\src *.cs /S /NFL /NDL /NJS /NC /NS /NP /XD Jali.Tests Jali.TestConsole obj .vs artifacts | Out-Default
}
Write-Host "Building NuGet package with ID $packageId and version $nugetVersion" -ForegroundColor Green
Write-Host
# TODO: https://oren.codes/2015/09/23/enabling-source-code-debugging-for-your-nuget-packages-with-gitlink/
exec { & $workingDir\tools\nuget\3.2.0\nuget.exe pack $nuspecPath -Symbols -Properties "version=$nugetVersion" }
move -Path .\*.nupkg -Destination $packageDropPath
}
}
Write-Host "Build documentation: $buildDocumentation"
#if ($buildDocumentation)
#{
# $mainBuild = $builds | where { $_.Name -eq "Jali" } | select -first 1
# $mainBuildFinalDir = $mainBuild.FinalDir
# $documentationSourcePath = "$workingDir\Package\Bin\$mainBuildFinalDir"
# $docOutputPath = "$workingDir\Documentation\"
# Write-Host -ForegroundColor Green "Building documentation from $documentationSourcePath"
# Write-Host "Documentation output to $docOutputPath"
# # 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" "/p:OutputPath=$docOutputPath" $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\license.txt -Destination $workingDir\Package\
## exclude package directories but keep packages\repositories.config
#$packageDirs = gci $workingSourceDir\packages | where {$_.PsIsContainer} | Select -ExpandProperty Name
#robocopy $workingSourceDir $workingDir\Package\Source\Src /MIR /NFL /NDL /NJS /NC /NS /NP /XD bin obj TestResults AppPackages $packageDirs /XF *.suo *.user | Out-Default
#robocopy $buildDir $workingDir\Package\Source\Build /MIR /NFL /NDL /NJS /NC /NS /NP /XF runbuild.txt | Out-Default
#robocopy $docDir $workingDir\Package\Source\Doc /MIR /NFL /NDL /NJS /NC /NS /NP | Out-Default
#robocopy $toolsDir $workingDir\Package\Source\Tools /MIR /NFL /NDL /NJS /NC /NS /NP | 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"
#}
# Run tests on deployed files
#task Test -depends Deploy {
# Update-Project $workingSourceDir\Jali\project.json $false
# foreach ($build in $builds)
# {
# if ($build.TestsFunction -ne $null)
# {
# & $build.TestsFunction $build
# }
# }
#}
function MSBuildBuild($build)
{
$name = $build.Name
$targetPath = "$workingDir\$name\$name.sln"
$finalDir = $build.FinalDir
Write-Host
Write-Host "Restoring $workingDir\$name.sln" -ForegroundColor Green
[Environment]::SetEnvironmentVariable("EnableNuGetPackageRestore", "true", "Process")
exec { .\tools\nuget\3.2.0\NuGet.exe update -self }
# exec { .\tools\nuget\3.2.0\NuGet.exe restore "$workingSourceDir\$name.sln" -verbosity detailed -configfile $workingSourceDir\..\nuget.config | Out-Default } "Error restoring $name"
exec { .\tools\nuget\3.2.0\NuGet.exe restore $targetPath -verbosity detailed -configfile $workingDir\nuget.config | Out-Default } "Error restoring $name"
$constants = GetConstants $build.Constants $signAssemblies
Write-Host
Write-Host "Building $targetPath" -ForegroundColor Green
exec { msbuild "/t:Clean;Rebuild" /p:Configuration=Release "/p:CopyNuGetImplementations=true" "/p:Platform=Any CPU" "/p:PlatformTarget=AnyCPU" /p:OutputPath=bin\Release\$finalDir\ /p:AssemblyOriginatorKeyFile=$signKeyPath "/p:SignAssembly=$signAssemblies" "/p:TreatWarningsAsErrors=$treatWarningsAsErrors" "/p:VisualStudioVersion=14.0" /p:DefineConstants=`"$constants`" "$targetPath" | Out-Default } "Error building $name"
}
function DnxBuild($build)
{
$name = $build.Name
$projectPath = "$workingSourceDir\Jali\project.json"
# exec { dnvm install $dnvmVersion -r clr -u | Out-Default }
exec { dnvm use $dnvmVersion -r clr | Out-Default }
Write-Host -ForegroundColor Green "Restoring packages for $name"
Write-Host
exec {
try {
dnu restore $projectPath | Out-Default
Write-Host "Restore last exit code: $lastexitcode"
}
catch [System.Management.Automation.RemoteException]
{
Write-Host "Restore last exit code: $lastexitcode"
Write-Host ("Restore exception: " + $_.ToString())
$global:lastexitcode = 0
$lastexitcode = 0
}
}
Write-Host -ForegroundColor Green "Building $projectPath"
exec { dnu build $projectPath --configuration Release | Out-Default }
}
function DnxTests($build)
{
$name = $build.TestsName
exec { dnvm install $dnvmVersion -r coreclr -u | Out-Default }
exec { dnvm use $dnvmVersion -r coreclr | Out-Default }
Write-Host -ForegroundColor Green "Restoring packages for $name"
Write-Host
exec {
try {
dnu restore "$workingSourceDir\Jali.Tests\project.json" | Out-Default
Write-Host "Restore last exit code: $lastexitcode"
}
catch [System.Management.Automation.RemoteException]
{
Write-Host "Restore last exit code: $lastexitcode"
Write-Host ("Restore exception: " + $_.ToString())
$global:lastexitcode = 0
$lastexitcode = 0
}
}
Write-Host -ForegroundColor Green "Ensuring test project builds for $name"
Write-Host
try
{
Set-Location "$workingSourceDir\Jali.Tests"
exec { dnx --configuration Release test | Out-Default }
}
finally
{
Set-Location $baseDir
}
}
#function NUnitTests($build)
#{
# $name = $build.TestsName
# $finalDir = $build.FinalDir
# $framework = $build.Framework
# Write-Host -ForegroundColor Green "Copying test assembly $name to deployed directory"
# Write-Host
# robocopy "$workingSourceDir\Jali.Tests\bin\Release\$finalDir" $workingDir\Deployed\Bin\$finalDir /MIR /NFL /NDL /NJS /NC /NS /NP /XO | Out-Default
# Copy-Item -Path "$workingSourceDir\Jali.Tests\bin\Release\$finalDir\Jali.Tests.dll" -Destination $workingDir\Deployed\Bin\$finalDir\
# Write-Host -ForegroundColor Green "Running NUnit tests " $name
# Write-Host
# exec { .\Tools\NUnit\nunit-console.exe "$workingDir\Deployed\Bin\$finalDir\Jali.Tests.dll" /framework=$framework /xml:$workingDir\$name.xml | Out-Default } "Error running $name tests"
#}
function GetConstants($constants, $includeSigned)
{
$signed = switch($includeSigned) { $true { ";SIGNED" } default { "" } }
return "CODE_ANALYSIS;TRACE;$constants$signed"
}
function GetVersion($majorVersion)
{
$now = [DateTime]::UtcNow
$year = $now.Year - 2000
$month = $now.Month
$totalMonthsSince2000 = ($year * 12) + $month
$day = $now.Day
$minor = "{0}{1:00}" -f $totalMonthsSince2000, $day
$hour = $now.Hour
$minute = $now.Minute
$revision = "{0:00}{1:00}" -f $hour, $minute
return $majorVersion + "." + $minor
}
function Update-AssemblyInfoFiles ([string] $workingSourceDir, [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 $workingSourceDir -r -filter AssemblyInfo.cs | ForEach-Object {
$filename = $_.Directory.ToString() + '\' + $_.Name
Write-Host $filename
$filename + ' -> ' + $version
(Get-Content $filename) | ForEach-Object {
% {$_ -replace $assemblyVersionPattern, $assemblyVersion } |
% {$_ -replace $fileVersionPattern, $fileVersion }
} | Set-Content $filename
}
}
function Edit-XmlNodes {
param (
[xml] $doc,
[string] $xpath = $(throw "xpath is a required parameter"),
[string] $value = $(throw "value is a required parameter")
)
$nodes = $doc.SelectNodes($xpath)
$count = $nodes.Count
Write-Host "Found $count nodes with path '$xpath'"
foreach ($node in $nodes) {
if ($node -ne $null) {
if ($node.NodeType -eq "Element")
{
$node.InnerXml = $value
}
else
{
$node.Value = $value
}
}
}
}
function Update-Project {
param (
[string] $projectPath,
[string] $sign
)
$file = switch($sign) { $true { $signKeyPath } default { $null } }
$json = (Get-Content $projectPath) -join "`n" | ConvertFrom-Json
$options = @{"warningsAsErrors" = $true; "keyFile" = $file; "define" = ((GetConstants "dotnet" $sign) -split ";") }
Add-Member -InputObject $json -MemberType NoteProperty -Name "compilationOptions" -Value $options -Force
ConvertTo-Json $json -Depth 10 | Set-Content $projectPath
}
function Execute-Command($command) {
$currentRetry = 0
$success = $false
do {
try
{
& $command
$success = $true
}
catch [System.Exception]
{
if ($currentRetry -gt 5) {
throw $_.Exception.ToString()
} else {
write-host "Retry $currentRetry"
Start-Sleep -s 1
}
$currentRetry = $currentRetry + 1
}
} while (!$success)
}