-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
build.ps1
executable file
·132 lines (103 loc) · 3.94 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
#! /usr/bin/env pwsh
#Requires -PSEdition Core
#Requires -Version 7
param(
[Parameter(Mandatory = $false)][string] $OutputPath = "",
[Parameter(Mandatory = $false)][switch] $SkipTests
)
# These make CI builds faster
$env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE = "true"
$env:NUGET_XMLDOC_MODE = "skip"
if ($null -eq $env:MSBUILDTERMINALLOGGER) {
$env:MSBUILDTERMINALLOGGER = "auto"
}
$Configuration = "Release"
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
$solutionPath = $PSScriptRoot
$sdkFile = Join-Path $solutionPath "global.json"
$dotnetVersion = (Get-Content $sdkFile | Out-String | ConvertFrom-Json).sdk.version
if ($OutputPath -eq "") {
$OutputPath = Join-Path $PSScriptRoot "artifacts"
}
$installDotNetSdk = $false;
if (($null -eq (Get-Command "dotnet" -ErrorAction SilentlyContinue)) -and ($null -eq (Get-Command "dotnet.exe" -ErrorAction SilentlyContinue))) {
Write-Host "The .NET SDK is not installed."
$installDotNetSdk = $true
}
else {
Try {
$installedDotNetVersion = (dotnet --version 2>&1 | Out-String).Trim()
}
Catch {
$installedDotNetVersion = "?"
}
if ($installedDotNetVersion -ne $dotnetVersion) {
Write-Host "The required version of the .NET SDK is not installed. Expected $dotnetVersion."
$installDotNetSdk = $true
}
}
if ($installDotNetSdk -eq $true) {
$env:DOTNET_INSTALL_DIR = Join-Path $PSScriptRoot ".dotnetcli"
$sdkPath = Join-Path $env:DOTNET_INSTALL_DIR "sdk" "$dotnetVersion"
if (!(Test-Path $sdkPath)) {
if (!(Test-Path $env:DOTNET_INSTALL_DIR)) {
mkdir $env:DOTNET_INSTALL_DIR | Out-Null
}
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor "Tls12"
if (($PSVersionTable.PSVersion.Major -ge 6) -And !$IsWindows) {
$installScript = Join-Path $env:DOTNET_INSTALL_DIR "install.sh"
Invoke-WebRequest "https://dot.net/v1/dotnet-install.sh" -OutFile $installScript -UseBasicParsing
chmod +x $installScript
& $installScript --version "$dotnetVersion" --install-dir "$env:DOTNET_INSTALL_DIR" --no-path --skip-non-versioned-files
}
else {
$installScript = Join-Path $env:DOTNET_INSTALL_DIR "install.ps1"
Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile $installScript -UseBasicParsing
& $installScript -Version "$dotnetVersion" -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath -SkipNonVersionedFiles
}
}
}
else {
$env:DOTNET_INSTALL_DIR = Split-Path -Path (Get-Command dotnet).Path
}
$dotnet = Join-Path "$env:DOTNET_INSTALL_DIR" "dotnet"
if ($installDotNetSdk -eq $true) {
$env:PATH = "$env:DOTNET_INSTALL_DIR;$env:PATH"
}
function DotNetTest {
param([string]$Project)
$additionalArgs = @()
if (![string]::IsNullOrEmpty($env:GITHUB_SHA)) {
$additionalArgs += "--logger"
$additionalArgs += "GitHubActions;report-warnings=false"
}
& $dotnet test $Project --output $OutputPath --configuration $Configuration $additionalArgs
if ($LASTEXITCODE -ne 0) {
throw "dotnet test failed with exit code $LASTEXITCODE"
}
}
function DotNetPublish {
param([string]$Project)
$publishPath = Join-Path $OutputPath "publish"
& $dotnet publish $Project --output $publishPath
if ($LASTEXITCODE -ne 0) {
throw "dotnet publish failed with exit code $LASTEXITCODE"
}
}
$testProjects = @(
(Join-Path $solutionPath "tests" "TodoApp.Tests" "TodoApp.Tests.csproj")
)
$publishProjects = @(
(Join-Path $solutionPath "src" "TodoApp" "TodoApp.csproj")
)
Write-Host "Publishing solution..." -ForegroundColor Green
ForEach ($project in $publishProjects) {
DotNetPublish $project $Configuration
}
if ($SkipTests -eq $false) {
Write-Host "Testing $($testProjects.Count) project(s)..." -ForegroundColor Green
ForEach ($project in $testProjects) {
DotNetTest $project
}
}