-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.ps1
143 lines (113 loc) · 3.95 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
# **************************************************************************
# Copyright (c) Cloud Native Foundation.
# SPDX-License-Identifier: Apache-2.0
# **************************************************************************
<#
.SYNOPSIS
Builds and tests the CloudEvents.Sdk module
.DESCRIPTION
The script is the entry point to build and test the CloudEvents.Sdk module.
.PARAMETER OutputDir
Target directory where the CloudEvents.Sdk will be created by the script. The default is the PS Script Root
.PARAMETER TestsType
Specifies the type of the test to be run post build. Possible values are 'none','unit', 'integration', 'all'.
The default is 'all'
.PARAMETER ExitProcess
Specifies whther to exit the running process. Exits with exit code equal to the number of failing tests.
#>
param(
[Parameter()]
[string]
$OutputDir,
[Parameter()]
[ValidateSet('none','unit', 'integration', 'all')]
[string]
$TestsType = 'all',
[Parameter()]
[switch]
$ExitProcess
)
$moduleName = 'CloudEvents.Sdk'
#region Input
if (-not $OutputDir) {
$OutputDir = $PSScriptRoot
}
# Create Output Dir if it doesn't exist
if (-not (Test-Path $OutputDir)) {
New-Item -ItemType Directory -Path $OutputDir -Force -Confirm:$false | Out-Null
# Get the full path
$OutputDir = (Resolve-Path $OutputDir).Path
}
$OutputDir = Join-Path $OutputDir $moduleName
if (-not (Test-Path $OutputDir)) {
New-Item -ItemType Directory -Path $OutputDir | Out-Null
}
#endregion
#region Helper Funcitons
function Write-InfoLog($message) {
$dt = (Get-Date).ToLongTimeString()
Write-Host "[$dt] INFO: $message" -ForegroundColor Green
}
function Write-ErrorLog($message) {
$dt = (Get-Date).ToLongTimeString()
Write-Host "[$dt] ERROR: $message" -ForegroundColor Red
}
function Test-BuildToolsAreAvailable {
$dotnetSdk = Get-Command 'dotnet'
if (-not $dotnetSdk) {
throw "'dotnet' sdk is not available"
}
}
function Start-Tests {
param(
[Parameter()]
[ValidateSet('unit', 'integration')]
[string]
$TestsType
)
$pesterModule = Get-Module Pester -List
if ($pesterModule -eq $null) {
Write-ErrorLog "Pester Module is not available"
} else {
# Run Tests in external process because it will load build output binaries
Write-InfoLog "Run $TestsType tests"
$usePowerShell = (Get-Process -Id $pid).ProcessName
$testLauncherScript = Join-Path (Join-Path $PSScriptRoot 'test') 'RunTests.ps1'
$CloudEventsModulePath = Join-Path $OutputDir "$moduleName.psd1"
$testProcessArguments = "-Command $testLauncherScript -CloudEventsModulePath '$CloudEventsModulePath' -TestsType '$TestsType' -EnableProcessExit"
# Process Exit Code is 0 if all tests pass, otherwise it equals the number of failed tests
$testProcess = Start-Process `
-FilePath $usePowerShell `
-ArgumentList $testProcessArguments `
-PassThru `
-NoNewWindow
$testProcess | Wait-Process | Out-Null
# Return the number of failed tests
$testProcess.ExitCode
}
}
#endregion
$dotnetProjectName = 'CloudEventsPowerShell'
$dotnetProjectPath = Join-Path (Join-Path (Join-Path $PSScriptRoot 'src') $dotnetProjectName) "$dotnetProjectName.csproj"
# 1. Test dotnet command is available
Test-BuildToolsAreAvailable
# 2. Publish CloudEvents Module
Write-InfoLog "Publish CloudEvents.Sdk Module to '$OutputDir'"
dotnet publish -c Release -o $OutputDir $dotnetProjectPath | Out-Null
# 3. Cleanup Unnecessary Outputs
Get-ChildItem "$dotnetProjectName*" -Path $OutputDir | Remove-Item -Confirm:$false
$failedTests = 0
# 4. Run Unit Tests
if ($TestsType -eq 'unit' -or $TestsType -eq 'all') {
$failedTests += (Start-Tests -TestsType 'unit')
}
# 5. Run Integration Tests
if ($TestsType -eq 'integration' -or $TestsType -eq 'all') {
$failedTests += (Start-Tests -TestsType 'integration')
}
# 6. Set exit code
if ($ExitProcess.IsPresent) {
exit $failedTests
} else {
$failedTests
}