-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild.ps1
More file actions
170 lines (132 loc) · 6.05 KB
/
Build.ps1
File metadata and controls
170 lines (132 loc) · 6.05 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
Import-Module -Name Pester -MinimumVersion 5.2.0
Import-Module -Name Cofl.Util -MinimumVersion 1.2.2
Import-Module -Name ".\build-functions.psm1"
$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"
$scriptsPath = Resolve-Path -Path "$projectPath\scripts"
$testsPath = Resolve-Path -Path "$projectPath\tests"
$ignoreFile = Resolve-Path -Path ".psqcignore"
#$moduleName = "TridentCC.Common"
$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
}
# Start of Project Based checks
$qualityCheckSplat = @{
'ProjectPath' = $projectPath
'ScriptAnalyzerRulesPath' = $ScriptRules
'HelpRulesPath' = (Resolve-Path -Path '.\HelpRules.psd1')
'Passthru' = $true
'PesterConfiguration' = $PesterConfiguration
'IgnoreFile' = $ignoreFile
}
$qualityResult = Invoke-PSQualityCheck @qualityCheckSplat
# End of Project Based checks
# Running tests
if ($qualityResult.Script.FailedCount -eq 0 -and $qualityResult.Project.FailedCount -eq 0) {
$testResults = @()
# Run the unit tests for the public functions of any modules in the project
# 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
$testResults += Invoke-Pester -Configuration $PesterConfiguration
}
}
# TODO: Add integration tests here
}
else {
# Write-Information 'Functions not tested - there were project quality check errors'
# Write-Warning -Message "Project Quality Check fails"
Write-Error -Message "Project quality check failed"
break
}
# End of running tests
# Start of module build
# Build the module(s) only if there are no unit/integration test failures
$testFailedCount = 0
foreach ($result in $testResults) {
$testFailedCount += $result.FailedCount
}
if ($testFailedCount -eq 0 ) {
foreach ($module in $modules) {
$buildPropertiesFile = ".\source\$($module.BaseName)\build.psd1"
Build-Module -SourcePath $buildPropertiesFile
}
}
else {
Write-Error -Message 'One or more module were not built because there were function unit test errors'
throw
}
# End of module build
# Run any available unit tests for files in Scripts folder
$scriptFiles = @()
$testResults = @()
$scriptFiles += Get-FilteredChildItem -Path $scriptsPath -IgnoreFileName $ignoreFile
foreach ($scriptFile in $scriptFiles) {
$scriptFolder = $scriptFile.FullName -ireplace [regex]::Escape($scriptsPath.Path), ''
$scriptFolder = $scriptFolder -ireplace [regex]::Escape($scriptFile.Name), ''
$fileContent = Get-FunctionFileContent -Path $scriptFile.FullName
$container = New-PesterContainer -Path "$testsPath\scripts$scriptFolder\$($scriptFile.BaseName).Tests.ps1" -Data @{FileContent = $fileContent }
$PesterConfiguration.Run.Container = $container
$testResults += Invoke-Pester -Configuration $PesterConfiguration
}
$testFailedCount = 0
foreach ($result in $testResults) {
$testFailedCount += $result.FailedCount
}
# If there are no script failures then copy the scripts to the build folder and archive to the Artifacts folder
if ($testFailedCount -eq 0) {
$builtScriptsFolder = Join-Path -Path $buildFolder -ChildPath "scripts"
if (-not (Test-Path -Path $builtScriptsFolder -ErrorAction SilentlyContinue)) {
New-Item -Path $buildFolder -Name "scripts" -ItemType "directory" -force
}
Copy-Item -Path "Scripts" -Destination $buildFolder -Recurse -Force -Container
$compressSplat = @{
Path = $builtScriptsFolder
CompressionLevel = "Fastest"
DestinationPath = "$artifactsFolder\scripts.zip"
Update = $true
}
Compress-Archive @compressSplat
}
else {
Write-Error -Message "Scripts were not copied to artifacts folder because there were failed unit tests"
break
}
# End of script copy
### END OF SCRIPT