-
Notifications
You must be signed in to change notification settings - Fork 16
/
PSCodeHealth.build.ps1
220 lines (190 loc) · 8.56 KB
/
PSCodeHealth.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#Requires -Modules 'InvokeBuild'
# Importing all build settings into the current scope
. '.\PSCodeHealth.BuildSettings.ps1'
Set-BuildHeader {
Param($Path)
Write-Build Cyan "Task $Path"
"`n" + ('-' * 79) + "`n" + "`t`t`t $($Task.Name.ToUpper()) `n" + ('-' * 79) + "`n"
}
Function Get-ModulePrivateFunction {
[CmdletBinding()]
[OutputType([System.Management.Automation.FunctionInfo])]
Param (
[Parameter(Mandatory)]
[string]$ModuleName
)
$ModuleInfo = $Null
$ModuleInfo = Get-Module -Name $ModuleName -ErrorAction SilentlyContinue
If ( -not $ModuleInfo ) {
Write-Error "Module '$ModuleName' not found"
}
$ScriptBlock = { $ExecutionContext.InvokeCommand.GetCommands('*', 'Function', $true) }
$PublicFunctions = ($ModuleInfo.ExportedCommands.GetEnumerator() | Select-Object -ExpandProperty Value).Name
& $ModuleInfo $ScriptBlock | Where-Object { $_.Source -eq $ModuleName -and $_.Name -notin $PublicFunctions }
}
task Clean {
If (Test-Path -Path $Settings.BuildOutput) {
"Removing existing files and folders in $($Settings.BuildOutput)\"
Get-ChildItem $Settings.BuildOutput | Remove-Item -Force -Recurse
}
Else {
"$($Settings.BuildOutput) is not present, nothing to clean up."
$Null = New-Item -ItemType Directory -Path $Settings.BuildOutput
}
}
task Install_Dependencies {
Foreach ( $Depend in $Settings.Dependency ) {
"Installing build dependency : $Depend"
If ( $Depend -eq 'Selenium.WebDriver' ) {
Install-Package $Depend -Source nuget.org -Force
}
Else {
Install-Module $Depend -Scope CurrentUser -Force -SkipPublisherCheck
Import-Module $Depend -Force
}
}
}
task Unit_Tests {
$UnitTestSettings = $Settings.UnitTestParams
$Script:UnitTestsResult = Invoke-Pester @UnitTestSettings
}
task Fail_If_Failed_Unit_Test {
$FailureMessage = '{0} Unit test(s) failed. Aborting build' -f $UnitTestsResult.FailedCount
assert ($UnitTestsResult.FailedCount -eq 0) $FailureMessage
}
task Publish_Unit_Tests_Coverage {
$Coverage = Format-Coverage -PesterResults $UnitTestsResult -CoverallsApiToken $Settings.CoverallsKey -BranchName $Settings.Branch
Publish-Coverage -Coverage $Coverage
}
task Integration_Tests {
$IntegrationTestSettings = $Settings.IntegrationTestParams
$Script:IntegrationTestsResult = Invoke-Pester @IntegrationTestSettings
}
task Fail_If_Failed_Integration_Test {
$FailureMessage = '{0} Integration test(s) failed. Aborting build' -f $IntegrationTestsResult.FailedCount
assert ($IntegrationTestsResult.FailedCount -eq 0) $FailureMessage
}
task Upload_Test_Results_To_AppVeyor {
$TestResultFiles = (Get-ChildItem -Path $Settings.BuildOutput -Filter '*TestsResult.xml').FullName
Foreach ( $TestResultFile in $TestResultFiles ) {
"Uploading test result file : $TestResultFile"
(New-Object 'System.Net.WebClient').UploadFile($Settings.TestUploadUrl, $TestResultFile)
}
}
task Upload_Test_Screenshots_To_Appveyor {
$ScreenShots = Get-ChildItem -Path $Settings.ScreenshotPath
Foreach ( $ScreenShot in $ScreenShots ) {
Push-AppveyorArtifact $ScreenShot.FullName
}
}
task Test Unit_Tests,
Fail_If_Failed_Unit_Test,
Publish_Unit_Tests_Coverage,
Integration_Tests,
Fail_If_Failed_Integration_Test,
Upload_Test_Results_To_AppVeyor,
Upload_Test_Screenshots_To_Appveyor
task Analyze {
Add-AppveyorTest -Name 'Code Analysis' -Outcome Running
$AnalyzeSettings = $Settings.AnalyzeParams
$Script:AnalyzeFindings = Invoke-ScriptAnalyzer @AnalyzeSettings
If ( $AnalyzeFindings ) {
$FindingsString = $AnalyzeFindings | Out-String
Write-Warning $FindingsString
Update-AppveyorTest -Name 'Code Analysis' -Outcome Failed -ErrorMessage $FindingsString
}
Else {
Update-AppveyorTest -Name 'Code Analysis' -Outcome Passed
}
}
task Fail_If_Analyze_Findings {
$FailureMessage = 'PSScriptAnalyzer found {0} issues. Aborting build' -f $AnalyzeFindings.Count
assert ( -not($AnalyzeFindings) ) $FailureMessage
}
Task Build_Documentation {
Remove-Module -Name $Settings.ModuleName -Force -ErrorAction SilentlyContinue
# platyPS + AppVeyor requires the module to be loaded in Global scope
Import-Module $Settings.ManifestPath -Force -Global
$HeaderContent = Get-Content -Path $Settings.HeaderPath -Raw
$HeaderContent += " - Public Functions:`n"
If (Test-Path -Path $Settings.PublicFunctionDocsPath) {
Get-ChildItem $Settings.PublicFunctionDocsPath | Remove-Item -Force -Recurse
}
Else {
$Null = New-Item -ItemType Directory -Path $Settings.PublicFunctionDocsPath
}
$PlatyPSSettings = $Settings.PlatyPSParams
New-MarkdownHelp @PlatyPSSettings | Foreach-Object {
$Part = ' - {0}: PublicFunctions/{1}' -f $_.BaseName, $_.Name
"Created markdown documentation file : $($_.FullName)"
$HeaderContent += "{0}`n" -f $Part
}
$HeaderContent | Set-Content -Path $Settings.MkdocsPath -Force
# Building the docs for the private functions
$MkdocsContent = Get-Content -Path $Settings.MkdocsPath -Raw
$MkdocsContent += " - Internal Functions:`n"
$PrivateFunctions = Get-ModulePrivateFunction -ModuleName $Settings.ModuleName | Where-Object Name -NotIn $Settings.FunctionsToExclude
If (Test-Path -Path $Settings.PrivateFunctionDocsPath) {
Get-ChildItem $Settings.PrivateFunctionDocsPath | Remove-Item -Force -Recurse
}
Else {
$Null = New-Item -ItemType Directory -Path $Settings.PrivateFunctionDocsPath
}
Foreach ( $PrivateFunction in $PrivateFunctions ) {
$FunctionDefinition = 'Function Global:{0} {{ {1} }}' -f $PrivateFunction.Name, $PrivateFunction.Definition
. ([scriptblock]::Create($FunctionDefinition))
$InternalDocsSettings = $Settings.InternalDocsPlatyPSParams
$NewMarkdownFile = New-MarkdownHelp @InternalDocsSettings -Command $PrivateFunction.Name
"Created markdown documentation file : $($NewMarkdownFile.FullName)"
$Part = ' - {0}: InternalFunctions/{1}' -f $NewMarkdownFile.BaseName, $NewMarkdownFile.Name
$MkdocsContent += "{0}`n" -f $Part
}
$MkdocsContent | Set-Content -Path $Settings.MkdocsPath -Force
Remove-Item "Function:\$($PrivateFunction.Name)" -ErrorAction SilentlyContinue
}
task Set_Module_Version {
$ManifestContent = Get-Content -Path $Settings.ManifestPath
$CurrentVersion = $Settings.VersionRegex.Match($ManifestContent).Groups['ModuleVersion'].Value
"Current module version in the manifest : $CurrentVersion"
$ManifestContent -replace $CurrentVersion,$Settings.Version | Set-Content -Path $Settings.ManifestPath -Force
$NewManifestContent = Get-Content -Path $Settings.ManifestPath
$NewVersion = $Settings.VersionRegex.Match($NewManifestContent).Groups['ModuleVersion'].Value
"Updated module version in the manifest : $NewVersion"
If ( $NewVersion -ne $Settings.Version ) {
Throw "Module version was not updated correctly to $($Settings.Version) in the manifest."
}
}
task Push_Build_Changes_To_Repo {
cmd /c "git config --global credential.helper store 2>&1"
Add-Content "$env:USERPROFILE\.git-credentials" "https://$($Settings.GitHubKey):[email protected]`n"
cmd /c "git config --global user.email ""$($Settings.Email)"" 2>&1"
cmd /c "git config --global user.name ""$($Settings.Name)"" 2>&1"
cmd /c "git config --global core.autocrlf true 2>&1"
cmd /c "git checkout $($Settings.Branch) 2>&1"
cmd /c "git add -A 2>&1"
cmd /c "git commit -m ""Commit build changes [ci skip]"" 2>&1"
cmd /c "git status 2>&1"
cmd /c "git push origin $($Settings.Branch) 2>&1"
}
task Copy_Source_To_Build_Output {
"Copying the source folder [$($Settings.SourceFolder)] into the build output folder : [$($Settings.BuildOutput)]"
Copy-Item -Path $Settings.SourceFolder -Destination $Settings.BuildOutput -Recurse
}
task Publish_Module_To_PSGallery {
Remove-Module -Name 'PSCodeHealth' -Force -ErrorAction SilentlyContinue
Write-Host "OutputModulePath : $($Settings.OutputModulePath)"
Write-Host "PSGalleryKey : $($Settings.PSGalleryKey)"
Get-PackageProvider -ListAvailable
Publish-Module -Path $Settings.OutputModulePath -NuGetApiKey $Settings.PSGalleryKey -Verbose
}
# Default task :
task . Clean,
Install_Dependencies,
Test,
Analyze,
Fail_If_Analyze_Findings,
Build_Documentation,
Set_Module_Version,
Push_Build_Changes_To_Repo,
Copy_Source_To_Build_Output,
Publish_Module_To_PSGallery