Skip to content

Commit 624734b

Browse files
Merge pull request #199 from StartAutomating/PipeScriptUpdates
Pipe script updates
2 parents 0c6634f + a030834 commit 624734b

File tree

126 files changed

+1452
-168
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+1452
-168
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
## 0.1.2:
2+
* New Inline Language support
3+
* Batch support (Fixes #198)
4+
* Bash Support (Fixes #194)
5+
* Core Inline Transpiler : Adding -LinePattern (fixes #197)
6+
* New-PipeScript: Writing help (Fixes #195) and functions (fixes #196)
7+
* AST Type Improvements:
8+
* Adding [AST].Transpile() (Fixes #192)
9+
* Considering a pipeline in a hashtable to be assigned (Fixes #193)
10+
* [ScriptBlock]/[AST]: Adding .Transpilers (Fixes #185)
11+
* [CommandAst].ResolvedCommand: Checking transpilers first (Fixes #186)
12+
* Improvements:
13+
* new keyword property bag improvements (Fixes #191)
14+
* Use-PipeScript: Defaulting to core transpiler (Fixes #188)
15+
* Core Transpiler: Allowing -ScriptBlock to be provided postionally (Fixes #189)
16+
* Adding Hashtable formatter (Fixes #187)
17+
* HTTP Protocol: Enabling Splatting (Fixes #183)
18+
* Requiring Inline Transpilers accept [Management.Automation.CommandInfo] from the Pipeline (Fixes #184)
19+
---
20+
121
## 0.1.1:
222
* New Keywords:
323
* await (Fixes #181)

Formatting/Hashtable.format.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Write-FormatView -TypeName Hashtable -Name Hashtable -Action {
2+
$_ | Format-Hashtable -Depth $FormatEnumerationLimit
3+
}

Get-PipeScript.ps1

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Piecemeal [ 0.3.3 ] : Easy Extensible Plugins for PowerShell
1+
#region Piecemeal [ 0.3.4 ] : Easy Extensible Plugins for PowerShell
22
# Install-Module Piecemeal -Scope CurrentUser
33
# Import-Module Piecemeal -Force
44
# Install-Piecemeal -ExtensionNoun 'PipeScript' -ExtensionPattern '\.psx\.ps1{0,1}$','\.ps1{0,1}\.(?<ext>[^.]+$)','\.ps1{0,1}$' -ExtensionTypeName 'PipeScript' -OutputPath '.\Get-PipeScript.ps1'
@@ -230,7 +230,7 @@ function Get-PipeScript
230230
}
231231

232232
$extCmd.PSObject.Methods.Add([psscriptmethod]::new('GetExtendedCommands', {
233-
233+
param([Management.Automation.CommandInfo[]]$CommandList)
234234
$extendedCommandNames = @(
235235
foreach ($attr in $this.ScriptBlock.Attributes) {
236236
if ($attr -isnot [Management.Automation.CmdletAttribute]) { continue }
@@ -244,13 +244,16 @@ function Get-PipeScript
244244
$this | Add-Member NoteProperty ExtensionCommands @() -Force
245245
return
246246
}
247-
$allLoadedCmds = $ExecutionContext.SessionState.InvokeCommand.GetCommands('*','All', $true)
247+
if (-not $CommandList) {
248+
$commandList = $ExecutionContext.SessionState.InvokeCommand.GetCommands('*','Function,Alias,Cmdlet', $true)
249+
}
248250
$extends = @{}
249-
foreach ($loadedCmd in $allLoadedCmds) {
251+
:nextCommand foreach ($loadedCmd in $commandList) {
250252
foreach ($extensionCommandName in $extendedCommandNames) {
251253
if ($extensionCommandName -and $loadedCmd.Name -match $extensionCommandName) {
252254
$loadedCmd
253255
$extends[$loadedCmd.Name] = $loadedCmd
256+
continue nextCommand
254257
}
255258
}
256259
}
@@ -263,7 +266,12 @@ function Get-PipeScript
263266
$this | Add-Member NoteProperty ExtensionCommands $extends.Values -Force
264267
}))
265268

266-
$null = $extCmd.GetExtendedCommands()
269+
if (-not $script:AllCommands) {
270+
$script:AllCommands = $ExecutionContext.SessionState.InvokeCommand.GetCommands('*','Function,Alias,Cmdlet', $true)
271+
}
272+
273+
274+
$null = $extCmd.GetExtendedCommands($script:AllCommands)
267275

268276
$inheritanceLevel = [ComponentModel.InheritanceLevel]::Inherited
269277

@@ -824,9 +832,8 @@ function Get-PipeScript
824832
return $allDynamicParameters
825833
}
826834
}
827-
}
828-
#endregion Define Inner Functions
829-
835+
}
836+
#endregion Define Inner Functions
830837

831838
$extensionFullRegex =
832839
[Regex]::New($(
@@ -847,7 +854,8 @@ function Get-PipeScript
847854
$getCmd = $ExecutionContext.SessionState.InvokeCommand.GetCommand
848855

849856
if ($Force) {
850-
$script:PipeScripts = $null
857+
$script:PipeScripts = $null
858+
$script:AllCommands = @()
851859
}
852860
if (-not $script:PipeScripts)
853861
{
@@ -917,5 +925,5 @@ function Get-PipeScript
917925
}
918926
}
919927
}
920-
#endregion Piecemeal [ 0.3.3 ] : Easy Extensible Plugins for PowerShell
928+
#endregion Piecemeal [ 0.3.4 ] : Easy Extensible Plugins for PowerShell
921929

Get-Transpiler.ps1

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Piecemeal [ 0.3.3 ] : Easy Extensible Plugins for PowerShell
1+
#region Piecemeal [ 0.3.4 ] : Easy Extensible Plugins for PowerShell
22
# Install-Module Piecemeal -Scope CurrentUser
33
# Import-Module Piecemeal -Force
44
# Install-Piecemeal -ExtensionNoun 'Transpiler' -ExtensionPattern '\.psx\.ps1$' -ExtensionTypeName 'PipeScript.Transpiler' -OutputPath '.\Get-Transpiler.ps1'
@@ -230,7 +230,7 @@ function Get-Transpiler
230230
}
231231

232232
$extCmd.PSObject.Methods.Add([psscriptmethod]::new('GetExtendedCommands', {
233-
233+
param([Management.Automation.CommandInfo[]]$CommandList)
234234
$extendedCommandNames = @(
235235
foreach ($attr in $this.ScriptBlock.Attributes) {
236236
if ($attr -isnot [Management.Automation.CmdletAttribute]) { continue }
@@ -244,13 +244,16 @@ function Get-Transpiler
244244
$this | Add-Member NoteProperty ExtensionCommands @() -Force
245245
return
246246
}
247-
$allLoadedCmds = $ExecutionContext.SessionState.InvokeCommand.GetCommands('*','All', $true)
247+
if (-not $CommandList) {
248+
$commandList = $ExecutionContext.SessionState.InvokeCommand.GetCommands('*','Function,Alias,Cmdlet', $true)
249+
}
248250
$extends = @{}
249-
foreach ($loadedCmd in $allLoadedCmds) {
251+
:nextCommand foreach ($loadedCmd in $commandList) {
250252
foreach ($extensionCommandName in $extendedCommandNames) {
251253
if ($extensionCommandName -and $loadedCmd.Name -match $extensionCommandName) {
252254
$loadedCmd
253255
$extends[$loadedCmd.Name] = $loadedCmd
256+
continue nextCommand
254257
}
255258
}
256259
}
@@ -263,7 +266,12 @@ function Get-Transpiler
263266
$this | Add-Member NoteProperty ExtensionCommands $extends.Values -Force
264267
}))
265268

266-
$null = $extCmd.GetExtendedCommands()
269+
if (-not $script:AllCommands) {
270+
$script:AllCommands = $ExecutionContext.SessionState.InvokeCommand.GetCommands('*','Function,Alias,Cmdlet', $true)
271+
}
272+
273+
274+
$null = $extCmd.GetExtendedCommands($script:AllCommands)
267275

268276
$inheritanceLevel = [ComponentModel.InheritanceLevel]::Inherited
269277

@@ -824,9 +832,8 @@ function Get-Transpiler
824832
return $allDynamicParameters
825833
}
826834
}
827-
}
828-
#endregion Define Inner Functions
829-
835+
}
836+
#endregion Define Inner Functions
830837

831838
$extensionFullRegex =
832839
[Regex]::New($(
@@ -847,7 +854,8 @@ function Get-Transpiler
847854
$getCmd = $ExecutionContext.SessionState.InvokeCommand.GetCommand
848855

849856
if ($Force) {
850-
$script:Transpilers = $null
857+
$script:Transpilers = $null
858+
$script:AllCommands = @()
851859
}
852860
if (-not $script:Transpilers)
853861
{
@@ -917,5 +925,5 @@ function Get-Transpiler
917925
}
918926
}
919927
}
920-
#endregion Piecemeal [ 0.3.3 ] : Easy Extensible Plugins for PowerShell
928+
#endregion Piecemeal [ 0.3.4 ] : Easy Extensible Plugins for PowerShell
921929

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)