-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
psakeFile.ps1
256 lines (221 loc) · 9.67 KB
/
psakeFile.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
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
249
250
251
252
253
254
255
256
properties {
$projectRoot = $ENV:BHProjectPath
if(-not $projectRoot) {
$projectRoot = $PSScriptRoot
}
$sut = $env:BHModulePath
$tests = "$projectRoot\Tests"
$outputDir = Join-Path -Path $projectRoot -ChildPath 'out'
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
$psVersion = $PSVersionTable.PSVersion.Major
$pathSeperator = [IO.Path]::PathSeparator
$dotnetFramework = 'netstandard2.0'
$release = 'release'
$imageName = 'alpine3.14'
# $dockerImages = @(
# 'alpine3.8'
# 'ubuntu16.04'
# 'ubuntu18.04'
# #'nano1803'
# )
}
task default -depends Test
task Init {
"`nSTATUS: Testing with PowerShell $psVersion"
"Build System Details:"
Get-Item ENV:BH*
"`n"
} -description 'Initialize build environment'
task Test -Depends Init, Analyze, Pester -description 'Run test suite'
task Analyze -Depends Build {
$analysis = Invoke-ScriptAnalyzer -Path $outputModVerDir -Settings ./ScriptAnalyzerSettings.psd1 -Verbose:$false
$errors = $analysis | Where-Object {$_.Severity -eq 'Error'}
$warnings = $analysis | Where-Object {$_.Severity -eq 'Warning'}
if (($errors.Count -eq 0) -and ($warnings.Count -eq 0)) {
' PSScriptAnalyzer passed without errors or warnings'
}
if (@($errors).Count -gt 0) {
$errors | Format-Table -AutoSize
Write-Error -Message 'One or more Script Analyzer errors were found. Build cannot continue!'
}
if (@($warnings).Count -gt 0) {
$warnings | Format-Table -AutoSize
Write-Warning -Message 'One or more Script Analyzer warnings were found. These should be corrected.'
}
} -description 'Run PSScriptAnalyzer'
task Pester -Depends Build {
Push-Location
Set-Location -PassThru $outputModDir
if(-not $ENV:BHProjectPath) {
Set-BuildEnvironment -Path $PSScriptRoot\..
}
$origModulePath = $env:PSModulePath
if ( $env:PSModulePath.split($pathSeperator) -notcontains $outputDir ) {
$env:PSModulePath = ($outputDir + $pathSeperator + $origModulePath)
}
Remove-Module $ENV:BHProjectName -ErrorAction SilentlyContinue -Verbose:$false
Import-Module -Name $outputModDir -Force -Verbose:$false
$testResultsXml = Join-Path -Path $outputDir -ChildPath 'testResults.xml'
$testResults = Invoke-Pester -Path $tests -PassThru -OutputFile $testResultsXml -OutputFormat NUnitXml
# Upload test artifacts to AppVeyor
if ($env:APPVEYOR_JOB_ID) {
$wc = New-Object 'System.Net.WebClient'
$wc.UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", $testResultsXml)
}
if ($testResults.FailedCount -gt 0) {
$testResults | Format-List
Write-Error -Message 'One or more Pester tests failed. Build cannot continue!'
}
Pop-Location
$env:PSModulePath = $origModulePath
} -description 'Run Pester tests'
task CreateMarkdownHelp -Depends Compile {
# PoshBot functions
Import-Module -Name $outputModDir -Verbose:$false -Global
$mdHelpPath = Join-Path -Path $projectRoot -ChildPath 'docs/reference/functions'
$mdFiles = New-MarkdownHelp -Module $env:BHProjectName -OutputFolder $mdHelpPath -WithModulePage -Force
" PoshBot markdown help created at [$mdHelpPath]"
# Builtin commands
Import-Module -Name $outputModVerDir/Plugins/Builtin -Verbose:$false -Global
$mdHelpPath = Join-Path -Path $projectRoot -ChildPath 'docs/reference/commands'
$mdFiles = New-MarkdownHelp -Module 'Builtin' -OutputFolder $mdHelpPath -WithModulePage -Force
" Builtin plugin markdown help created at [$mdHelpPath]"
@('Builtin', $env:BHProjectName).ForEach({
Remove-Module -Name $_ -Verbose:$false
})
} -description 'Create initial markdown help files'
task UpdateMarkdownHelp -Depends Compile {
#Import-Module -Name $sut -Force -Verbose:$false
Import-Module -Name $outputModDir -Verbose:$false
$mdHelpPath = Join-Path -Path $projectRoot -ChildPath 'docs/reference/functions'
$mdFiles = Update-MarkdownHelpModule -Path $mdHelpPath -Verbose:$false
" Markdown help updated at [$mdHelpPath]"
} -description 'Update markdown help files'
task CreateExternalHelp -Depends CreateMarkdownHelp {
New-ExternalHelp "$projectRoot\docs\reference\functions" -OutputPath "$outputModVerDir\en-US" -Force > $null
} -description 'Create module help from markdown files'
Task RegenerateHelp -Depends UpdateMarkdownHelp, CreateExternalHelp
Task Publish -Depends Test {
" Publishing version [$($manifest.ModuleVersion)] to PSGallery..."
Publish-Module -Path $outputModVerDir -NuGetApiKey $env:PSGALLERY_API_KEY -Repository PSGallery
}
task Clean -depends Init {
Remove-Module -Name $env:BHProjectName -Force -ErrorAction SilentlyContinue
if (Test-Path -Path $outputModVerDir) {
Get-ChildItem -Path $outputModVerDir -Recurse | Remove-Item -Force -Recurse
} else {
New-Item -Path $outputModVerDir -ItemType Directory -Force > $null
}
" Cleaned previous output directory [$outputModVerDir]"
} -description 'Cleans module output directory'
task Compile -depends Clean {
# Create module output directory
$modDir = New-Item -Path $outputModDir -ItemType Directory -Force
New-Item -Path $outputModVerDir -Force -ItemType Directory > $null
# Append items to psm1
Write-Verbose -Message 'Creating psm1...'
$psm1 = Copy-Item -Path (Join-Path -Path $sut -ChildPath 'PoshBot.psm1') -Destination (Join-Path -Path $outputModVerDir -ChildPath "$($ENV:BHProjectName).psm1") -PassThru
# This is dumb but oh well :)
# We need to write out the classes in a particular order
$classDir = (Join-Path -Path $sut -ChildPath 'Classes')
@(
'Enums'
'LogMessage'
'Logger'
'BaseLogger'
'ExceptionFormatter'
'Event'
'Person'
'Room'
'Response'
'Message'
'Stream'
'CommandResult'
'ParsedCommand'
'CommandParser'
'Permission'
'CommandAuthorizationResult'
'AccessFilter'
'Role'
'Group'
'Trigger'
'StorageProvider'
'RoleManager'
'Command'
'CommandHistory'
'Plugin'
'PluginCommand'
'Approver'
'CommandExecutionContext'
'MiddlewareHook'
'MiddlewareConfiguration'
'CommandExecutor'
'ScheduledMessage'
'Scheduler'
'ConfigProvidedParameter'
'PluginManager'
'ConnectionConfig'
'Connection'
'Backend'
'ApprovalCommandConfiguration'
'ApprovalConfiguration'
'ChannelRule'
'BotConfiguration'
'Bot'
) | ForEach-Object {
Get-Content -Path (Join-Path -Path $classDir -ChildPath "$($_).ps1") | Add-Content -Path $psm1 -Encoding UTF8
}
Get-ChildItem -Path (Join-Path -Path $sut -ChildPath 'Private') -Recurse |
Get-Content -Raw | Add-Content -Path $psm1 -Encoding UTF8
Get-ChildItem -Path (Join-Path -Path $sut -ChildPath 'Public') -Recurse |
Get-Content -Raw | Add-Content -Path $psm1 -Encoding UTF8
Get-ChildItem -Path (Join-Path -Path $sut -ChildPath 'Implementations') -File -Recurse -Filter '*.ps1' -Exclude '*ServiceBusReceiver*.ps1' |
Get-Content -Raw | Add-Content -Path $psm1 -Encoding UTF8
# Copy over "lib" files.
# These are the Service Bus DLLs and scripts
New-Item -Path $outputModVerDir/lib -ItemType Directory > $null
Copy-Item -Path "$sut/lib/*" -Destination $outputModVerDir/lib -Recurse
Copy-Item -Path "$sut/Implementations/Teams/*netstandard*.ps1" -Destination $outputModVerDir/lib/linux
Copy-Item -Path "$sut/Implementations/Teams/*net45*.ps1" -Destination $outputModVerDir/lib/windows
# Copy over other items
Copy-Item -Path $env:BHPSModuleManifest -Destination $outputModVerDir
Copy-Item -Path (Join-Path -Path $classDir -ChildPath 'PoshBotAttribute.ps1') -Destination $outputModVerDir
Copy-Item -Path (Join-Path -Path $sut -ChildPath 'Plugins') -Destination $outputModVerDir -Recurse
Copy-Item -Path (Join-Path -Path $sut -ChildPath 'Task') -Destination $outputModVerDir -Recurse
# Fix case of PSM1 and PSD1
Rename-Item -Path $outputModVerDir/poshbot.psd1 -NewName PoshBot.psd1 -ErrorAction Ignore
Rename-Item -Path $outputModVerDir/poshbot.psm1 -NewName PoshBot.psm1 -ErrorAction Ignore
" Created compiled module at [$modDir]"
} -description 'Compiles module from source'
task Build -depends Compile, CreateMarkdownHelp, CreateExternalHelp {
# External help
$helpXml = New-ExternalHelp "$projectRoot\docs\reference\functions" -OutputPath (Join-Path -Path $outputModVerDir -ChildPath 'en-US') -Force
" Module XML help created at [$helpXml]"
}
task Build-Docker {
$version = $manifest.ModuleVersion.ToString()
Push-Location
Set-Location -Path $projectRoot
$dockerFilePath = Join-Path $projectRoot -ChildPath 'docker' -AdditionalChildPath @($imageName, 'Dockerfile')
$tag = "$imageName-$version"
$fullImageName = "poshbotio/poshbot`:$tag"
"Building docker image: $fullImageName"
exec {
& docker build -t $fullImageName --label version=$version -f $dockerFilePath .
}
Pop-Location
} -description 'Create Docker container'
task Publish-Docker -depends Build-Docker {
$version = $manifest.ModuleVersion.ToString()
exec {
docker login
}
$tag = "$imageName-$version"
$fullImageName = "poshbotio/poshbot`:$tag"
"Publishing docker image: $fullImageName"
exec {
docker push $fullImageName
}
}