forked from Ed-Fi-Alliance-OSS/Ed-Fi-API-Publisher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
247 lines (200 loc) · 7 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
# SPDX-License-Identifier: Apache-2.0
# Licensed to the Ed-Fi Alliance under one or more agreements.
# The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
# See the LICENSE and NOTICES files in the project root for more information.
<#
.SYNOPSIS
Automation script for running build operations from the command line.
.DESCRIPTION
Provides automation of the following tasks:
* Build: runs `dotnet build` with several implicit steps
(clean, restore, inject version information).
* BuildAndPublish: build and creates the publish files.
* Package: create nuget package.
* Push: Push package to azure artifacts.
* UnitTest: runs the unit tests.
.EXAMPLE
.\build.ps1 build -Configuration Release -Version "2.0.0"
Overrides the default build configuration (Debug) to build in release
mode with assembly version 2.0.0.45.
.EXAMPLE
.\build.ps1 BuildAndPublish
Output: Build and publish files will be created.
.EXAMPLE
.\build.ps1 Package -Version "2.0.0"
Output: Create nuget package with EdFi.ApiPublisher, EdFi.ApiPublisher.Win64 folders
.EXAMPLE
.\build.ps1 UnitTest
Output: Test results will be logged to .trx files.
.EXAMPLE
.\build.ps1 Push -NuGetApiKey $env:nuget_key
Output: Application nuget package will be pushed to azure artifacts.
#>
[CmdLetBinding()]
param(
# Command to execute, defaults to "Build".
[string]
[ValidateSet("Build", "BuildAndPublish", "Package", "Push", "UnitTest")]
$Command = "Build",
# [switch] $SelfContained,
# Assembly and package version number, defaults 2.6.1
[string]
$Version = "0.1.0",
# .NET project build configuration, defaults to "Debug". Options are: Debug, Release.
[string]
[ValidateSet("Debug", "Release")]
$Configuration = "Debug",
# Ed-Fi's official NuGet package feed for package download and distribution.
[string]
$EdFiNuGetFeed = "https://pkgs.dev.azure.com/ed-fi-alliance/Ed-Fi-Alliance-OSS/_packaging/EdFi/nuget/v3/index.json",
# API key for accessing the feed above. Only required with with the Push
# command.
[string]
$NuGetApiKey,
# Full path of a package file to push to the NuGet feed. Optional, only
# applies with the Push command. If not set, then the script looks for a
# NuGet package corresponding to the provided $Version.
[string]
$PackageFile
)
$Env:MSBUILDDISABLENODEREUSE = "1"
$solutionRoot = "$PSScriptRoot/src"
$maintainers = "Ed-Fi Alliance, LLC and contributors"
Import-Module -Name ("$PSScriptRoot/eng/build-helpers.psm1") -Force
$cliProject = "EdFi.Tools.ApiPublisher.Cli"
$publishOutputPath = "$solutionRoot/$cliProject/publish"
$publishFddOutputDirectory = "$publishOutputPath/fdd"
$publishScdOutputDirectory = "$publishOutputPath/scd"
$testProjectName = "EdFi.Tools.ApiPublisher.Tests"
function DotNetClean {
Invoke-Execute { dotnet clean $solutionRoot -c $Configuration --nologo -v minimal }
}
function AssemblyInfo {
Invoke-Execute {
$assembly_version = $Version
$year = (Get-Date -Format yyyy)
Invoke-RegenerateFile ("$solutionRoot/Directory.Build.props") @"
<Project>
<!-- This file is generated by the build script. -->
<PropertyGroup>
<Product>Ed-Fi API-Publisher</Product>
<Authors>$maintainers</Authors>
<Company>$maintainers</Company>
<Copyright>Copyright © $year Ed-Fi Alliance</Copyright>
<VersionPrefix>$assembly_version</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
"@
}
}
function Compile {
Invoke-Execute {
dotnet --info
dotnet build $solutionRoot -c $Configuration --nologo
}
}
function Publish {
Invoke-Execute {
$project = "$solutionRoot/$cliProject"
Write-Host "Self contained." -ForegroundColor Cyan
dotnet publish $project -c $Configuration /p:EnvironmentName=Production -o "$publishScdOutputDirectory" --self-contained -r win-x64 --nologo
Write-Host "Not self contained." -ForegroundColor Cyan
dotnet publish $project -c $Configuration /p:EnvironmentName=Production -o "$publishFddOutputDirectory" --no-self-contained --no-build --nologo
}
}
function RunDotNetPack {
param (
[string]
$PackageVersion,
[string]
$ProjectName,
[string]
$NuspecFileName
)
dotnet pack "$ProjectName.csproj" --no-build --no-restore --output "$PSScriptRoot" --configuration $Configuration -p:NuspecFile="$NuspecFileName.nuspec" -p:NuspecProperties="version=$PackageVersion"
}
function Package {
Invoke-Execute {
$baseProjectFullName = "$solutionRoot/$cliProject/$cliProject"
RunDotNetPack -PackageVersion $Version -projectName $baseProjectFullName $baseProjectFullName
}
}
function Push {
param (
[string]
$PackageVersion = $Version
)
if (-not $NuGetApiKey) {
throw "Cannot push a NuGet package without providing an API key in the `NuGetApiKey` argument."
}
if (-not $PackageFile) {
$PackageFile = "$PSScriptRoot/EdFi.ApiPublisher.$PackageVersion.nupkg"
DotnetPush $PackageFile
}
else
{
DotnetPush $PackageFile
}
}
function DotnetPush {
param (
[string]
$PackageFileName
)
Write-Host "Pushing $PackageFileName to $EdFiNuGetFeed"
dotnet nuget push $PackageFileName --api-key $NuGetApiKey --source $EdFiNuGetFeed
}
function RunTests {
param (
# File search filter
[string]
$Filter
)
$testAssemblyPath = "$solutionRoot/$Filter/bin/$Configuration/"
$testAssemblies = Get-ChildItem -Path $testAssemblyPath -Filter "$Filter.dll" -Recurse
if ($testAssemblies.Length -eq 0) {
Write-Host "no test assemblies found in $testAssemblyPath"
}
$testAssemblies | ForEach-Object {
Write-Host "Executing: dotnet test $($_)"
Invoke-Execute {
dotnet test $_ `
--logger "trx;LogFileName=$($_).trx" `
--nologo `
--no-build
}
}
}
function UnitTests {
Invoke-Execute { RunTests -Filter $testProjectName }
}
function Invoke-Build {
Write-Host "Building Version $Version" -ForegroundColor Cyan
Invoke-Step { DotNetClean }
Invoke-Step { AssemblyInfo }
Invoke-Step { Compile }
}
function Invoke-BuildAndPublish {
Invoke-Build
Invoke-Step { Publish }
}
function Invoke-UnitTests {
Invoke-Step { UnitTests }
}
function Invoke-Package {
Invoke-Step { Package }
}
function Invoke-Push {
Invoke-Step { Push }
}
Invoke-Main {
switch ($Command) {
Build { Invoke-Build }
UnitTest { Invoke-UnitTests }
BuildAndPublish { Invoke-BuildAndPublish }
Package { Invoke-Package }
Push { Invoke-Push }
default { throw "Command '$Command' is not recognized" }
}
}