Skip to content

Commit 56eb0a9

Browse files
committed
Script Analyzer fixes
1 parent 9498936 commit 56eb0a9

8 files changed

Lines changed: 28 additions & 20 deletions

src/other/PreLoad.ps1

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "ModuleBuildLoggingEnabled")]
2-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "BuildPath")]
3-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "functionpredicate")]
4-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "CommonParams")]
5-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "ScriptText")]
1+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "ModuleBuildLoggingEnabled",Justification="ModuleBuildLoggingEnabled is optional. It's used to enable Nlog-based logging.")]
62
param()
73

8-
$ModuleBuildLoggingEnabled = $false # Set to true to enable nlog-based logging
4+
$ModuleBuildLoggingEnabled = $false # Set to true to enable nlog-based logging
5+

src/private/Get-BuildFilePath.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
function Get-BuildFilePath {
22
$BuildPath = (Get-ChildItem -File -Filter "*.buildenvironment.json" -Path '.\','..\','.\build\' -ErrorAction:SilentlyContinue | Select-Object -First 1).FullName
3+
Return $BuildPath
34
}

src/private/Get-CommonParameters.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ function Get-CommonParameter {
33
# advanced function
44
function somefunct {
55
[CmdletBinding(SupportsShouldProcess = $true, SupportsPaging = $true, SupportsTransactions = $true)]
6+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Scope="Function", Target="somefunct",Justification="This is a dummy function in order to get all CommonParameters without hardcoding them. There is no reason for it to actually use SupportsShouldProcess.")]
67
param()
78
}
89

src/private/Get-FunctionParameter.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ function Get-FunctionParameter {
3030
1.0.1 - Updated function name to remove plural format
3131
Added Name parameter and logic for getting script parameters if no function is defined.
3232
Added ScriptParameters parameter to include parameters for a script (not just ones associated with defined functions)
33+
1.0.2 - Added SuppressMessageAttribute for functionpredicate
3334
#>
35+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments","functionpredicate",Scope="Function",Target='Get-FunctionParameter',Justification="Unused AST filter")]
36+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments","CommonParams",Scope="Function",Target='Get-FunctionParameter',Justification="")]
3437
[CmdletBinding()]
3538
param(
3639
[parameter(ValueFromPipeline=$true, HelpMessage='Lines of code to process.')]

src/private/New-CommentBasedHelp.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
Version History
3030
1.0.0 - Initial release
3131
1.0.1 - Updated for ModuleBuild
32+
1.0.2 - Added SuppressMessageAttribute
3233
#>
34+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "ScriptText",Scope="Function",Target="New-CommentBasedHelp",Justification="Seems it's here since duo a copy paste from other functions (Add-MissingCBH,Get-Function,Get-FunctionParameter). Leaving it here since it doesn't do any harm.")]
35+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions","",Scope="Function",Target="New-CommentBasedHelp",Justification="Function does not change system state. Simply outputs a obj with CommentBasedHelp.")]
3336
[CmdletBinding()]
3437
param(
3538
[parameter(Position=0, ValueFromPipeline=$true, HelpMessage='Lines of code to process.')]

src/private/New-DynamicParameter.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ function New-DynamicParameter {
415415
}
416416
}
417417
#>
418+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions","",Scope="Function",Target="New-DynamicParameter",Justification="Function does not change system state.")]
418419
[CmdletBinding(PositionalBinding = $false, DefaultParameterSetName = 'DynamicParameter')]
419420
Param
420421
(

src/public/Import-ModulePrivateFunction.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function Import-ModulePrivateFunction {
3535
.NOTES
3636
This only applies to modules of the type 'Script'. Be very careful before importing everything as any wayward functions might get imported and bloat your resulting module needlessly.
3737
#>
38-
38+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidDefaultValueSwitchParameter", "",Scope="function",Target="Import-ModulePrivateFunction",Justification="Not adding CBH by default is actually useful.")]
3939
[CmdletBinding( SupportsShouldProcess = $True, ConfirmImpact = 'High' )]
4040
param(
4141
[parameter(Position = 0, ValueFromPipeline = $TRUE)]

src/public/Set-BuildEnvironment.ps1

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function Set-BuildEnvironment {
1717
Set-BuildEnvironment -OptionSensitiveTerms @('myapikey','myname','password')
1818
#>
1919

20-
[CmdletBinding()]
20+
[CmdletBinding(SupportsShouldProcess=$True)]
2121
param(
2222
[parameter(Position = 0, ValueFromPipeline = $TRUE)]
2323
[String]$Path
@@ -50,7 +50,7 @@ function Set-BuildEnvironment {
5050
}
5151
}
5252
catch {
53-
#throw "Unable to load the build file in $BuildPath"
53+
throw "Unable to load the build file in $BuildPath"
5454
}
5555
}
5656

@@ -75,18 +75,20 @@ function Set-BuildEnvironment {
7575
New-DynamicParameter -CreateVariables -BoundParameters $PSBoundParameters
7676

7777
if ((Test-Path $BuildPath) -and ($BuildPath -like "*.buildenvironment.json")) {
78-
try {
79-
$LoadedBuildEnv = Get-BuildEnvironment -Path $BuildPath
80-
Foreach ($ParamKey in ($PSBoundParameters.Keys | Where-Object {$_ -ne 'Path'})) {
81-
$LoadedBuildEnv.$ParamKey = $PSBoundParameters[$ParamKey]
82-
Write-Output "Updating $ParamKey to be $($PSBoundParameters[$ParamKey])"
78+
If ($PSCmdlet.ShouldProcess("Updating buildenvironment.json")) {
79+
try {
80+
$LoadedBuildEnv = Get-BuildEnvironment -Path $BuildPath
81+
Foreach ($ParamKey in ($PSBoundParameters.Keys | Where-Object {$_ -ne 'Path'})) {
82+
$LoadedBuildEnv.$ParamKey = $PSBoundParameters[$ParamKey]
83+
Write-Output "Updating $ParamKey to be $($PSBoundParameters[$ParamKey])"
84+
}
85+
$LoadedBuildEnv.PSObject.Properties.remove('Path')
86+
$LoadedBuildEnv | ConvertTo-Json | Out-File -FilePath $BuildPath -Encoding:utf8 -Force
87+
Write-Output "Saved configuration file - $BuildPath"
88+
}
89+
catch {
90+
throw "Unable to load the build file in $BuildPath"
8391
}
84-
$LoadedBuildEnv.PSObject.Properties.remove('Path')
85-
$LoadedBuildEnv | ConvertTo-Json | Out-File -FilePath $BuildPath -Encoding:utf8 -Force
86-
Write-Output "Saved configuration file - $BuildPath"
87-
}
88-
catch {
89-
throw "Unable to load the build file in $BuildPath"
9092
}
9193
}
9294
else {

0 commit comments

Comments
 (0)