Skip to content

Commit 36fd191

Browse files
StartAutomatingStartAutomating
authored andcommitted
New-PipeScript: Adding inline documentation.
1 parent 6cd68c9 commit 36fd191

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

New-PipeScript.ps1

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,28 @@ HTTP Accept indicates what content types the web request will accept as a respon
152152
#>
153153
[Alias('WeakType', 'WeakParameters', 'WeaklyTypedParameters', 'WeakProperties', 'WeaklyTypedProperties')]
154154
[switch]
155-
$WeaklyTyped
155+
$WeaklyTyped,
156+
# The name of the function to create.
157+
[string]
158+
$FunctionName,
159+
# The type of the function to create. This will be ignored if -FunctionName is not passed.
160+
[string]
161+
$FunctionType = 'function',
162+
# A description of the script's functionality. If provided with -Synopsis, will generate help.
163+
[string]
164+
$Description,
165+
# A short synopsis of the script's functionality. If provided with -Description, will generate help.
166+
[string]
167+
$Synopsis,
168+
# A list of examples to use in help. Will be ignored if -Synopsis and -Description are not passed.
169+
[string[]]
170+
$Example,
171+
# A list of links to use in help. Will be ignored if -Synopsis and -Description are not passed.
172+
[string[]]
173+
$Link,
174+
# A list of attributes to declare on the scriptblock.
175+
[string[]]
176+
$Attribute
156177
)
157178
begin {
158179
$ParametersToCreate = [Ordered]@{}
@@ -177,6 +198,36 @@ HTTP Accept indicates what content types the web request will accept as a respon
177198
}
178199
}
179200
process {
201+
if ($Synopsis -and $Description) {
202+
function indentHelpLine {
203+
foreach ($line in $args -split '(?>\r\n|\n)') {
204+
(' ' * 4) + $line.Trim()
205+
}
206+
207+
}
208+
$helpHeader = @(
209+
"<#"
210+
".Synopsis"
211+
indentHelpLine $Synopsis
212+
".Description"
213+
indentHelpLine $Description
214+
215+
foreach ($helpExample in $Example) {
216+
".Example"
217+
indentHelpLine $helpExample
218+
}
219+
foreach ($helplink in $Link) {
220+
".Link"
221+
indentHelpLine $link
222+
}
223+
"#>"
224+
) -join [Environment]::Newline
225+
$allHeaders += $helpHeader
226+
}
227+
228+
if ($Attribute) {
229+
$allHeaders += $Attribute
230+
}
180231
if ($parameter) {
181232
# The -Parameter can be a dictionary of parameters.
182233
if ($Parameter -is [Collections.IDictionary]) {
@@ -252,6 +303,7 @@ HTTP Accept indicates what content types the web request will accept as a respon
252303
$parameter
253304
}
254305
}
306+
# If the -Parameter was provided via reflection
255307
elseif ($parameter -is [Reflection.PropertyInfo] -or
256308
$parameter -as [Reflection.PropertyInfo[]] -or
257309
$parameter -is [Reflection.ParameterInfo] -or
@@ -324,15 +376,21 @@ HTTP Accept indicates what content types the web request will accept as a respon
324376
# accumulate them.
325377
$allEndBlocks += $end
326378
}
379+
# If -AutoParameter was passed
327380
if ($AutoParameter) {
381+
# Find all of the variable expressions within -Begin, -Process, and -End
328382
$variableDefinitions = $Begin, $Process, $End |
329383
Where-Object { $_ } |
330384
Search-PipeScript -AstType VariableExpressionAST |
331385
Select-Object -ExpandProperty Result
332386
foreach ($var in $variableDefinitions) {
387+
# Then, see where those variables were assigned
333388
$assigned = $var.GetAssignments()
389+
# (if they were assigned, keep moving)
334390
if ($assigned) { continue }
391+
# If there were not assigned
335392
$varName = $var.VariablePath.userPath.ToString()
393+
# add it to the list of parameters to create.
336394
$ParametersToCreate[$varName] = @(
337395
@(
338396
"[Parameter(ValueFromPipelineByPropertyName)]"
@@ -359,7 +417,7 @@ HTTP Accept indicates what content types the web request will accept as a respon
359417
$newParamBlock = $parameterScriptBlocks | Join-PipeScript
360418
}
361419
# Create the script block by combining together the provided parts.
362-
$createdScriptBlock = [scriptblock]::Create("
420+
$createdScriptBlock = [scriptblock]::Create("$(if ($functionName) { "$functionType $FunctionName {"})
363421
$($allHeaders -join [Environment]::Newline)
364422
$newParamBlock
365423
$(if ($allDynamicParameters) {
@@ -376,9 +434,17 @@ $(if ($allEndBlocks -and -not $allBeginBlocks -and -not $allProcessBlocks) {
376434
} elseif ($allEndBlocks) {
377435
@(@("end {") + $allEndBlocks + '}') -join [Environment]::Newline
378436
})
437+
$(if ($FunctionName) { '}'})
379438
")
380-
# return the created script block.
381-
return $createdScriptBlock
439+
# If we have a -FunctionName and the -FunctionType is not a built-in function type
440+
if ($CreatedScriptBlock -and
441+
$functionName -and $FunctionType -notin 'function', 'filter') {
442+
# return the transpiled script.
443+
return $createdScriptBlock.Transpile()
444+
} else {
445+
# otherwise, return the created script.
446+
return $createdScriptBlock
447+
}
382448
}
383449
}
384450

0 commit comments

Comments
 (0)