Skip to content

Commit e9fbae4

Browse files
committed
Fix for AddCBH when no params are added to function. New displays verbose messages and throws error pointing to CBH.
1 parent 56eb0a9 commit e9fbae4

3 files changed

Lines changed: 56 additions & 43 deletions

File tree

plaster/ModuleBuild/scaffold/modulename.build.template

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -296,19 +296,21 @@ task UpdateCBHtoScratch {
296296
$CBH = $currscript | New-CommentBasedHelp
297297
$currscriptblock = [scriptblock]::Create($currscript)
298298
. $currscriptblock
299-
$currfunct = get-command $CBH.FunctionName
300-
301-
302-
if ($currfunct.definition -notmatch $CBHPattern) {
303-
$CBHUpdates++
304-
Write-Description White "Inserting template CBH and writing to : $($Script:BuildEnv.ScratchFolder)\$($Script:BuildEnv.PublicFunctionSource)\$($FileName)" -Level 3
305-
$UpdatedFunct = 'Function ' + $currfunct.Name + ' {' + "`r`n" + $CBH.CBH + "`r`n" + $currfunct.definition + "`r`n" + '}'
306-
$UpdatedFunct | Out-File "$($ScratchPath)\$($Script:BuildEnv.PublicFunctionSource)\$($FileName)" -Encoding $Script:BuildEnv.Encoding -force
307-
}
308-
else {
309-
Write-Description Yellow 'Comment based help already exists!' -Level 2
299+
if([string]::IsNullOrEmpty($CBH))
300+
{
301+
Write-Error "Could not Add CBH, possibly duo having no parameters in $filename" -ErrorAction Stop
302+
} else {
303+
$currfunct = get-command $CBH.FunctionName
304+
if ($currfunct.definition -notmatch $CBHPattern) {
305+
$CBHUpdates++
306+
Write-Description White "Inserting template CBH and writing to : $($Script:BuildEnv.ScratchFolder)\$($Script:BuildEnv.PublicFunctionSource)\$($FileName)" -Level 3
307+
$UpdatedFunct = 'Function ' + $currfunct.Name + ' {' + "`r`n" + $CBH.CBH + "`r`n" + $currfunct.definition + "`r`n" + '}'
308+
$UpdatedFunct | Out-File "$($ScratchPath)\$($Script:BuildEnv.PublicFunctionSource)\$($FileName)" -Encoding $Script:BuildEnv.Encoding -force
309+
}
310+
else {
311+
Write-Description Yellow 'Comment based help already exists!' -Level 2
312+
}
310313
}
311-
312314
Remove-Item Function:\$($currfunct.Name)
313315
}
314316
Write-Build White ''

src/private/Get-FunctionParameter.ps1

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,28 @@ function Get-FunctionParameter {
8383
Write-Verbose "$($FunctionName): Not including embedded functions."
8484
$functions = $functions | Where-Object {-not $_.IsEmbedded}
8585
}
86-
87-
Foreach ($f in $functions) {
88-
$function = $f.ast
89-
$Parameters = $function.FindAll($parampredicate, $true)
90-
foreach ($p in $Parameters) {
91-
$ParamType = $p.FindAll($typepredicate, $true)
92-
Write-Verbose "$($FunctionName): Processing Parameter of type [$($ParamType.typeName.FullName)] - $($p.Name.VariablePath.ToString())"
93-
$OutProps = @{
94-
'FunctionName' = $function.Name.ToString()
95-
'ParameterName' = $p.Name.VariablePath.ToString()
96-
'ParameterType' = $ParamType[0].typeName.FullName
97-
}
98-
# This will add in any other parameter attributes if they are specified (default attributes are thus not included and output may not be normalized)
99-
$p.FindAll($paramattributes, $true) | ForEach-Object {
100-
$OutProps.($_.ArgumentName) = $_.Argument.Value
86+
If([string]::IsNullOrEmpty($functions))
87+
{
88+
Write-Verbose "$($FunctionName): There were no script parameters found"
89+
} else
90+
{
91+
Foreach ($f in $functions) {
92+
$function = $f.ast
93+
$Parameters = $function.FindAll($parampredicate, $true)
94+
foreach ($p in $Parameters) {
95+
$ParamType = $p.FindAll($typepredicate, $true)
96+
Write-Verbose "$($FunctionName): Processing Parameter of type [$($ParamType.typeName.FullName)] - $($p.Name.VariablePath.ToString())"
97+
$OutProps = @{
98+
'FunctionName' = $function.Name.ToString()
99+
'ParameterName' = $p.Name.VariablePath.ToString()
100+
'ParameterType' = $ParamType[0].typeName.FullName
101+
}
102+
# This will add in any other parameter attributes if they are specified (default attributes are thus not included and output may not be normalized)
103+
$p.FindAll($paramattributes, $true) | ForEach-Object {
104+
$OutProps.($_.ArgumentName) = $_.Argument.Value
105+
}
106+
$Output += New-Object -TypeName PSObject -Property $OutProps
101107
}
102-
$Output += New-Object -TypeName PSObject -Property $OutProps
103108
}
104109
}
105110
}

src/private/New-CommentBasedHelp.ps1

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
1.0.0 - Initial release
3131
1.0.1 - Updated for ModuleBuild
3232
1.0.2 - Added SuppressMessageAttribute
33+
1.0.3 - Extra Verbose message to check if function had Params
3334
#>
3435
[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.")]
3536
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions","",Scope="Function",Target="New-CommentBasedHelp",Justification="Function does not change system state. Simply outputs a obj with CommentBasedHelp.")]
@@ -76,26 +77,31 @@
7677
}
7778
$AllParams = Get-FunctionParameter @FuncParams -Code $Codeblock | Sort-Object -Property FunctionName
7879
$AllFunctions = @($AllParams.FunctionName | Select-Object -unique)
80+
If([string]::IsNullOrEmpty($AllFunctions))
81+
{
82+
Write-Verbose "$($FunctionName): Found no Params in function."
83+
} else
84+
{
85+
foreach ($f in $AllFunctions) {
86+
$OutCBH = @{}
87+
$OutCBH.FunctionName = $f
88+
[string]$OutParams = ''
89+
$fparams = @($AllParams | Where-Object {$_.FunctionName -eq $f} | Sort-Object -Property Position)
90+
if ($fparams.count -gt 0) {
91+
$fparams | ForEach-Object {
92+
$ParamHelpMessage = if ([string]::IsNullOrEmpty($_.HelpMessage)) { $_.ParameterName + " explanation`n`r`n`r"} else {$_.HelpMessage + "`n`r`n`r"}
7993

80-
foreach ($f in $AllFunctions) {
81-
$OutCBH = @{}
82-
$OutCBH.FunctionName = $f
83-
[string]$OutParams = ''
84-
$fparams = @($AllParams | Where-Object {$_.FunctionName -eq $f} | Sort-Object -Property Position)
85-
if ($fparams.count -gt 0) {
86-
$fparams | ForEach-Object {
87-
$ParamHelpMessage = if ([string]::IsNullOrEmpty($_.HelpMessage)) { $_.ParameterName + " explanation`n`r`n`r"} else {$_.HelpMessage + "`n`r`n`r"}
88-
89-
$OutParams += $CBH_PARAM -replace '%%PARAM%%',$_.ParameterName -replace '%%PARAMHELP%%',$ParamHelpMessage
94+
$OutParams += $CBH_PARAM -replace '%%PARAM%%',$_.ParameterName -replace '%%PARAMHELP%%',$ParamHelpMessage
95+
}
9096
}
91-
}
92-
else {
97+
else {
9398

94-
}
99+
}
95100

96-
$OutCBH.'CBH' = $CBHTemplate -replace '%%PARAMETER%%',$OutParams
101+
$OutCBH.'CBH' = $CBHTemplate -replace '%%PARAMETER%%',$OutParams
97102

98-
New-Object PSObject -Property $OutCBH
103+
New-Object PSObject -Property $OutCBH
104+
}
99105
}
100106

101107
Write-Verbose "$($FunctionName): End."

0 commit comments

Comments
 (0)