Skip to content

Commit cfad468

Browse files
Merge pull request #117 from StartAutomating/PipeScriptUpdates
Pipe script updates
2 parents ed95220 + 71e162c commit cfad468

File tree

82 files changed

+4437
-184
lines changed

Some content is hidden

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

82 files changed

+4437
-184
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
## 0.0.8:
2+
* New Commands:
3+
* New-PipeScript (#94)
4+
* Search-PipeScript (#115)
5+
* New Transpilers:
6+
* REST (#114)
7+
* Inline.Kotlin (#110)
8+
* Bugfixes and improvements:
9+
* Fixing Help Generation (#56)
10+
* Anchoring match for Get-Transpiler (#109)
11+
* Core Inline Transpiler Cleanup (#111)
12+
* Shared Context within Inline Transpilers (#112)
13+
* Fixing Include Transpiler Pattern (#96)
14+
* Join-PipeScript interactive .Substring error (#116)
15+
---
16+
117
## 0.0.7:
218
* Syntax Improvements:
319
* Support for Dot Notation (#107)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-FormatView -TypeName Search.PipeScript.Result -GroupByProperty InputObject -Property Result, Expression

Get-Transpiler.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#region Piecemeal [ 0.3.2 ] : Easy Extensible Plugins for PowerShell
22
# Install-Module Piecemeal -Scope CurrentUser
33
# Import-Module Piecemeal -Force
4-
# Install-Piecemeal -ExtensionNoun 'Transpiler' -ExtensionPattern '\.psx\.ps1' -ExtensionTypeName 'PipeScript.Transpiler' -OutputPath '.\Get-Transpiler.ps1'
4+
# Install-Piecemeal -ExtensionNoun 'Transpiler' -ExtensionPattern '\.psx\.ps1$' -ExtensionTypeName 'PipeScript.Transpiler' -OutputPath '.\Get-Transpiler.ps1'
55
function Get-Transpiler
66
{
77
<#
@@ -170,7 +170,7 @@ function Get-Transpiler
170170
)
171171

172172
begin {
173-
$TranspilerPattern = '\.psx\.ps1'
173+
$TranspilerPattern = '\.psx\.ps1$'
174174
$TranspilerTypeName = 'PipeScript.Transpiler'
175175
#region Define Inner Functions
176176
function WhereExtends {

Invoke-PipeScript.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,10 @@
497497
elseif ($script:TypeAcceleratorsList -notcontains $transpilerStepName -and $transpilerStepName -notin 'Ordered') {
498498
$psCmdlet.WriteError(
499499
[Management.Automation.ErrorRecord]::new(
500-
[exception]::new("Unable to find a transpiler for [$TranspilerStepName]")
500+
[exception]::new("Unable to find a transpiler for [$TranspilerStepName]"),
501+
'Transpiler.Not.Found',
502+
'ParserError',
503+
$command
501504
)
502505
)
503506

Join-PipeScript.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ function Join-PipeScript
120120
if ($BlockType -contains 'param') {
121121
foreach ($combined in $AllScriptBlocks.Ast.ParamBlock) {
122122
if (-not $combined.Parent.Extent) { continue }
123-
$combined.Parent.Extent.ToString().Substring(0, $combined.Extent.StartOffset)
123+
$offsetDifference = $combined.Extent.StartOffset - $combined.Parent.Extent.StartOffset
124+
$combined.Parent.Extent.ToString().Substring(0, $offsetDifference) -replace '^[\r\n]+\{'
124125
}
125126
}
126127
# Start the param block

New-PipeScript.ps1

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
function New-PipeScript {
2+
<#
3+
.Synopsis
4+
Creates new PipeScript.
5+
.Description
6+
Creates new PipeScript and PowerShell ScriptBlocks.
7+
.EXAMPLE
8+
New-PipeScript -Parameter @{a='b'}
9+
#>
10+
[Alias('New-ScriptBlock')]
11+
param(
12+
# Defines one or more parameters for a ScriptBlock.
13+
# Parameters can be defined in a few ways:
14+
# * As a ```[Collections.Dictionary]``` of Parameters
15+
# * As the ```[string]``` name of an untyped parameter.
16+
# * As an ```[Object[]]```.
17+
# * As a ```[ScriptBlock]```
18+
[Parameter(ValueFromPipelineByPropertyName)]
19+
[ValidateScript({
20+
$validTypeList = [System.Collections.IDictionary],[System.String],[System.Object[]],[System.Management.Automation.ScriptBlock]
21+
$thisType = $_.GetType()
22+
$IsTypeOk =
23+
$(@( foreach ($validType in $validTypeList) {
24+
if ($_ -as $validType) {
25+
$true;break
26+
}
27+
}))
28+
if (-not $isTypeOk) {
29+
throw "Unexpected type '$(@($thisType)[0])'. Must be 'System.Collections.IDictionary','string','System.Object[]','scriptblock'."
30+
}
31+
return $true
32+
})]
33+
34+
$Parameter,
35+
36+
# The dynamic parameter block.
37+
[Parameter(ValueFromPipelineByPropertyName)]
38+
[ValidateScript({
39+
if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock) {
40+
throw "ScriptBlock should not have any named blocks"
41+
}
42+
return $true
43+
})]
44+
[ValidateScript({
45+
if ($_.Ast.ParamBlock.Parameters.Count) {
46+
throw "ScriptBlock should not have parameters"
47+
}
48+
return $true
49+
})]
50+
[Alias('DynamicParameterBlock')]
51+
[ScriptBlock]
52+
$DynamicParameter,
53+
# The begin block.
54+
[Parameter(ValueFromPipelineByPropertyName)]
55+
[ValidateScript({
56+
if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock) {
57+
throw "ScriptBlock should not have any named blocks"
58+
}
59+
return $true
60+
})]
61+
[ValidateScript({
62+
if ($_.Ast.ParamBlock.Parameters.Count) {
63+
throw "ScriptBlock should not have parameters"
64+
}
65+
return $true
66+
})]
67+
[Alias('BeginBlock')]
68+
[ScriptBlock]
69+
$Begin,
70+
# The process block.
71+
[Parameter(ValueFromPipelineByPropertyName)]
72+
[ValidateScript({
73+
if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock) {
74+
throw "ScriptBlock should not have any named blocks"
75+
}
76+
return $true
77+
})]
78+
[ValidateScript({
79+
if ($_.Ast.ParamBlock.Parameters.Count) {
80+
throw "ScriptBlock should not have parameters"
81+
}
82+
return $true
83+
})]
84+
[Alias('ProcessBlock')]
85+
[ScriptBlock]
86+
$Process,
87+
# The end block.
88+
[Parameter(ValueFromPipelineByPropertyName)]
89+
[ValidateScript({
90+
if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock) {
91+
throw "ScriptBlock should not have any named blocks"
92+
}
93+
return $true
94+
})]
95+
[ValidateScript({
96+
if ($_.Ast.ParamBlock.Parameters.Count) {
97+
throw "ScriptBlock should not have parameters"
98+
}
99+
return $true
100+
})]
101+
[Alias('EndBlock')]
102+
[ScriptBlock]
103+
$End,
104+
105+
# The script header.
106+
[Parameter(ValueFromPipelineByPropertyName)]
107+
[string]
108+
$Header
109+
)
110+
begin {
111+
$ParametersToCreate = [Ordered]@{}
112+
$allDynamicParameters = @()
113+
$allBeginBlocks = @()
114+
$allEndBlocks = @()
115+
$allProcessBlocks = @()
116+
$allHeaders = @()
117+
}
118+
process {
119+
if ($parameter) {
120+
# The -Parameter can be a dictionary of parameters.
121+
if ($Parameter -is [Collections.IDictionary]) {
122+
$parameterType = ''
123+
# If it is, walk thur each parameter in the dictionary
124+
foreach ($EachParameter in $Parameter.GetEnumerator()) {
125+
# Continue past any parameters we already have
126+
if ($ParametersToCreate.Contains($EachParameter.Key)) {
127+
continue
128+
}
129+
# If the parameter is a string and the value is not a variable
130+
if ($EachParameter.Value -is [string] -and $EachParameter.Value -notlike '*$*') {
131+
$parameterName = $EachParameter.Key
132+
$ParametersToCreate[$EachParameter.Key] =
133+
@(
134+
$parameterAttribute = "[Parameter(ValueFromPipelineByPropertyName)]"
135+
$parameterType
136+
'$' + $parameterName
137+
) -ne ''
138+
}
139+
# If the value is a string and the value contains variables
140+
elseif ($EachParameter.Value -is [string]) {
141+
# embed it directly.
142+
$ParametersToCreate[$EachParameter.Key] = $EachParameter.Value
143+
}
144+
# If the value is a ScriptBlock
145+
elseif ($EachParameter.Value -is [ScriptBlock]) {
146+
# Embed it
147+
$ParametersToCreate[$EachParameter.Key] =
148+
# If there was a param block on the script block
149+
if ($EachParameter.Value.Ast.ParamBlock) {
150+
# embed the parameter block (except for the param keyword)
151+
$EachParameter.Value.Ast.ParamBlock.Extent.ToString() -replace
152+
'^[\s\r\n]param(' -replace ')[\s\r\n]$'
153+
} else {
154+
# Otherwise
155+
$EachParameter.Value.ToString() -replace
156+
"\`$$($eachParameter.Key)[\s\r\n]$" -replace # Replace any trailing variables
157+
'param()[\s\r\n]{0,}$' # then replace any empty param blocks.
158+
}
159+
}
160+
elseif ($EachParameter.Value -is [Object[]]) {
161+
$ParametersToCreate[$EachParameter.Key] =
162+
$EachParameter.Value -join [Environment]::Newline
163+
}
164+
}
165+
} elseif ($Parameter -is [string]) {
166+
$ParametersToCreate[$Parameter] = @(
167+
"[Parameter(ValueFromPipelineByPropertyName)]"
168+
"`$$Parameter"
169+
)
170+
} elseif ($Parameter -is [Object[]]) {
171+
$currentParam = @()
172+
$currentParamName = ''
173+
foreach ($EachParameter in $Parameter) {
174+
if ($EachParameter -is [string] -and -not $EachParameter.Contains(' ')) {
175+
if ($currentParam) {
176+
$ParametersToCreate[$currentParamName] = $currentParam
177+
$currentParam = @()
178+
$currentParamName = ''
179+
}
180+
$currentParam += "`$$EachParameter"
181+
$currentParamName = $EachParameter
182+
} elseif ($EachParameter -is [string] -and $EachParameter.Contains(' ')) {
183+
$currentParam = @(
184+
if ($EachParameter.Contains("`n")) {
185+
"<#" + [Environment]::newLine + $EachParameter + [Environment]::newLine + '#>'
186+
} else {
187+
"# $EachParameter"
188+
}
189+
) + $currentParam
190+
} elseif ($EachParameter -is [type]) {
191+
$currentParam += "[$($EachParameter.Fullname)]"
192+
}
193+
}
194+
if ($currentParamName) {
195+
$ParametersToCreate[$currentParamName] = $currentParam
196+
}
197+
}
198+
}
199+
if ($header) {
200+
$allHeaders += $Header
201+
}
202+
if ($DynamicParameter) {
203+
$allDynamicParameters += $DynamicParameter
204+
}
205+
if ($Begin) {
206+
$allBeginBlocks += $begin
207+
}
208+
if ($process) {
209+
$allProcessBlocks += $process
210+
}
211+
if ($end) {
212+
$allEndBlocks += $end
213+
}
214+
215+
}
216+
end {
217+
$newParamBlock =
218+
"param(" + [Environment]::newLine +
219+
$(@(foreach ($toCreate in $ParametersToCreate.GetEnumerator()) {
220+
$toCreate.Value -join [Environment]::NewLine
221+
}) -join (',' + [Environment]::NewLine)) +
222+
[Environment]::NewLine +
223+
')'
224+
$createdScriptBlock = [scriptblock]::Create("
225+
$($allHeaders -join [Environment]::Newline)
226+
$newParamBlock
227+
$(if ($allDynamicParameters) {
228+
@(@("dynamicParam {") + $allDynamicParameters + '}') -join [Environment]::Newline
229+
})
230+
$(if ($allBeginBlocks) {
231+
@(@("begin {") + $allBeginBlocks + '}') -join [Environment]::Newline
232+
})
233+
$(if ($allProcessBlocks) {
234+
@(@("process {") + $allProcessBlocks + '}') -join [Environment]::Newline
235+
})
236+
$(if ($allEndBlocks -and -not $allBeginBlocks -and -not $allProcessBlocks) {
237+
$allEndBlocks -join [Environment]::Newline
238+
} elseif ($allEndBlocks) {
239+
@(@("end {") + $allEndBlocks + '}') -join [Environment]::Newline
240+
})
241+
")
242+
$createdScriptBlock
243+
}
244+
}
245+

0 commit comments

Comments
 (0)