-
Notifications
You must be signed in to change notification settings - Fork 28
/
BuildAndTest.ps1
178 lines (148 loc) · 5.52 KB
/
BuildAndTest.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
<#
.SYNOPSIS
Build, test, and package JSchema.
.DESCRIPTION
Builds JSchema for multiple target frameworks, runs the tests, and creates
NuGet packages.
.PARAMETER Configuration
The build configuration. Default=Release
.PARAMETER RunJsonSchemaTestSuite
Run the official JSON Schema test suite.
.PARAMETER NoBuild
Do not build.
.PARAMETER NoTest
Do not run tests.
.PARAMETER NoPackage
Do not create NuGet packages.
#>
[CmdletBinding()]
param(
[string]
[ValidateSet("Debug", "Release")]
$Configuration="Release",
[switch]
$RunJsonSchemaTestSuite,
[switch]
$NoBuild,
[switch]
$NoTest,
[switch]
$NoPackage
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$InformationPreference = "Continue"
$SourceRoot = "$PSScriptRoot\src"
$SolutionFile = "$SourceRoot\Everything.sln"
$Platform = "AnyCPU"
$BuildDirectory = "$PSScriptRoot\bld"
$PackageOutputDirectory = "$BuildDirectory\bin\NuGet\$Configuration"
function Exit-WithFailureMessage($message) {
Write-Information $message
Write-Information "SCRIPT FAILED."
exit 1
}
function Get-PackageLicenseExpression() {
$buildPropsPath = "$PSScriptRoot\src\build.props"
$namespace = @{ msbuild = "http://schemas.microsoft.com/developer/msbuild/2003" }
$xPath = "/msbuild:Project/msbuild:PropertyGroup[@Label='Package']/msbuild:PackageLicenseExpression"
$xml = Select-Xml -Path $buildPropsPath -Namespace $namespace -XPath $xPath
$packageLicenseExpression = $xml.Node.InnerText
$packageLicenseExpression
}
function Invoke-Build {
if (Test-Path $buildDirectory) {
Remove-Item -Force -Recurse $buildDirectory
}
dotnet build --no-incremental --configuration $Configuration /fileloggerparameters:Verbosity=detailed $SolutionFile
if ($LASTEXITCODE -ne 0) {
Exit-WithFailureMessage "Build failed."
}
}
function Invoke-Tests($project) {
dotnet test --no-build --no-restore --configuration $Configuration src\$project.UnitTests\$project.UnitTests.csproj
if ($LASTEXITCODE -ne 0) {
Exit-WithFailureMessage "$project unit tests failed."
}
}
# Publish-Application
#
# This function invokes "dotnet publish" to create a "publish" directory
# containing all files necessary to execute an application. In particular,
# dotnet publish adds the binaries from any dependent NuGet packages to the
# publish directory.
#
# This operation is necessary only when building an application for .NET Core.
# When building for .NET Framework, the build output directory already contains
# the necessary dependencies.
function Publish-Application($project, $framework) {
Write-Information "Publishing $project for $framework ..."
dotnet publish $SourceRoot\$project\$project.csproj --no-restore --configuration $Configuration --framework $framework
}
function New-NuGetPackageFromProjectFile($project, $version) {
$projectFile = "$PSScriptRoot\src\$project\$project.csproj"
$arguments =
"pack", $projectFile,
"--configuration", $Configuration,
"--no-build", "--no-restore",
"--include-source", "--include-symbols",
"-p:Platform=$Platform",
"--output", $PackageOutputDirectory
Write-Debug "dotnet $($arguments -join ' ')"
dotnet $arguments
if ($LASTEXITCODE -ne 0) {
Exit-WithFailureMessage "$project NuGet package creation failed."
}
}
function New-NuGetPackageFromNuspecFile($project, $version, $packageLicenseExpression, $suffix = "") {
$nuspecFile = "$PSScriptRoot\src\$project\$project.nuspec"
$arguments=
"pack", $nuspecFile,
"-Properties", "platform=$Platform;configuration=$Configuration;version=$version;packageLicenseExpression=$packageLicenseExpression",
"-Verbosity", "Quiet",
"-BasePath", ".\",
"-OutputDirectory", $PackageOutputDirectory
if ($suffix -ne "") {
$arguments += "-Suffix", $Suffix
}
$nugetExePath = "$PSScriptRoot\.nuget\NuGet.exe"
Write-Debug "$nuGetExePath $($arguments -join ' ')"
&$nuGetExePath $arguments
if ($LASTEXITCODE -ne 0) {
Exit-WithFailureMessage "$project NuGet package creation failed."
} else {
Write-Information "Successfully created package from $nuspecFile"
}
}
if (-not $NoBuild) {
Invoke-Build
}
if ($RunJsonSchemaTestSuite) {
# To understand the reason for this, see the comment in
# src\Json.Schema.Validation.UnitTests\ValidationSuite.cs.
$jsonSchemaTestSuitePath = "$PSScriptRoot\..\JSON-Schema-Test-Suite"
if (-not (Test-Path $jsonSchemaTestSuitePath)) {
$jsonSchemaTestSuiteUri = "https://github.com/json-schema-org/JSON-Schema-Test-Suite"
Write-Information "Cloning the offical JSON schema test suite..."
git clone $jsonSchemaTestSuiteUri $jsonSchemaTestSuitePath
}
}
if (-not $NoTest) {
$testedProjects = "Json.Pointer", "Json.Schema", "Json.Schema.ToDotNet", "Json.Schema.Validation"
foreach ($project in $testedProjects) {
Invoke-Tests $project
}
}
if (-not $NoPackage) {
$versionPrefix, $versionSuffix = .\src\build\Get-VersionConstants.ps1
$version = "$versionPrefix$versionSuffix"
$packagingProjects = "Json.Pointer", "Json.Schema"
foreach ($project in $packagingProjects) {
New-NuGetPackageFromProjectFile $project $version
}
$nuspecProjects = "Json.Schema.ToDotNet.Cli", "Json.Schema.Validation.Cli"
foreach ($project in $nuspecProjects) {
Publish-Application $project netcoreapp3.1
New-NuGetPackageFromNuSpecFile $project $version $(Get-PackageLicenseExpression)
}
}