-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.ps1
204 lines (154 loc) · 7.19 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
[CmdletBinding()]
[OutputType([System.Void])]
param (
[switch]$Install
)
Import-Module -Name Pester -MinimumVersion 5.5.0
Import-Module -Name Cofl.Util -MinimumVersion 1.2.2
Import-Module -Name '.\build-functions.psm1' -Force
$InformationPreference = 'Continue'
$ErrorActionPreference = 'Stop'
# PesterConfiguration
$PesterConfiguration = [PesterConfiguration]::Default
$PesterConfiguration.Run.Exit = $false
$PesterConfiguration.CodeCoverage.Enabled = $false
$PesterConfiguration.Output.Verbosity = 'Detailed'
$PesterConfiguration.Run.PassThru = $true
$PesterConfiguration.Should.ErrorAction = 'Stop'
# $ScriptRules = @(
# '../ScriptAnalyzerRules/Indented.CodingConventions/'
# #, './Analyzer/PSScriptAnalyzer/Tests/Engine/CommunityAnalyzerRules/'
# #, './Analyzer/InjectionHunter/'
# )
$projectPath = Resolve-Path -Path '.\'
$sourcePath = Resolve-Path -Path "$projectPath\source"
$testsPath = Resolve-Path -Path "$projectPath\tests"
$ignoreFile = Resolve-Path -Path '.psqcignore'
$moduleName = 'PSQualityCheck'
$buildFolder = Join-Path -Path "$projectPath" -ChildPath 'build'
$artifactsFolder = Join-Path -Path "$projectPath" -ChildPath 'artifacts'
if (-not (Test-Path -Path $artifactsFolder -ErrorAction SilentlyContinue)) {
New-Item -Path $artifactsFolder -ItemType 'directory' -Force
}
if (-not (Test-Path -Path $buildFolder -ErrorAction SilentlyContinue)) {
New-Item -Path $buildFolder -ItemType 'directory' -Force
}
$modules = Get-ChildItem -Path $sourcePath -Directory
Write-Host 'BUILD> Build bootstrap version of PSQualityCheck' -ForegroundColor Black -BackgroundColor Gray
# Create a bootstrap built version of the module (pre-testing)
# to allow us to use this module (PSQualityCheck) to test itself whilst building
try {
foreach ($module in $modules) {
$repositoryName = "$module-local"
if ($module -in ((Get-Module) | Select-Object -Property Name)) {
Remove-Module $module
}
$buildPropertiesFile = "$sourcePath\$($module.BaseName)\build.psd1"
Write-Host $buildPropertiesFile
Build-Module -SourcePath $buildPropertiesFile
if ($repositoryName -in ((Get-PSRepository) | Select-Object -Property Name)) {
Unregister-PSRepository -Name $repositoryName
}
Publish-BuiltModule -Module $moduleName -ArtifactsFolder $artifactsFolder -BuildFolder $buildFolder -SourceFolder $sourcePath -Clean
Install-BuiltModule -Module $moduleName
}
} catch {
# try and tidy up?
# foreach ($module in $modules) {
# Uninstall-BuiltModule -Module $module
# Unpublish-BuiltModule -Module $module -SourceFolder $sourcePath -ArtifactsFolder $artifactsFolder
# }
throw $_
break
}
## Commented out running PSQualityCheck on this code as it doesn't work
# # Start of Project Based checks
# Write-Host 'BUILD> Running PSQualityCheck Project checks' -ForegroundColor Black -BackgroundColor Gray
# $qualityCheckSplat = @{
# 'ProjectPath' = $projectPath
# 'ScriptAnalyzerRulesPath' = $ScriptRules
# 'HelpRulesPath' = (Resolve-Path -Path '.\HelpRules.psd1')
# 'Passthru' = $true
# 'PesterConfiguration' = $PesterConfiguration
# 'IgnoreFile' = $ignoreFile
# }
# $qualityCheckResult = Invoke-PSQualityCheck @qualityCheckSplat
# # End of Project Based checks
# # unpublish and remove the bootstrap version of the modules
# Write-Host 'BUILD> Uninstall and Unpublish bootstrap modules' -ForegroundColor Black -BackgroundColor Gray
# foreach ($module in $modules) {
# Uninstall-BuiltModule -Module $module.Name
# Unpublish-BuiltModule -Module $module.Name -SourceFolder $sourcePath -ArtifactsFolder $artifactsFolder
# }
# # Running tests
# if ($qualityCheckResult.Script.FailedCount -ne 0 -or $qualityCheckResult.Project.FailedCount -ne 0) {
# Write-Error -Message 'Project quality check failed'
# }
# Run the unit tests for the public functions of any modules in the project
Write-Host 'BUILD> Running unit tests for functions' -ForegroundColor Black -BackgroundColor Gray
$unitTestResults = @()
# Get the modules (the directories in the Source folder)
$modules = Get-ChildItem -Path $sourcePath -Directory
foreach ($module in $modules) {
# Get the public functions (minus any excluded by the ignore file)
$functionFiles = @()
$functionFiles += Get-FilteredChildItem -Path (Join-Path -Path $module.FullName -ChildPath 'public') -IgnoreFileName $ignoreFile
# If there are any scripts in the private folder with corresponding tests then run those too
# $privateFunctionFiles += Get-ChildItem -Path (Join-Path -Path $Module.FullName -ChildPath "private")
# Write-Host $privateFunctionFiles.Count -ForegroundColor Yellow
# foreach ($function in $privateFunctionFiles) {
# Write-Host $function.FullName -ForegroundColor Yellow
# if (Test-Path -Path ".\tests\unit\$($module.BaseName)\$($function.BaseName).Tests.ps1") {
# $functionFiles += (Get-ChildItem -Path ".\tests\unit\$($module.BaseName)\$($function.BaseName).Tests.ps1")
# }
# }
foreach ($function in $functionFiles) {
$fileContent = Get-FunctionFileContent -Path $function.FullName
. "$($function.FullName)"
$container = New-PesterContainer -Path "$testsPath\unit\$($module.BaseName)\$($function.BaseName).Tests.ps1" -Data @{FileContent = $fileContent }
$PesterConfiguration.Run.Container = $container
$unitTestResults += Invoke-Pester -Configuration $PesterConfiguration
}
}
$unitTestFailedCount = 0
foreach ($result in $unitTestResults) {
$unitTestFailedCount += $result.FailedCount
}
# Check to see whether the unit tests have failed
if ($unitTestFailedCount -ne 0 ) {
Write-Error -Message 'One or more module were not built because there were function unit test errors'
throw $_
}
# TODO: Add integration tests here
# End of running tests
## End of bootstrap build and tests
# Start of final module build
# Build the module(s) only if there are no unit/integration test failures
Write-Host 'BUILD> Building final version of module' -ForegroundColor Black -BackgroundColor Gray
try {
foreach ($module in $modules) {
# QUERY: Why not just do Remove-Module -Name $module ? It will do nothing if there is no module loaded ?
if ($module -in ((Get-Module) | Select-Object -Property Name)) {
Remove-Module $module
}
$buildPropertiesFile = "$sourcePath\$($module.BaseName)\build.psd1"
Write-Host $buildPropertiesFile
Build-Module -SourcePath $buildPropertiesFile
}
} catch {
throw $_
break
}
# End of final module build
# Publish the final built modules
foreach ($module in $modules) {
Write-Host "BUILD> Publish final built module: $module" -ForegroundColor Black -BackgroundColor Gray
$repositoryName = "$module-local"
Publish-BuiltModule -Module $module.Name -ArtifactsFolder $artifactsFolder -BuildFolder $buildFolder -SourceFolder $sourcePath -Clean
# Optionally install
if ($PSBoundParameters.ContainsKey('Install')) {
Write-Host "BUILD> Install final built module: $module" -ForegroundColor Black -BackgroundColor Gray
Install-BuiltModule -Module $module.Name
}
}
### END OF SCRIPT