Skip to content

Commit 2403aff

Browse files
committed
Add PSScriptAnalyzerSettings.psd1 file
1 parent 12fe88e commit 2403aff

8 files changed

Lines changed: 81 additions & 38 deletions

File tree

ModuleBuild.build.ps1

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -462,31 +462,25 @@ task UpdateCBH {
462462
task AnalyzeModuleRelease -if {$Script:BuildEnv.OptionAnalyzeCode} {
463463
Write-Description White 'Analyzing the project with ScriptAnalyzer' -accent
464464
$StageReleasePath = Join-Path (Join-Path $BuildRoot $Script:BuildEnv.ScratchFolder) $Script:BuildEnv.BaseReleaseFolder
465-
$Analysis = Invoke-ScriptAnalyzer -Path $StageReleasePath
466-
$AnalysisErrors = @($Analysis | Where-Object {@('Information', 'Warning') -notcontains $_.Severity})
467-
if ($AnalysisErrors.Count -ne 0) {
468-
Write-Build White 'The following errors came up in the script analysis:' -level 2
469-
$AnalysisErrors
470-
Write-Build
471-
Write-Build White "Note that this was from the script analysis run against $StageReleasePath" -Level 2
472-
Prompt-ForBuildBreak -CustomError $AnalysisErrors
465+
$Analysis = Invoke-ScriptAnalyzer -Path $StageReleasePath -Settings (Join-Path $BuildRoot "PSScriptAnalyzerSettings.psd1")
466+
if ($Analysis.Count) {
467+
Write-Description White "Note that this was from the script analysis run against $StageReleasePath" -level 2
468+
Write-Description Red "$($Analysis.Count) linting errors or warnings were found:" -level 2
469+
$Analysis | Format-Table -AutoSize
470+
Write-Error "$($Analysis.Count) linting errors or warnings were found. The build cannot continue." -ErrorAction Stop
473471
}
474472
}
475473

476474
# Synopsis: Run PSScriptAnalyzer against the public source files.
477475
task AnalyzePublic {
478-
Write-Description White 'Analyzing the public source files with ScriptAnalyzer.' -accent
479-
$Analysis = Invoke-ScriptAnalyzer -Path (Join-Path $BuildRoot $Script:BuildEnv.PublicFunctionSource)
480-
$AnalysisErrors = @($Analysis | Where-Object {@('Information', 'Warning') -notcontains $_.Severity})
481-
482-
if ($AnalysisErrors.Count -ne 0) {
483-
Write-Description White 'The following errors came up in the script analysis:' -level 2
484-
$AnalysisErrors
485-
Write-Description
476+
Write-Description White "Analyzing the public source files with ScriptAnalyzer." -accent
477+
$Analysis = Invoke-ScriptAnalyzer -Path (Join-Path $BuildRoot $Script:BuildEnv.PublicFunctionSource) -Settings (Join-Path $BuildRoot "PSScriptAnalyzerSettings.psd1")
478+
if ($Analysis.Count) {
486479
Write-Description White "Note that this was from the script analysis run against $($Script:BuildEnv.PublicFunctionSource)" -level 2
480+
Write-Description Red "$($Analysis.Count) linting errors or warnings were found:" -level 2
481+
$Analysis | Format-Table -AutoSize
487482
}
488483
}
489-
490484
# Synopsis: Build help files for module
491485
task CreateHelp CreateMarkdownHelp, CreateExternalHelp, CreateUpdateableHelpCAB, CreateProjectHelp, AddAdditionalDocFiles
492486

PSScriptAnalyzerSettings.psd1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@{
2+
IncludeRules=@('PSProvideCommentHelp',
3+
'PSAvoidUsingWriteHost')
4+
}

plaster/ModuleBuild/plasterManifest.xml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
templateType="Project" xmlns="http://www.microsoft.com/schemas/PowerShell/Plaster/v1">
55
<metadata>
66
<name>ModuleBuild</name>
7-
<id>c0ce518c-f894-4114-91f9-d49945d9dfb1</id>
7+
<id>4b283342-b625-4db2-99c7-cf85de213289</id>
88
<version>0.0.1</version>
99
<title>New ModuleBuild Project</title>
1010
<description>Create a new PowerShell Module with a ModuleBuild wrapper</description>
@@ -194,7 +194,19 @@
194194
condition="$PLASTER_PARAM_PluginModuleLogging -eq &quot;True&quot;" />
195195
<file
196196
source="scaffold\plugins\NLog\NLogModule\*"
197-
destination="plugins\nlog\NLog\NLogModule"
197+
destination="plugins\nlog\NLogModule"
198+
condition="$PLASTER_PARAM_PluginModuleLogging -eq &quot;True&quot;" />
199+
<file
200+
source="scaffold\plugins\NLog\NLogModule\docs\*"
201+
destination="plugins\nlog\NLogModule\docs"
202+
condition="$PLASTER_PARAM_PluginModuleLogging -eq &quot;True&quot;" />
203+
<file
204+
source="scaffold\plugins\NLog\NLogModule\en-US\*"
205+
destination="plugins\nlog\NLogModule\en-US"
206+
condition="$PLASTER_PARAM_PluginModuleLogging -eq &quot;True&quot;" />
207+
<file
208+
source="scaffold\plugins\NLog\NLogModule\lib\*"
209+
destination="plugins\nlog\NLogModule\lib"
198210
condition="$PLASTER_PARAM_PluginModuleLogging -eq &quot;True&quot;" />
199211
<file
200212
source="scaffold\build\cleanup\*"
@@ -257,5 +269,8 @@
257269
<templateFile
258270
source="scaffold\build\docs\ReadTheDocs\*"
259271
destination="build\docs\ReadTheDocs" />
272+
<file
273+
source="scaffold\PSScriptAnalyzerSettings.psd1"
274+
destination="PSScriptAnalyzerSettings.psd1" />
260275
</content>
261276
</plasterManifest>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@{
2+
IncludeRules=@('PSUseApprovedVerbs',
3+
'PSReservedCmdletChar',
4+
'PSReservedParams',
5+
'PSShouldProcess',
6+
'PSUseShouldProcessForStateChangingFunctions',
7+
'PSUseSingularNouns',
8+
'PSMissingModuleManifestField',
9+
'PSAvoidDefaultValueSwitchParameter',
10+
'PSAvoidUsingCmdletAliases',
11+
'PSAvoidUsingWMICmdlet',
12+
'PSAvoidUsingEmptyCatchBlock',
13+
'PSUseCmdletCorrectly',
14+
'PSUseShouldProcessForStateChangingFunctions',
15+
'PSAvoidUsingPositionalParameters',
16+
'PSAvoidGlobalVars',
17+
'PSUseDeclaredVarsMoreThanAssignments',
18+
'PSAvoidUsingInvokeExpression',
19+
'PSAvoidUsingPlainTextForPassword',
20+
'PSAvoidUsingComputerNameHardcoded',
21+
'PSAvoidUsingConvertToSecureStringWithPlainText',
22+
'PSUsePSCredentialType',
23+
'PSAvoidUsingUserNameAndPasswordParams',
24+
'PSDSC*'
25+
)
26+
}

plaster/ModuleBuild/scaffold/modulename.build.template

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -465,28 +465,23 @@ task UpdateCBH {
465465
task AnalyzeModuleRelease -if {$Script:BuildEnv.OptionAnalyzeCode} {
466466
Write-Description White 'Analyzing the project with ScriptAnalyzer' -accent
467467
$StageReleasePath = Join-Path (Join-Path $BuildRoot $Script:BuildEnv.ScratchFolder) $Script:BuildEnv.BaseReleaseFolder
468-
$Analysis = Invoke-ScriptAnalyzer -Path $StageReleasePath
469-
$AnalysisErrors = @($Analysis | Where-Object {@('Information', 'Warning') -notcontains $_.Severity})
470-
if ($AnalysisErrors.Count -ne 0) {
471-
Write-Build White 'The following errors came up in the script analysis:' -level 2
472-
$AnalysisErrors
473-
Write-Build
474-
Write-Build White "Note that this was from the script analysis run against $StageReleasePath" -Level 2
475-
Prompt-ForBuildBreak -CustomError $AnalysisErrors
468+
$Analysis = Invoke-ScriptAnalyzer -Path $StageReleasePath -Settings (Join-Path $BuildRoot "PSScriptAnalyzerSettings.psd1")
469+
if ($Analysis.Count) {
470+
Write-Description White "Note that this was from the script analysis run against $StageReleasePath" -level 2
471+
Write-Description Red "$($Analysis.Count) linting errors or warnings were found:" -level 2
472+
$Analysis | Format-Table -AutoSize
473+
Write-Error "$($Analysis.Count) linting errors or warnings were found. The build cannot continue." -ErrorAction Stop
476474
}
477475
}
478476

479477
# Synopsis: Run PSScriptAnalyzer against the public source files.
480478
task AnalyzePublic {
481-
Write-Description White 'Analyzing the public source files with ScriptAnalyzer.' -accent
482-
$Analysis = Invoke-ScriptAnalyzer -Path (Join-Path $BuildRoot $Script:BuildEnv.PublicFunctionSource)
483-
$AnalysisErrors = @($Analysis | Where-Object {@('Information', 'Warning') -notcontains $_.Severity})
484-
485-
if ($AnalysisErrors.Count -ne 0) {
486-
Write-Description White 'The following errors came up in the script analysis:' -level 2
487-
$AnalysisErrors
488-
Write-Description
479+
Write-Description White "Analyzing the public source files with ScriptAnalyzer." -accent
480+
$Analysis = Invoke-ScriptAnalyzer -Path (Join-Path $BuildRoot $Script:BuildEnv.PublicFunctionSource) -Settings (Join-Path $BuildRoot "PSScriptAnalyzerSettings.psd1")
481+
if ($Analysis.Count) {
489482
Write-Description White "Note that this was from the script analysis run against $($Script:BuildEnv.PublicFunctionSource)" -level 2
483+
Write-Description Red "$($Analysis.Count) linting errors or warnings were found:" -level 2
484+
$Analysis | Format-Table -AutoSize
490485
}
491486
}
492487

@@ -883,13 +878,13 @@ task GithubPush VersionCheck, {
883878
task . Configure, CodeHealthReport, Clean, PrepareStage, GetPublicFunctions, SanitizeCode, CreateHelp, CreateModulePSM1, CreateModuleManifest, AnalyzeModuleRelease, PushVersionRelease, PushCurrentRelease, CreateProjectHelp, PostBuildTasks, BuildSessionCleanup
884879

885880
# Synopsis: Install and test load the module.
886-
task InstallAndTestModule LoadBuildTools, InstallModule, TestInstalledModule
881+
task InstallAndTestModule InstallModule, TestInstalledModule
887882

888883
# Synopsis: Build, Install, and Test the module
889884
task BuildInstallAndTestModule Configure, CodeHealthReport, Clean, PrepareStage, GetPublicFunctions, SanitizeCode, CreateHelp, CreateModulePSM1, CreateModuleManifest, AnalyzeModuleRelease, PushVersionRelease, PushCurrentRelease, CreateProjectHelp, InstallModule, TestInstalledModule, PostBuildTasks, BuildSessionCleanup
890885

891886
# Synopsis: Build, Install, Test, and Publish the module
892887
task BuildInstallTestAndPublishModule Configure, CodeHealthReport, Clean, PrepareStage, GetPublicFunctions, SanitizeCode, CreateHelp, CreateModulePSM1, CreateModuleManifest, AnalyzeModuleRelease, PushVersionRelease, PushCurrentRelease, CreateProjectHelp, InstallModule, TestInstalledModule, PublishPSGallery, PostBuildTasks, BuildSessionCleanup
893888

894-
# Synopsis: Insert Comment Based Help where it doesn't already exist (output to scratch directory)
889+
# Synopsis: Instert Comment Based Help where it doesn't already exist (output to scratch directory)
895890
task InsertMissingCBH Configure, Clean, UpdateCBHtoScratch, BuildSessionCleanup

plaster/ModuleBuild/scaffold/src/other/PostLoad.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ $null = Register-EngineEvent -SourceIdentifier ( [System.Management.Automation.P
5353
}
5454

5555
# Use this in your scripts to check if the function is being called from your module or independantly.
56+
# Call it immediately to avoid PSScriptAnalyzer 'PSUseDeclaredVarsMoreThanAssignments'
5657
$ThisModuleLoaded = $true
58+
$ThisModuleLoaded
5759

5860
# Non-function exported public module members might go here.
5961
#Export-ModuleMember -Variable SomeVariable -Function *

plaster/PlasterContent.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $Content = @(
2626
ContentType = 'file'
2727
Source = 'scaffold\gitattributes'
2828
Destination = '.gitattributes'
29-
},
29+
},
3030
@{
3131
ContentType = 'templateFile'
3232
Source = 'scaffold\vscode\*'
@@ -190,5 +190,10 @@ $Content = @(
190190
ContentType = 'templateFile'
191191
Source = 'scaffold\build\docs\ReadTheDocs\*'
192192
Destination = 'build\docs\ReadTheDocs'
193+
},
194+
@{
195+
ContentType = 'file'
196+
Source = 'scaffold\PSScriptAnalyzerSettings.psd1'
197+
Destination = 'PSScriptAnalyzerSettings.psd1'
193198
}
194199
)

src/other/PostLoad.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ $null = Register-EngineEvent -SourceIdentifier ( [System.Management.Automation.P
5454
}
5555

5656
# Use this in your scripts to check if the function is being called from your module or independantly.
57+
# Call it immediately to avoid PSScriptAnalyzer 'PSUseDeclaredVarsMoreThanAssignments'
5758
$ThisModuleLoaded = $true
59+
$ThisModuleLoaded
5860

5961
# Non-function exported public module members might go here.
6062
#Export-ModuleMember -Variable SomeVariable -Function *

0 commit comments

Comments
 (0)