-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-definition.ps1
133 lines (94 loc) · 4.29 KB
/
build-definition.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
Properties {
# This number will be used to replace the * in all versions of all libraries.
# This should be overwritten by a CI system like VSTS, AppVeyor, TeamCity, ...
$tag = @{ $true = $env:PRE_RELEASE_TAG; $false = 1 }[$env:PRE_RELEASE_TAG -ne $NULL];
$BuildNumber = $tag + @{ $true = $env:APPVEYOR_BUILD_NUMBER; $false = 1 }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
# The build configuration used for compilation
$BuildConfiguration = "Release"
# The folder in which all output artifacts should be placed
$ArtifactsPath = "artifacts"
# Artifacts-subfolder in which test results should be placed
$ArtifactsPathTests = "tests"
# Artifacts-subfolder in which NuGet packages should be placed
$ArtifactsPathNuGet = "nuget"
# A list of projects for which NuGet packages should be created
$NugetLibraries = @(
#"src/RestCode"
)
}
FormatTaskName ("`n" + ("-"*25) + "[{0}]" + ("-"*25) + "`n")
Task Default -depends init, clean, dotnet-install, dotnet-restore, dotnet-build, dotnet-test, dotnet-pack
Task init {
Write-Host "BuildNumber: $BuildNumber"
Write-Host "BuildConfiguration: $BuildConfiguration"
Write-Host "ArtifactsPath: $ArtifactsPath"
Write-Host "ArtifactsPathTests: $ArtifactsPathTests"
Write-Host "ArtifactsPathNuGet: $ArtifactsPathNuGet"
Assert ($BuildNumber -ne $null) "Property 'BuildNumber' may not be null."
Assert ($BuildConfiguration -ne $null) "Property 'BuildConfiguration' may not be null."
Assert ($ArtifactsPath -ne $null) "Property 'ArtifactsPath' may not be null."
Assert ($ArtifactsPathTests -ne $null) "Property 'ArtifactsPathTests' may not be null."
Assert ($ArtifactsPathNuGet -ne $null) "Property 'ArtifactsPathNuGet' may not be null."
}
Task clean {
if (Test-Path $ArtifactsPath) { Remove-Item -Path $ArtifactsPath -Recurse -Force -ErrorAction Ignore }
New-Item $ArtifactsPath -ItemType Directory -ErrorAction Ignore | Out-Null
Write-Host "Created artifacts folder '$ArtifactsPath'"
}
Task dotnet-install {
if (Get-Command "dotnet.exe" -ErrorAction SilentlyContinue) {
Write-Host "dotnet SDK already installed"
exec { dotnet --version }
} else {
Write-Host "Installing dotnet SDK"
$installScript = Join-Path $ArtifactsPath "dotnet-install.ps1"
Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0/scripts/obtain/install.ps1" `
-OutFile $installScript
& $installScript
}
}
Task dotnet-restore {
exec { dotnet restore -v Minimal }
}
Task dotnet-build {
exec { dotnet build **\project.json -c $BuildConfiguration --version-suffix $BuildNumber }
}
Task dotnet-test {
$testOutput = Join-Path $ArtifactsPath $ArtifactsPathTests
New-Item $testOutput -ItemType Directory -ErrorAction Ignore | Out-Null
# Find every library that has a configured testRunner and test it.
# Run all libraries, even if one fails to make sure we have test results for all libraries.
$testFailed = $false
Get-ChildItem -Filter project.json -Recurse | ForEach-Object {
$projectJson = Get-Content -Path $_.FullName -Raw -ErrorAction Ignore | ConvertFrom-Json -ErrorAction Ignore
if ($projectJson -and $projectJson.testRunner -ne $null)
{
$library = Split-Path $_.DirectoryName -Leaf
$testResultOutput = Join-Path $testOutput "$library.xml"
Write-Host ""
Write-Host "Testing $library"
Write-Host ""
dotnet test $_.Directory -c $BuildConfiguration --no-build -xml $testResultOutput
if ($LASTEXITCODE -ne 0) {
$testFailed = $true
}
}
}
if ($testFailed) {
throw "Tests for at least one library failed"
}
}
Task dotnet-pack {
if ($NugetLibraries -eq $null -or $NugetLibraries.Count -eq 0) {
Write-Host "No NugetLibraries configured"
return
}
$NugetLibraries | ForEach-Object {
$library = $_
$libraryOutput = Join-Path $ArtifactsPath $ArtifactsPathNuGet
Write-Host ""
Write-Host "Packaging $library to $libraryOutput"
Write-Host ""
exec { dotnet pack $library -c $BuildConfiguration --version-suffix $BuildNumber --no-build -o $libraryOutput }
}
}