Skip to content

Commit 8680d63

Browse files
committed
Improve build script robustness by validating module imports and safely handling missing dependencies.
This update enhances the module build script to ensure all required modules are explicitly imported and verified before use. The script now: Imports the target module with proper error handling Attempts to import PSScriptAnalyzer, Pester, and PlatyPS only when needed Skips analyzer, test, or documentation steps if their respective modules are unavailable Prevents calls to Invoke-ScriptAnalyzer, Invoke-Pester, or PlatyPS commands unless the module successfully loaded Provides clear informational and warning messages for missing dependencies Improves reliability across fresh environments, CI runners, and developer machines These changes make the build pipeline more predictable, resilient, and self‑diagnosing, reducing friction during development and module publishing.
1 parent c485260 commit 8680d63

2 files changed

Lines changed: 63 additions & 40 deletions

File tree

‎Docs/New-ModuleTemplate.md‎

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Creates a full or minimal PowerShell module project scaffold.
1414

1515
```
1616
New-ModuleTemplate [-Path] <String> [-Name] <String> [-Minimal] [-Force] [-NoGit] [-NoTests] [-NoDocs]
17-
[-NoAnalyzer] [-NoScripts] [-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm] [<CommonParameters>]
17+
[-NoAnalyzer] [-NoScripts] [-ProgressAction <ActionPreference>] [<CommonParameters>]
1818
```
1919

2020
## DESCRIPTION
@@ -193,37 +193,6 @@ Accept pipeline input: False
193193
Accept wildcard characters: False
194194
```
195195
196-
### -WhatIf
197-
Shows what would happen if the cmdlet runs.
198-
The cmdlet is not run.
199-
200-
```yaml
201-
Type: SwitchParameter
202-
Parameter Sets: (All)
203-
Aliases: wi
204-
205-
Required: False
206-
Position: Named
207-
Default value: None
208-
Accept pipeline input: False
209-
Accept wildcard characters: False
210-
```
211-
212-
### -Confirm
213-
Prompts you for confirmation before running the cmdlet.
214-
215-
```yaml
216-
Type: SwitchParameter
217-
Parameter Sets: (All)
218-
Aliases: cf
219-
220-
Required: False
221-
Position: Named
222-
Default value: None
223-
Accept pipeline input: False
224-
Accept wildcard characters: False
225-
```
226-
227196
### -ProgressAction
228197
{{ Fill ProgressAction Description }}
229198

‎Scripts/build.ps1‎

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,83 @@ param(
33
[switch]$SkipAnalyzer,
44
[switch]$SkipDocs
55
)
6+
67
$InformationPreference = 'Continue'
78

89
Write-Information "Building NewModuleTemplate..."
9-
Import-Module -Name "$PSScriptRoot\..\" -Force
1010

11+
# Import the module being built
12+
try {
13+
Import-Module -Name "$PSScriptRoot\..\" -Force -ErrorAction Stop
14+
Write-Information "Module imported successfully."
15+
}
16+
catch {
17+
Write-Error "Failed to import module from $PSScriptRoot\..\. Build aborted."
18+
exit 1
19+
}
20+
21+
# ---------------------------
22+
# Analyzer
23+
# ---------------------------
1124
if (-not $SkipAnalyzer) {
1225
Write-Information "Running PSScriptAnalyzer..."
13-
Invoke-ScriptAnalyzer -Path $PSScriptRoot -Settings "$PSScriptRoot\..\Analyzer\PSScriptAnalyzerSettings.psd1"
26+
27+
try {
28+
Import-Module PSScriptAnalyzer -ErrorAction Stop
29+
}
30+
catch {
31+
Write-Warning "PSScriptAnalyzer is not installed. Skipping analyzer step."
32+
$SkipAnalyzer = $true
33+
}
34+
35+
if (-not $SkipAnalyzer) {
36+
Invoke-ScriptAnalyzer -Path $PSScriptRoot `
37+
-Settings "$PSScriptRoot\..\Analyzer\PSScriptAnalyzerSettings.psd1"
38+
}
1439
}
1540

41+
# ---------------------------
42+
# Tests
43+
# ---------------------------
1644
if (-not $SkipTests) {
1745
Write-Information "Running Pester tests..."
18-
Invoke-Pester -Path "$PSScriptRoot\..\Tests"
19-
if ($LASTEXITCODE -ne 0) {
20-
Write-Error "Pester tests failed."
21-
exit $LASTEXITCODE
46+
47+
try {
48+
Import-Module Pester -ErrorAction Stop
49+
}
50+
catch {
51+
Write-Warning "Pester is not installed. Skipping tests."
52+
$SkipTests = $true
53+
}
54+
55+
if (-not $SkipTests) {
56+
Invoke-Pester -Path "$PSScriptRoot\..\Tests"
57+
if ($LASTEXITCODE -ne 0) {
58+
Write-Error "Pester tests failed."
59+
exit $LASTEXITCODE
60+
}
2261
}
2362
}
2463

64+
# ---------------------------
65+
# Documentation
66+
# ---------------------------
2567
if (-not $SkipDocs) {
2668
Write-Information "Updating PlatyPS docs..."
27-
Import-Module PlatyPS -ErrorAction Stop
28-
New-MarkdownHelp -Module NewModuleTemplate -OutputFolder "$PSScriptRoot\..\Docs\" -WithModulePage -Force
69+
70+
try {
71+
Import-Module PlatyPS -ErrorAction Stop
72+
}
73+
catch {
74+
Write-Warning "PlatyPS is not installed. Skipping documentation generation."
75+
$SkipDocs = $true
76+
}
77+
78+
if (-not $SkipDocs) {
79+
New-MarkdownHelp -Module NewModuleTemplate `
80+
-OutputFolder "$PSScriptRoot\..\Docs\" `
81+
-WithModulePage -Force
82+
}
2983
}
3084

3185
Write-Information "Build complete."

0 commit comments

Comments
 (0)