-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensionMethod.Build.ps1
227 lines (185 loc) · 7.58 KB
/
ExtensionMethod.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
221
222
223
224
225
226
227
#Requires -Modules InvokeBuild
<#
.Synopsis
Build script invoked by Invoke-Build.
.Description
Build the Powershell project Extensionmethod
#>
[CmdletBinding()]
param(
[ValidateSet('Release','Debug')]
[string] $Configuration,
[ValidateSet('Dev','Prod')]
[string] $Environnement
)
$WarningPreference = "Continue"
if ($PSBoundParameters.ContainsKey('Verbose'))
{ $VerbosePreference = "Continue" }
if ($PSBoundParameters.ContainsKey('Debug'))
{ $DebugPreference = "Continue" }
. $PSScriptRoot\Extensionmethod.BuildSettings.ps1 @PSBoundParameters
###############################################################################
# Core task implementations. Avoid modifying these tasks.
###############################################################################
task . Test
task Init {
NewDirectory -Path $ModuleOutDir -TaskName $Task.Name
}
task Clean Init, {
# Maybe a bit paranoid but this task nuked \ on my laptop. Good thing I was not running as admin.
if ($ModuleOutDir.Length -gt 3)
{ Get-ChildItem $ModuleOutDir | Remove-Item -Recurse -Force -Verbose:($VerbosePreference -eq 'Continue') }
else
{ Write-Verbose "$($Task.Name) - `$ModuleOutDir '$ModuleOutDir' must be longer than 3 characters." }
}
task StageFiles Init, Clean, BeforeStageFiles, CoreStageFiles, AfterStageFiles, {
}
task CoreStageFiles {
Copy-Item -Path $SrcRootDir\* -Destination "$OutDir\$ProjectName" -Recurse -Exclude $Exclude -Verbose:($VerbosePreference -eq 'Continue')
Write-Host "The files have been copied to '$ModuleOutDir'"
}
task Build Init, Clean, BeforeBuild, StageFiles, Analyze, BuildHelp, AfterBuild, {
}
task Actionlint -If ($null -ne (Get-Command gh.exe -EA SilentlyContinue) ) {
# Linting all workflow files only in .\.github\workflows directory
if (Get-Command gh.exe)
{
$isActionLintExist=(gh extension list|Where-Object {$_ -match 'actionlint'}|Select-Object -first 1) -ne $null
if (-not $isActionLintExist)
{ Throw "Github Cli: 'actionlint' extension not found. Use : gh extension install cschleiden/gh-actionlint"}
$ActionLintErrors=gh actionlint -format '{{json .}}'|ConvertFrom-Json
$ExitCode=$LastExitCode
if ($ExitCode -ne 0)
{
$ErrorFiles=$ActionLintErrors|Group-Object filepath
$ofs=' , '
gh actionlint
Throw "One or more Github Action lint errors were found : $($ErrorFiles.Name). Build cannot continue."
}
}
else
{ Throw 'gh.exe (Github Cli) not found. See https://cli.github.com/'}
}
task Analyze StageFiles, ActionLint, {
if (!$ScriptAnalysisEnabled) {
"Script analysis is not enabled. Skipping $($Task.Name) task."
return
}
if (!(Get-Module PSScriptAnalyzer -ListAvailable)) {
"PSScriptAnalyzer module is not installed. Skipping $($Task.Name) task."
return
}
"ScriptAnalysisFailBuildOnSeverityLevel set to: $ScriptAnalysisFailBuildOnSeverityLevel"
$analysisResult = Invoke-ScriptAnalyzer -Path $ModuleOutDir -Settings $ScriptAnalyzerSettingsPath -Recurse -Verbose:($VerbosePreference -eq 'Continue') #-IncludeDefaultRules
$analysisResult | Format-Table
switch ($ScriptAnalysisFailBuildOnSeverityLevel) {
'None' {
return
}
{$_ -in 'Error','ParseError'} {
$Count=@($analysisResult | Where-Object {$_.Severity -eq 'Error' -or $_.Severity -eq 'ParseError'}).Count
Assert ( $Count -eq 0 ) 'One or more ScriptAnalyzer errors were found. Build cannot continue.'
}
'Warning' {
$Count=@($analysisResult | Where-Object {$_.Severity -eq 'Warning' -or $_.Severity -eq 'Error' -or $_.Severity -eq 'ParseError'}).Count
Assert ( $Count -eq 0) 'One or more ScriptAnalyzer warnings were found. Build cannot continue.'
}
default {
Assert ($analysisResult.Count -eq 0) 'One or more ScriptAnalyzer issues were found. Build cannot continue.'
}
}
}
task Install Build, BeforeInstall, CoreInstall, AfterInstall, {
}
task CoreInstall {
if (!(Test-Path -LiteralPath $InstallPath)) {
Write-Verbose 'Creating install directory'
New-Item -Path $InstallPath -ItemType Directory -Verbose:($VerbosePreference -eq 'Continue') > $null
}
Copy-Item -Path $ModuleOutDir\* -Destination $InstallPath -Verbose:($VerbosePreference -eq 'Continue') -Recurse -Force
"Module installed into $InstallPath"
}
task Test Build, {
Write-Warning "todo"
Return
if (!(Get-Module Pester -ListAvailable)) {
"Pester module is not installed. Skipping $($Task.Name) task."
return
}
Import-Module Pester
try {
Microsoft.PowerShell.Management\Push-Location -LiteralPath $TestRootDir
.\Run.ps1
# if ($TestOutputFile) {
# $testing = @{
# OutputFile = $TestOutputFile
# OutputFormat = $TestOutputFormat
# PassThru = $true
# Verbose = $VerbosePreference
# }
# }
# else {
# $testing = @{
# PassThru = $true
# Verbose = $VerbosePreference
# }
# }
# To control the Pester code coverage, a boolean $CodeCoverageEnabled is used.
# if ($CodeCoverageEnabled) {
# $testing.CodeCoverage = $CodeCoverageFiles
# }
# $testResult = Invoke-Pester @testing
# Assert ( $testResult.FailedCount -eq 0) 'One or more Pester tests failed, build cannot continue.'
# if ($CodeCoverageEnabled) {
# $testCoverage = [int]($testResult.CodeCoverage.NumberOfCommandsExecuted /
# $testResult.CodeCoverage.NumberOfCommandsAnalyzed * 100)
# "Pester code coverage on specified files: ${testCoverage}%"
# }
}
finally {
Microsoft.PowerShell.Management\Pop-Location
}
}
Task BuildHelp BeforeBuildHelp, GenerateHelpFiles, AfterBuildHelp, {
if (!$IsHelpGeneration) {
"Script building help file is not enabled. Skipping $($Task.Name) task."
return
}
}
Task GenerateHelpFiles {
# presupposes the existence of .md files generated by New-MarkdownHelp
if (!(Get-Module platyPS -ListAvailable)) {
"platyPS module is not installed. Skipping $($Task.Name) task."
return
}
if (!(Get-ChildItem -LiteralPath $DocsRootDir -Filter *.md -Recurse -ErrorAction SilentlyContinue)) {
"No markdown help files to process. Skipping $($Task.Name) task."
return
}
$helpLocales = (Get-ChildItem -Path $DocsRootDir -Directory).Name
# Generate the module's primary MAML help file.
foreach ($locale in $helpLocales) {
New-ExternalHelp -Path $DocsRootDir\$locale -OutputPath $ModuleOutDir\$locale -Force `
-ErrorAction SilentlyContinue -Verbose:($VerbosePreference -eq 'Continue') > $null
}
}
Task Publish Build, Test, BuildHelp, BeforePublish, CorePublish, AfterPublish, {
}
Task CorePublish {
. Test-Requisite -Environnement $Environnement
Write-Host "Published on the repository : '$PublishRepository'"
$publishParams = @{
Path = $ModuleOutDir
NuGetApiKey = $NuGetApiKey
}
# If an alternate repository is specified, set the appropriate parameter.
if ($PublishRepository) {
$publishParams['Repository'] = $PublishRepository
}
# Consider not using -ReleaseNotes parameter when Update-ModuleManifest has been fixed.
if ($ReleaseNotesPath) {
$publishParams['ReleaseNotes'] = @(Get-Content $ReleaseNotesPath)
}
"Calling Publish-Module ..."
Publish-Module @publishParams
}