-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild-Module.ps1
More file actions
191 lines (151 loc) · 6.12 KB
/
Build-Module.ps1
File metadata and controls
191 lines (151 loc) · 6.12 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
[CmdletBinding(DefaultParameterSetName = "All")]
param(
[Parameter(Position = 0)]
[string]$name,
#output path of the build module
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[string]$outputDir = './dist',
# Building help is skipped by default to speed your inner loop.
# Use this flag to include building the help
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[switch]$buildHelp,
# By default the build will not install dependencies
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[switch]$installDep,
# built module will be imported into session
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[switch]$ipmo,
# run the scripts with the PS script analyzer
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[switch]$analyzeScript,
# runs the unit tests
[Parameter(ParameterSetName = "UnitTest", Mandatory = $true)]
[Parameter(ParameterSetName = "All")]
[switch]$runTests,
# can be used to filter the unit test parts that should be run
# see also: https://github.com/pester/Pester/wiki/Invoke%E2%80%90Pester#testname-alias-name
[Parameter(ParameterSetName = "UnitTest")]
[string]$testName,
# outputs the code coverage
[Parameter(ParameterSetName = "UnitTest")]
[switch]$codeCoverage,
# runs the integration tests
[Parameter(ParameterSetName = "UnitTest")]
[Parameter(ParameterSetName = "All")]
[switch]$runIntegrationTests
)
if (-not $name) {
$name = (get-item .).Name
Write-Output "Defaulting to '$name', pass -name to override"
}
function Import-Pester {
if ($null -eq $(Get-Module -ListAvailable Pester | Where-Object Version -like '5.*')) {
Write-Output "Installing Pester 5"
Install-Module -Name Pester -Repository PSGallery -Force -AllowPrerelease -MinimumVersion '5.0.2' -Scope CurrentUser -AllowClobber -SkipPublisherCheck
}
# This loads [PesterConfiguration] into scope
Import-Module Pester -MinimumVersion 5.0.0
}
function Start-IntegrationTests {
[CmdletBinding(DefaultParameterSetName = "All", SupportsShouldProcess, ConfirmImpact = "High")]
param()
process {
Import-Pester
$pesterArgs = [PesterConfiguration]::Default
$pesterArgs.Run.Path = '.\integration'
$pesterArgs.Run.Exit = $true
$pesterArgs.Output.Verbosity = "Detailed"
$pesterArgs.TestResult.Enabled = $true
$pesterArgs.TestResult.OutputPath = 'integrationTest-results.xml'
$pesterArgs.Run.PassThru = $false
Invoke-Pester -Configuration $pesterArgs
}
}
. ./Merge-File.ps1
if ($installDep.IsPresent -or $analyzeScript.IsPresent) {
# Load the psd1 file so you can read the required modules and install them
$manifest = Import-PowerShellDataFile ".\Source\$name.psd1"
# Install each module
if ($manifest.RequiredModules) {
$manifest.RequiredModules | ForEach-Object { if (-not (get-module $_ -ListAvailable)) { Write-Host "Installing $_"; Install-Module -SkipPublisherCheck -Name $_ -Repository PSGallery -F -Scope CurrentUser } }
}
}
if ([System.IO.Path]::IsPathRooted($outputDir)) {
$output = $outputDir
}
else {
$output = Join-Path (Get-Location) $outputDir
}
$output = [System.IO.Path]::GetFullPath($output)
Merge-File -inputFile ./Source/_functions.json -outputDir $output
Merge-File -inputFile ./Source/types/_types.json -outputDir $output
Merge-File -inputFile ./Source/Classes/_classes.json -outputDir $output
Merge-File -inputFile ./Source/formats/_formats.json -outputDir $output
# Build the help
if ($buildHelp.IsPresent) {
Write-Output 'Creating help files'
./Gen-Help.ps1
}
Write-Output 'Publishing about help files'
Copy-Item -Path ./Source/en-US -Destination "$output/" -Recurse -Force
Copy-Item -Path "./Source/$name.psm1" -Destination "$output/$name.psm1" -Force
$newValue = "'*'"
if (Test-Path "./Source/Public") {
Write-Output 'Updating Functions To Export'
$newValue = ((Get-ChildItem -Path "./Source/Public" -Filter '*.ps1').BaseName |
ForEach-Object -Process { Write-Output "'$_'" }) -join ','
}
(Get-Content "./Source/$name.psd1") -Replace ("FunctionsToExport.+", "FunctionsToExport = @($newValue)") | Set-Content "$output/$name.psd1"
Write-Output "Publish complete to $output"
# run the unit tests with Pester
if ($runTests.IsPresent -and (Test-Path '.\unit')) {
Import-Pester
$pesterArgs = [PesterConfiguration]::Default
$pesterArgs.Run.Path = '.\unit'
$pesterArgs.Output.Verbosity = "Detailed"
$pesterArgs.TestResult.Enabled = $true
$pesterArgs.TestResult.OutputPath = 'test-results.xml'
if ($codeCoverage.IsPresent) {
$pesterArgs.CodeCoverage.Enabled = $true
$pesterArgs.CodeCoverage.OutputFormat = 'JaCoCo'
$pesterArgs.CodeCoverage.OutputPath = "coverage.xml"
$pesterArgs.CodeCoverage.Path = "./Source/**/*.ps1"
}
else {
$pesterArgs.Run.PassThru = $false
}
if ($testName) {
$pesterArgs.Filter.FullName = $testName
}
Invoke-Pester -Configuration $pesterArgs
}
# reload the just built module
if ($ipmo.IsPresent -or $analyzeScript.IsPresent -or $runIntegrationTests.IsPresent) {
# module needs to be unloaded if present
if ((Get-Module $name)) {
Remove-Module $name
}
Write-Host "Importing module"
Import-Module "$output/$name.psd1" -Force
}
# Run this last so the results can be seen even if tests were also run
# if not the results scroll off and my not be in the buffer.
# run PSScriptAnalyzer
if ($analyzeScript.IsPresent) {
Write-Output "Starting static code analysis..."
if ($null -eq $(Get-Module -Name PSScriptAnalyzer)) {
Install-Module -Name PSScriptAnalyzer -Repository PSGallery -Force -Scope CurrentUser
}
$r = Invoke-ScriptAnalyzer -Path $output -Recurse
$r | ForEach-Object { Write-Host "##vso[task.logissue type=$($_.Severity);sourcepath=$($_.ScriptPath);linenumber=$($_.Line);columnnumber=$($_.Column);]$($_.Message)" }
Write-Output "Static code analysis complete."
}
# run integration tests with Pester
if ($runIntegrationTests.IsPresent) {
Start-IntegrationTests
}