Skip to content

Commit 818daa6

Browse files
author
James Brundage
committed
New-PipeScript: Writing help (Fixes #195) and functions (fixes #196)
1 parent 579406f commit 818daa6

File tree

1 file changed

+77
-4
lines changed

1 file changed

+77
-4
lines changed

New-PipeScript.ps1.ps1

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,35 @@ HTTP Accept indicates what content types the web request will accept as a respon
9797
#>
9898
[Alias('WeakType', 'WeakParameters', 'WeaklyTypedParameters', 'WeakProperties', 'WeaklyTypedProperties')]
9999
[switch]
100-
$WeaklyTyped
100+
$WeaklyTyped,
101+
102+
# The name of the function to create.
103+
[string]
104+
$FunctionName,
105+
106+
# The type of the function to create. This will be ignored if -FunctionName is not passed.
107+
[string]
108+
$FunctionType = 'function',
109+
110+
# A description of the script's functionality. If provided with -Synopsis, will generate help.
111+
[string]
112+
$Description,
113+
114+
# A short synopsis of the script's functionality. If provided with -Description, will generate help.
115+
[string]
116+
$Synopsis,
117+
118+
# A list of examples to use in help. Will be ignored if -Synopsis and -Description are not passed.
119+
[string[]]
120+
$Example,
121+
122+
# A list of links to use in help. Will be ignored if -Synopsis and -Description are not passed.
123+
[string[]]
124+
$Link,
125+
126+
# A list of attributes to declare on the scriptblock.
127+
[string[]]
128+
$Attribute
101129
)
102130

103131
begin {
@@ -121,9 +149,45 @@ HTTP Accept indicates what content types the web request will accept as a respon
121149
$_
122150
}
123151
}
152+
153+
124154
}
125155

126156
process {
157+
if ($Synopsis -and $Description) {
158+
function indentHelpLine {
159+
foreach ($line in $args -split '(?>\r\n|\n)') {
160+
(' ' * 4) + $line.Trim()
161+
}
162+
}
163+
164+
$helpHeader = @(
165+
"<#"
166+
".Synopsis"
167+
indentHelpLine $Synopsis
168+
".Description"
169+
indentHelpLine $Description
170+
171+
foreach ($helpExample in $Example) {
172+
".Example"
173+
indentHelpLine $helpExample
174+
}
175+
foreach ($helplink in $Link) {
176+
".Link"
177+
indentHelpLine $link
178+
}
179+
"#>"
180+
) -join [Environment]::Newline
181+
182+
$allHeaders += $helpHeader
183+
}
184+
185+
if ($Attribute) {
186+
$allHeaders += $Attribute
187+
}
188+
189+
190+
127191
if ($parameter) {
128192
# The -Parameter can be a dictionary of parameters.
129193
if ($Parameter -is [Collections.IDictionary]) {
@@ -201,6 +265,7 @@ HTTP Accept indicates what content types the web request will accept as a respon
201265
$parameter
202266
}
203267
}
268+
# If the -Parameter was provided via reflection
204269
elseif ($parameter -is [Reflection.PropertyInfo] -or
205270
$parameter -as [Reflection.PropertyInfo[]] -or
206271
$parameter -is [Reflection.ParameterInfo] -or
@@ -318,7 +383,7 @@ HTTP Accept indicates what content types the web request will accept as a respon
318383
}
319384

320385
# Create the script block by combining together the provided parts.
321-
$createdScriptBlock = [scriptblock]::Create("
386+
$createdScriptBlock = [scriptblock]::Create("$(if ($functionName) { "$functionType $FunctionName {"})
322387
$($allHeaders -join [Environment]::Newline)
323388
$newParamBlock
324389
$(if ($allDynamicParameters) {
@@ -335,9 +400,17 @@ $(if ($allEndBlocks -and -not $allBeginBlocks -and -not $allProcessBlocks) {
335400
} elseif ($allEndBlocks) {
336401
@(@("end {") + $allEndBlocks + '}') -join [Environment]::Newline
337402
})
403+
$(if ($FunctionName) { '}'})
338404
")
339405

340-
# return the created script block.
341-
return $createdScriptBlock
406+
# If we have a -FunctionName and the -FunctionType is not a built-in function type
407+
if ($CreatedScriptBlock -and
408+
$functionName -and $FunctionType -notin 'function', 'filter') {
409+
# return the transpiled script.
410+
return $createdScriptBlock.Transpile()
411+
} else {
412+
# otherwise, return the created script.
413+
return $createdScriptBlock
414+
}
342415
}
343416
}

0 commit comments

Comments
 (0)