-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSBinaryModule.build.ps1
More file actions
248 lines (210 loc) · 7.64 KB
/
PSBinaryModule.build.ps1
File metadata and controls
248 lines (210 loc) · 7.64 KB
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
248
#requires -modules InvokeBuild
<#
.SYNOPSIS
Build script for the 'PSBinaryModule' PowerShell binary module
.DESCRIPTION
This script contains the tasks for building the 'PSBinaryModule' PowerShell binary module
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '',
Justification = 'Suppress false positives in Invoke-Build tasks')]
param (
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]
$SemanticVersion,
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]
$NugetApiKey,
[Parameter()]
[ValidateSet('Debug', 'Release')]
[String]
$Configuration = 'Release'
)
# Synopsis: Default task
task . Clean, Build
# Setup build environment
Enter-Build {
$script:moduleName = 'PSBinaryModule'
$script:srcPath = Join-Path -Path $BuildRoot -ChildPath 'src'
$script:testPath = Join-Path -Path $BuildRoot -ChildPath 'tests'
$script:testOutputPath = Join-Path -Path $BuildRoot -ChildPath 'test-results'
$script:buildPath = Join-Path -Path $BuildRoot -ChildPath 'build'
$script:outputPath = Join-Path -Path $buildPath -ChildPath "out/$moduleName"
$script:solutionFile = Join-Path -Path $BuildRoot -ChildPath "$moduleName.sln"
}
# Synopsis: Restore NuGet packages
task Restore {
exec { dotnet restore $solutionFile }
}
# Synopsis: Build the C# project
task Compile Restore, {
$buildArgs = @(
'build'
$solutionFile
'--configuration', $Configuration
'--no-restore'
'--verbosity', 'minimal'
)
if ($SemanticVersion) {
$buildArgs += '/p:Version={0}' -f $SemanticVersion
}
exec { dotnet @buildArgs }
}
# Synopsis: Run C# unit tests with code coverage
task UnitTests Compile, {
if (-not (Test-Path $testOutputPath)) {
[void] (New-Item -Path $testOutputPath -ItemType Directory)
}
$testArgs = @(
'test'
$solutionFile
'--configuration', $Configuration
'--no-build'
'--verbosity', 'normal'
'--logger', "trx;LogFileName=$testOutputPath/unit-tests.trx"
'--collect', 'XPlat Code Coverage'
'--results-directory', $testOutputPath
)
exec { dotnet @testArgs }
# Move coverage file to expected location
$coverageFile = Get-ChildItem -Path $testOutputPath -Recurse -Filter 'coverage.cobertura.xml' | Select-Object -First 1
if ($coverageFile) {
$coverageDir = Join-Path -Path $testOutputPath -ChildPath 'codecoverage'
if (-not (Test-Path $coverageDir)) {
[void] (New-Item -Path $coverageDir -ItemType Directory)
}
Copy-Item -Path $coverageFile.FullName -Destination (Join-Path -Path $coverageDir -ChildPath 'code-coverage.xml') -Force
}
}
# Synopsis: Run PowerShell integration tests
task IntegrationTests {
if (-not (Test-Path $testOutputPath)) {
[void] (New-Item -Path $testOutputPath -ItemType Directory)
}
$integrationTestPath = Join-Path -Path $testPath -ChildPath 'Integration'
if (-not (Test-Path $integrationTestPath)) {
Write-Warning "No integration tests found at '$integrationTestPath'"
return
}
$config = New-PesterConfiguration @{
Run = @{
Path = $integrationTestPath
PassThru = $true
Exit = $true
}
TestResult = @{
Enabled = $true
OutputFormat = 'NUnitXml'
OutputPath = "$testOutputPath/integration-tests.xml"
}
Filter = @{
Tag = 'Integration'
}
Output = @{
Verbosity = 'Detailed'
}
}
Invoke-Pester -Configuration $config
}
# Synopsis: Run all tests
task Test UnitTests
# Synopsis: Build the module
task Build Compile, {
# Create output directory
if (-not (Test-Path $outputPath)) {
[void] (New-Item -Path $outputPath -ItemType Directory -Force)
}
# Copy compiled DLL
$dllPath = Join-Path -Path $srcPath -ChildPath "bin/$Configuration/$moduleName.dll"
Copy-Item -Path $dllPath -Destination $outputPath -Force
# Copy module manifest
$manifestPath = Join-Path -Path $srcPath -ChildPath "$moduleName.psd1"
$manifestContent = Get-Content -Path $manifestPath -Raw
# Update version in manifest if SemanticVersion is provided
if ($SemanticVersion) {
$manifestContent = $manifestContent -replace "ModuleVersion\s*=\s*'[^']*'", "ModuleVersion = '$SemanticVersion'"
}
$outputManifest = Join-Path -Path $outputPath -ChildPath "$moduleName.psd1"
Set-Content -Path $outputManifest -Value $manifestContent -Force
# Copy format file
$formatPath = Join-Path -Path $srcPath -ChildPath "$moduleName.Format.ps1xml"
if (Test-Path $formatPath) {
Copy-Item -Path $formatPath -Destination $outputPath -Force
}
# Copy help files
$helpPath = Join-Path -Path $srcPath -ChildPath 'en-US'
if (Test-Path $helpPath) {
$outputHelpPath = Join-Path -Path $outputPath -ChildPath 'en-US'
if (-not (Test-Path $outputHelpPath)) {
[void] (New-Item -Path $outputHelpPath -ItemType Directory -Force)
}
Copy-Item -Path "$helpPath/*" -Destination $outputHelpPath -Recurse -Force
}
# Copy dependencies
$binPath = Join-Path -Path $srcPath -ChildPath "bin/$Configuration"
Get-ChildItem -Path $binPath -Filter '*.dll' | Where-Object {
$_.Name -ne "$moduleName.dll" -and
$_.Name -ne 'PowerShellStandard.Library.dll'
} | ForEach-Object {
Copy-Item -Path $_.FullName -Destination $outputPath -Force
}
# Copy the deps.json file which helps with assembly resolution
$depsFile = Join-Path -Path $binPath -ChildPath "$moduleName.deps.json"
if (Test-Path $depsFile) {
Copy-Item -Path $depsFile -Destination $outputPath -Force
}
Write-Build Green "Module built successfully at: $outputPath"
}
# Synopsis: Create a NuGet package for the module
task Package {
$packageOutputPath = Join-Path -Path $buildPath -ChildPath 'package'
if (!(Test-Path $packageOutputPath)) {
[void] (New-Item -Path $packageOutputPath -ItemType Directory -Force)
}
$requestParam = @{
Name = "$($moduleName)_local_feed"
SourceLocation = $packageOutputPath
PublishLocation = $packageOutputPath
InstallationPolicy = 'Trusted'
ErrorAction = 'Stop'
}
[void] (Register-PSRepository @requestParam)
$requestParam = @{
Path = (Join-Path -Path $buildPath -ChildPath "out/$moduleName")
Repository = "$($moduleName)_local_feed"
NuGetApiKey = 'ABC123'
ErrorAction = 'Stop'
}
[void] (Publish-Module @requestParam)
[void] (Unregister-PSRepository -Name "$($moduleName)_local_feed")
}
# Synopsis: Publish the module to PSGallery
task Publish -If ($NugetApiKey) {
$requestParam = @{
Path = $outputPath
NuGetApiKey = $NugetApiKey
ErrorAction = 'Stop'
}
[void] (Publish-Module @requestParam)
}
# Synopsis: Clean up the build directory
task Clean {
if (Test-Path $buildPath) {
Write-Warning "Removing build output folder at '$buildPath'"
$requestParam = @{
Path = $buildPath
Recurse = $true
Force = $true
}
[void] (Remove-Item @requestParam)
}
# Clean dotnet build artifacts
$binDirs = Get-ChildItem -Path $BuildRoot -Directory -Recurse -Filter 'bin'
$objDirs = Get-ChildItem -Path $BuildRoot -Directory -Recurse -Filter 'obj'
foreach ($dir in ($binDirs + $objDirs)) {
Write-Warning "Removing $($dir.FullName)"
Remove-Item -Path $dir.FullName -Recurse -Force
}
}