-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuild-ModuleWithHelp.ps1
More file actions
303 lines (243 loc) · 12.2 KB
/
Build-ModuleWithHelp.ps1
File metadata and controls
303 lines (243 loc) · 12.2 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# Build-ModuleWithHelp.ps1
# Complete workflow: Generate module code + XML help + validation
<#
.SYNOPSIS
Builds a complete PowerShell module from OpenAPI specification including XML help.
.DESCRIPTION
This script orchestrates the entire module build process:
1. Generates PowerShell module code from OpenAPI spec (ConvertProject.ps1)
2. Generates MAML XML help files (ConvertToXmlHelp.ps1)
3. Validates the generated help file (Test-XmlHelp.ps1)
4. Optionally copies files to a target module directory
Requires PowerShell 7.0 or later.
.PARAMETER ProjectName
The name of the API/project (matches OpenAPI JSON filename)
.PARAMETER ModuleConfig
Hashtable containing all configuration parameters for both ConvertProject.ps1 and ConvertToXmlHelp.ps1
.PARAMETER DestinationPath
Optional path to copy the completed module files
.PARAMETER SkipValidation
Skip the help file validation step
.PARAMETER Force
Force overwrite existing files without prompting
.EXAMPLE
$config = @{
ProjectName = "GitHub"
GenerateMainModule = $true
LastNounToVern = @{ disable = "Disable"; enable = "Enable" }
FunctionRename = @{
"New-GitHubReposActionsWorkflowsDispatches" = "Start-GitHubReposActionsWorkflows"
}
}
.\Build-ModuleWithHelp.ps1 -ModuleConfig $config
.EXAMPLE
.\Build-ModuleWithHelp.ps1 -ProjectName "Cadmium" -DestinationPath "C:\Modules\Cadmium"
#>
#Requires -Version 7.0
[CmdletBinding()]
param(
[Parameter(Mandatory, Position=0)]
[string]$ProjectName,
[hashtable]$ModuleConfig = @{},
[string]$DestinationPath,
[switch]$SkipValidation,
[switch]$Force
)
$ErrorActionPreference = "Stop"
$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
# Ensure ProjectName is in config
if (!$ModuleConfig.ContainsKey('ProjectName')) {
$ModuleConfig['ProjectName'] = $ProjectName
}
Write-Host "╔════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ PowerShell Module Builder with XML Help ║" -ForegroundColor Cyan
Write-Host "║ Project: $($ProjectName.PadRight(47)) ║" -ForegroundColor Cyan
Write-Host "╚════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""
$buildSteps = @(
@{ Name = "Generate Module Code"; Script = "ConvertProject.ps1"; Required = $true }
@{ Name = "Generate XML Help"; Script = "ConvertToXmlHelp.ps1"; Required = $true }
@{ Name = "Validate Help File"; Script = "Test-XmlHelp.ps1"; Required = $false }
)
$results = @{
Success = $true
Steps = @()
Errors = @()
}
# Step 1: Generate Module Code
try {
Write-Host "[ 1/3 ] Generating PowerShell module code..." -ForegroundColor Yellow
Write-Host " Running: ConvertProject.ps1" -ForegroundColor Gray
$convertScript = Join-Path $ScriptPath "ConvertProject.ps1"
if (!(Test-Path $convertScript)) {
throw "ConvertProject.ps1 not found at $convertScript"
}
# Filter parameters for ConvertProject.ps1 (exclude help-specific parameters)
$convertParams = @{}
$convertProjectValidParams = @('ProjectName', 'MultipleFiles', 'BoolAsSwitch', 'Force',
'LastNounToVern', 'FunctionRename', 'FunctionRenamePattern',
'AdditionalParameters', 'DescriptionToVerb', 'FunctionNeverRequired',
'FunctionNeverAdded', 'OutPath', 'GenerateMainModule', 'MainProtocol')
foreach ($key in $ModuleConfig.Keys) {
if ($key -in $convertProjectValidParams) {
$convertParams[$key] = $ModuleConfig[$key]
}
}
& $convertScript @convertParams
if ($LASTEXITCODE -ne 0 -and $LASTEXITCODE -ne $null) {
throw "ConvertProject.ps1 failed with exit code $LASTEXITCODE"
}
Write-Host " ✓ Module code generated" -ForegroundColor Green
$results.Steps += @{ Step = 1; Name = "Module Code"; Status = "Success" }
} catch {
Write-Host " ❌ Failed to generate module code" -ForegroundColor Red
Write-Host " Error: $($_.Exception.Message)" -ForegroundColor Red
$results.Success = $false
$results.Errors += $_.Exception.Message
$results.Steps += @{ Step = 1; Name = "Module Code"; Status = "Failed" }
}
# Step 2: Generate XML Help
if ($results.Success) {
try {
Write-Host "`n[ 2/3 ] Generating XML help file..." -ForegroundColor Yellow
Write-Host " Running: ConvertToXmlHelp.ps1" -ForegroundColor Gray
$helpScript = Join-Path $ScriptPath "ConvertToXmlHelp.ps1"
if (!(Test-Path $helpScript)) {
throw "ConvertToXmlHelp.ps1 not found at $helpScript"
}
# Filter parameters for ConvertToXmlHelp.ps1 (exclude code-gen-specific parameters)
$helpParams = @{}
$convertHelpValidParams = @('ProjectName', 'MultipleFiles', 'LastNounToVern',
'FunctionRename', 'FunctionRenamePattern', 'DescriptionToVerb',
'OutPath', 'ModuleVersion', 'Culture', 'AdditionalParameters',
'AdditionalParameterDescriptions')
foreach ($key in $ModuleConfig.Keys) {
if ($key -in $convertHelpValidParams) {
$helpParams[$key] = $ModuleConfig[$key]
}
}
& $helpScript @helpParams
if ($LASTEXITCODE -ne 0 -and $LASTEXITCODE -ne $null) {
throw "ConvertToXmlHelp.ps1 failed with exit code $LASTEXITCODE"
}
Write-Host " ✓ XML help generated" -ForegroundColor Green
$results.Steps += @{ Step = 2; Name = "XML Help"; Status = "Success" }
} catch {
Write-Host " ❌ Failed to generate XML help" -ForegroundColor Red
Write-Host " Error: $($_.Exception.Message)" -ForegroundColor Red
$results.Success = $false
$results.Errors += $_.Exception.Message
$results.Steps += @{ Step = 2; Name = "XML Help"; Status = "Failed" }
}
}
# Step 3: Validate Help File
if ($results.Success -and !$SkipValidation) {
try {
Write-Host "`n[ 3/3 ] Validating XML help file..." -ForegroundColor Yellow
Write-Host " Running: Test-XmlHelp.ps1" -ForegroundColor Gray
$testScript = Join-Path $ScriptPath "Test-XmlHelp.ps1"
if (!(Test-Path $testScript)) {
Write-Host " ⚠ Test-XmlHelp.ps1 not found, skipping validation" -ForegroundColor Yellow
} else {
$culture = $ModuleConfig.ContainsKey('Culture') ? $ModuleConfig.Culture : 'en-US'
$outPath = $ModuleConfig.ContainsKey('OutPath') ? $ModuleConfig.OutPath : "$ScriptPath\Output"
$helpFile = "$outPath\$culture\$ProjectName-Help.xml"
if (!(Test-Path $helpFile)) {
throw "Help file not found at $helpFile"
}
$validationResult = & $testScript -HelpFilePath $helpFile
if ($validationResult.Valid) {
Write-Host " ✓ Help file validated successfully" -ForegroundColor Green
Write-Host " Commands: $($validationResult.Commands), Parameters: $($validationResult.Parameters)" -ForegroundColor Gray
$results.Steps += @{ Step = 3; Name = "Validation"; Status = "Success" }
} else {
Write-Host " ⚠ Help file has warnings ($($validationResult.Warnings) warnings)" -ForegroundColor Yellow
$results.Steps += @{ Step = 3; Name = "Validation"; Status = "Warning" }
}
}
} catch {
Write-Host " ⚠ Validation failed: $($_.Exception.Message)" -ForegroundColor Yellow
$results.Steps += @{ Step = 3; Name = "Validation"; Status = "Warning" }
}
} elseif ($SkipValidation) {
Write-Host "`n[ 3/3 ] Validation skipped (-SkipValidation)" -ForegroundColor Gray
$results.Steps += @{ Step = 3; Name = "Validation"; Status = "Skipped" }
}
# Copy to destination if specified
if ($results.Success -and $DestinationPath) {
try {
Write-Host "`n[ + ] Copying files to destination..." -ForegroundColor Yellow
Write-Host " Destination: $DestinationPath" -ForegroundColor Gray
if (!(Test-Path $DestinationPath)) {
New-Item -ItemType Directory -Path $DestinationPath -Force | Out-Null
}
$outputPath = $ModuleConfig.ContainsKey('OutPath') ? $ModuleConfig.OutPath : "$ScriptPath\Output"
$culture = $ModuleConfig.ContainsKey('Culture') ? $ModuleConfig.Culture : 'en-US'
# Copy module files
$filesToCopy = @(
"$outputPath\$ProjectName.ps1"
"$outputPath\$ProjectName.psm1"
"$outputPath\$ProjectName.psd1"
)
$copiedCount = 0
foreach ($file in $filesToCopy) {
if (Test-Path $file) {
Copy-Item $file $DestinationPath -Force
Write-Host " ✓ $(Split-Path $file -Leaf)" -ForegroundColor Green
$copiedCount++
}
}
# Copy help file
$helpFile = "$outputPath\$culture\$ProjectName-Help.xml"
if (Test-Path $helpFile) {
$destHelpDir = Join-Path $DestinationPath $culture
if (!(Test-Path $destHelpDir)) {
New-Item -ItemType Directory -Path $destHelpDir -Force | Out-Null
}
Copy-Item $helpFile $destHelpDir -Force
Write-Host " ✓ $culture\$ProjectName-Help.xml" -ForegroundColor Green
$copiedCount++
}
Write-Host " Total files copied: $copiedCount" -ForegroundColor Cyan
} catch {
Write-Host " ⚠ Error copying files: $($_.Exception.Message)" -ForegroundColor Yellow
}
}
# Summary
Write-Host "`n╔════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ Build Summary ║" -ForegroundColor Cyan
Write-Host "╚════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
foreach ($step in $results.Steps) {
$statusColor = switch ($step.Status) {
"Success" { "Green" }
"Warning" { "Yellow" }
"Failed" { "Red" }
"Skipped" { "Gray" }
}
$icon = switch ($step.Status) {
"Success" { "✓" }
"Warning" { "⚠" }
"Failed" { "❌" }
"Skipped" { "○" }
}
Write-Host "$icon Step $($step.Step): $($step.Name.PadRight(30)) [$($step.Status)]" -ForegroundColor $statusColor
}
if ($results.Success) {
Write-Host "`n✓ Build completed successfully!" -ForegroundColor Green
Write-Host "`nGenerated files:" -ForegroundColor Cyan
$outputPath = $ModuleConfig.ContainsKey('OutPath') ? $ModuleConfig.OutPath : "$ScriptPath\Output"
$culture = $ModuleConfig.ContainsKey('Culture') ? $ModuleConfig.Culture : 'en-US'
Write-Host " • Module code: $outputPath\$ProjectName.ps1" -ForegroundColor Gray
Write-Host " • Module main: $outputPath\$ProjectName.psm1" -ForegroundColor Gray
Write-Host " • Module manif: $outputPath\$ProjectName.psd1" -ForegroundColor Gray
Write-Host " • Help file: $outputPath\$culture\$ProjectName-Help.xml" -ForegroundColor Gray
if ($DestinationPath) {
Write-Host "`nFiles also copied to: $DestinationPath" -ForegroundColor Cyan
}
Write-Host "`nNext steps:" -ForegroundColor Yellow
Write-Host " 1. Import-Module $outputPath\$ProjectName.psd1" -ForegroundColor Gray
Write-Host " 2. Get-Help <CommandName> -Full" -ForegroundColor Gray
} else {
Write-Host "`n❌ Build failed. See errors above." -ForegroundColor Red
exit 1
}