Skip to content

Commit 0aa1f4c

Browse files
Merge pull request #99 from StartAutomating/PipeScriptingOneself
PipeScripting Oneself
2 parents 4514465 + d7a642d commit 0aa1f4c

File tree

10 files changed

+212
-23
lines changed

10 files changed

+212
-23
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 0.0.6:
2+
* New Transpilers:
3+
* ValidateScriptBlock
4+
* Improved Transpilers:
5+
* [Include] not including source generators (#96)
6+
* PipeScript.psm1 is now build with PipeScript (#95)
7+
* Join-PipeScript: Fixing -BlockType (#97)
8+
* GitHub Action will now look for PipeScript.psd1 in the workspace first (#98)
9+
---
10+
111
## 0.0.5
212
* New Language Features:
313
* PipedAssignment (#88)

Github/Actions/PipeScriptAction.ps1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,15 @@ $($gitHubEvent | ConvertTo-Json -Depth 100)
5252
::endgroup::
5353
"@ | Out-Host
5454

55-
if ($env:GITHUB_ACTION_PATH) {
55+
$PSD1Found = Get-ChildItem -Recurse -Filter "*.psd1" | Where-Object Name -eq 'PipeScript.psd1' | Select-Object -First 1
56+
57+
if ($PSD1Found) {
58+
$PipeScriptModulePath = $PSD1Found
59+
Import-Module $psd1Path -Force -PassThru | Out-Host
60+
} elseif ($env:GITHUB_ACTION_PATH) {
5661
$PipeScriptModulePath = Join-Path $env:GITHUB_ACTION_PATH 'PipeScript.psd1'
5762
if (Test-path $PipeScriptModulePath) {
58-
Import-Module $PipeScriptModulePath -Force -PassThru | Out-String
63+
Import-Module $PipeScriptModulePath -Force -PassThru | Out-Host
5964
} else {
6065
throw "PipeScript not found"
6166
}

Join-PipeScript.ps1

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ function Join-PipeScript
6060
}
6161
}
6262

63-
end {
63+
end {
64+
if ($SkipBlockType) {
65+
foreach ($skipBlock in $SkipBlockType) {
66+
if ($blockType -contains $skipBlock) {
67+
$blockType = $BlockType -ne $skipBlock
68+
}
69+
}
70+
}
6471
$AllScriptBlocks = @(
6572
foreach ($toMerge in $AllScriptBlocks) {
6673
if ($toMerge.Ast.body) {
@@ -72,13 +79,13 @@ function Join-PipeScript
7279

7380

7481
$mergedScript = @(
75-
if ($SkipBlockType -notcontains 'using') {
82+
if ($BlockType -contains 'using') {
7683
foreach ($usingStatement in $AllScriptBlocks.Ast.UsingStatements) {
7784
$usingStatement.Extent.ToString()
7885
}
7986
}
8087

81-
if ($SkipBlockType -notcontains 'requires') {
88+
if ($BlockType -contains 'requires') {
8289
foreach ($requirement in $AllScriptBlocks.Ast.ScriptRequirements) {
8390
if ($requirement.RequirementPSVersion) {
8491
"#requires -Version $($requirement.RequirementPSVersion)"
@@ -110,7 +117,7 @@ function Join-PipeScript
110117
}
111118
}
112119

113-
if ($SkipBlockType -notcontains 'param') {
120+
if ($BlockType -contains 'param') {
114121
foreach ($combined in $AllScriptBlocks.Ast.ParamBlock) {
115122
if (-not $combined.Parent.Extent) { continue }
116123
$combined.Parent.Extent.ToString().Substring(0, $combined.Extent.StartOffset)
@@ -119,7 +126,7 @@ function Join-PipeScript
119126
# Start the param block
120127

121128
$alreadyIncludedParameter = [Ordered]@{}
122-
if ($SkipBlockType -notcontains 'param') {
129+
if ($BlockType -contains 'param') {
123130
if (@($AllScriptBlocks.Ast.ParamBlock) -ne $null) {
124131
' ' * (@(@($AllScriptBlocks.Ast.ParamBlock) -ne $null)[0] | MeasureIndent) + "param("
125132
}
@@ -168,7 +175,7 @@ function Join-PipeScript
168175
}
169176
}
170177

171-
if ($SkipBlockType -notcontains 'dynamicParam') {
178+
if ($BlockType -contains 'dynamicParam') {
172179
$blocks = @($AllScriptBlocks.Ast.DynamicParamBlock)
173180
if ($blocks -ne $null) {
174181
$blockOpen = $false
@@ -187,7 +194,7 @@ function Join-PipeScript
187194

188195

189196

190-
if ($SkipBlockType -notcontains 'begin') { # If there were begin blocks,
197+
if ($BlockType -contains 'begin') { # If there were begin blocks,
191198
$blocks = @($AllScriptBlocks.Ast.BeginBlock)
192199
if ($blocks -ne $null) {
193200
$blockOpen = $false
@@ -204,7 +211,7 @@ function Join-PipeScript
204211
}
205212
}
206213

207-
if ($SkipBlockType -notcontains 'process') { # If there were process blocks
214+
if ($BlockType -contains 'process') { # If there were process blocks
208215
$blocks = @($AllScriptBlocks.Ast.ProcessBlock)
209216
if ($blocks -ne $null) {
210217
$blockOpen = $false
@@ -221,7 +228,7 @@ function Join-PipeScript
221228
}
222229
}
223230

224-
if ($SkipBlockType -notcontains 'end') {
231+
if ($BlockType -contains 'end') {
225232
# If there were end blcoks declared
226233

227234
$blocks = @($AllScriptBlocks.Ast.EndBlock)

PipeScript.ps1.psm1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[Include('*-*')]$psScriptRoot
2+
3+
$aliasNames = @()
4+
foreach ($transpilerCmd in Get-Transpiler) {
5+
$aliasNames += ".>$($transpilerCmd.DisplayName)"
6+
Set-Alias ".>$($transpilerCmd.DisplayName)" Use-PipeScript
7+
$aliasNames += ".<$($transpilerCmd.DisplayName)>"
8+
Set-Alias ".<$($transpilerCmd.DisplayName)>" Use-PipeScript
9+
}
10+
11+
foreach ($cmd in $ExecutionContext.SessionState.InvokeCommand.GetCommands('*','Function', $true)) {
12+
if ($cmd.ScriptBlock.Attributes.AliasNames) {
13+
$aliasNames += $cmd.ScriptBlock.Attributes.AliasNames
14+
}
15+
}
16+
17+
Export-ModuleMember -Function * -Alias $aliasNames

PipeScript.psd1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@{
2-
ModuleVersion = '0.0.5'
2+
ModuleVersion = '0.0.6'
33
Description = 'An Extensible Transpiler for PowerShell (and anything else)'
44
RootModule = 'PipeScript.psm1'
55
PowerShellVersion = '4.0'
@@ -17,6 +17,16 @@
1717

1818
Tags = 'PipeScript','PowerShell', 'Transpilation', 'Compiler'
1919
ReleaseNotes = @'
20+
## 0.0.6:
21+
* New Transpilers:
22+
* ValidateScriptBlock
23+
* Improved Transpilers:
24+
* [Include] not including source generators (#96)
25+
* PipeScript.psm1 is now build with PipeScript (#95)
26+
* Join-PipeScript: Fixing -BlockType (#97)
27+
* GitHub Action will now look for PipeScript.psd1 in the workspace first (#98)
28+
---
29+
2030
## 0.0.5
2131
* New Language Features:
2232
* PipedAssignment (#88)

PipeScript.psm1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
foreach ($file in Get-ChildItem -LiteralPath $PSScriptRoot -Filter *-*.ps1) {
2-
. $file.Fullname
3-
}
1+
foreach ($file in (Get-ChildItem -Path "$psScriptRoot" -Filter "*-*" -Recurse)) {
2+
if ($file.Extension -ne '.ps1') { continue }
3+
. $file.FullName
4+
}
45

56
$aliasNames = @()
67
foreach ($transpilerCmd in Get-Transpiler) {
78
$aliasNames += ".>$($transpilerCmd.DisplayName)"
89
Set-Alias ".>$($transpilerCmd.DisplayName)" Use-PipeScript
910
$aliasNames += ".<$($transpilerCmd.DisplayName)>"
10-
Set-Alias ".<$($transpilerCmd.DisplayName)>" Use-PipeScript
11+
Set-Alias ".<$($transpilerCmd.DisplayName)>" Use-PipeScript
1112
}
1213

1314
foreach ($cmd in $ExecutionContext.SessionState.InvokeCommand.GetCommands('*','Function', $true)) {
@@ -16,4 +17,4 @@ foreach ($cmd in $ExecutionContext.SessionState.InvokeCommand.GetCommands('*','F
1617
}
1718
}
1819

19-
Export-ModuleMember -Function * -Alias $aliasNames
20+
Export-ModuleMember -Function * -Alias $aliasNames

Transpilers/Include.psx.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ if ($psCmdlet.ParameterSetName -eq 'ScriptBlock' -or
130130
} elseif ($VariableAst.VariablePath -notmatch '^null$') {
131131
[ScriptBlock]::Create(@"
132132
foreach (`$file in (Get-ChildItem -Path "$($VariableAst)" -Filter "$FilePath" -Recurse)) {
133-
if (`$file.Extension -ne '.ps1') { continue }
133+
if (`$file.Extension -ne '.ps1') { continue } # Skip if the extension is not .ps1
134+
if (`$file.Name -match '\.ps1\.ps1$') { continue } # Skip if the file is a source generator.
134135
. `$file.FullName
135-
}
136+
}
136137
"@)
137138
}

Transpilers/Parameters/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ When this is the case it is common for the transpiler to add a ```[ValidateScrip
2020
|[ValidateExtension](ValidateExtension.psx.ps1) |[Validates Extensions](ValidateExtension.psx.ps1) |
2121
|[ValidatePlatform](ValidatePlatform.psx.ps1) |[Validates the Platform](ValidatePlatform.psx.ps1) |
2222
|[ValidatePropertyName](ValidatePropertyName.psx.ps1)|[Validates Property Names](ValidatePropertyName.psx.ps1) |
23+
|[ValidateScriptBlock](ValidateScriptBlock.psx.ps1) |[Validates Script Blocks](ValidateScriptBlock.psx.ps1) |
2324
|[ValidateTypes](ValidateTypes.psx.ps1) |[Validates if an object is one or more types.](ValidateTypes.psx.ps1)|
2425
|[VBN](VBN.psx.ps1) |[ValueFromPiplineByPropertyName Shorthand](VBN.psx.ps1) |
2526
|[VFP](VFP.psx.ps1) |[ValueFromPipline Shorthand](VFP.psx.ps1) |
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<#
2+
.SYNOPSIS
3+
Validates Script Blocks
4+
.DESCRIPTION
5+
Validates Script Blocks for a number of common scenarios.
6+
.EXAMPLE
7+
{
8+
param(
9+
[ValidateScriptBlock(Safe)]
10+
[ScriptBlock]
11+
$ScriptBlock
12+
)
13+
14+
$ScriptBlock
15+
} | .>PipeScript
16+
.EXAMPLE
17+
{
18+
param(
19+
[ValidateScriptBlock(NoBlock,NoParameters)]
20+
[ScriptBlock]
21+
$ScriptBlock
22+
)
23+
24+
$ScriptBlock
25+
} | .>PipeScript
26+
.EXAMPLE
27+
{
28+
param(
29+
[ValidateScriptBlock(OnlyParameters)]
30+
[ScriptBlock]
31+
$ScriptBlock
32+
)
33+
34+
$ScriptBlock
35+
} | .>PipeScript
36+
#>
37+
[CmdletBinding(DefaultParameterSetName='Parameter')]
38+
param(
39+
# If set, will validate that ScriptBlock is "safe".
40+
# This will attempt to recreate the Script Block as a datalanguage block and execute it.
41+
[Alias('Safe')]
42+
[switch]
43+
$DataLanguage,
44+
45+
# If set, will ensure that the [ScriptBlock] only has parameters
46+
[Alias('OnlyParameters')]
47+
[switch]
48+
$ParameterOnly,
49+
50+
# If set, will ensure that the [ScriptBlock] has no named blocks.
51+
[Alias('NoBlocks')]
52+
[switch]
53+
$NoBlock,
54+
55+
# If set, will ensure that the [ScriptBlock] has no parameters.
56+
[Alias('NoParameters','NoParam')]
57+
[switch]
58+
$NoParameter,
59+
60+
# A VariableExpression. If provided, the Validation attributes will apply to this variable.
61+
[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='VariableExpressionAST')]
62+
[Management.Automation.Language.VariableExpressionAST]
63+
$VariableAST
64+
)
65+
66+
process {
67+
68+
$validateScripts = @(
69+
if ($DataLanguage) {
70+
@'
71+
[ValidateScript({
72+
$sbCopy = "data { $_ }"
73+
try {
74+
$dataOutput = & ([ScriptBlock]::Create($sbCopy))
75+
return $true
76+
} catch {
77+
throw
78+
}
79+
})]
80+
'@
81+
}
82+
if ($ParameterOnly) {
83+
@'
84+
[ValidateScript({
85+
$statementCount = 0
86+
$statementCount += $_.Ast.DynamicParamBlock.Statements.Count
87+
$statementCount += $_.Ast.BeginBlock.Statements.Count
88+
$statementCount += $_.Ast.ProcessBlock.Statements.Count
89+
$statementCount += $_.Ast.EndBlock.Statements.Count
90+
if ($statementCount) {
91+
throw "ScriptBlock should have no statements"
92+
} else {
93+
return $true
94+
}
95+
})]
96+
'@
97+
}
98+
if ($NoBlock) {
99+
@'
100+
[ValidateScript({
101+
if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock) {
102+
throw "ScriptBlock should not have any named blocks"
103+
}
104+
return $true
105+
})]
106+
'@
107+
}
108+
if ($NoParameter) {
109+
@'
110+
[ValidateScript({
111+
if ($_.Ast.ParamBlock.Parameters.Count) {
112+
throw "ScriptBlock should not have parameters"
113+
}
114+
return $true
115+
})]
116+
'@
117+
}
118+
)
119+
if (-not $validateScripts) { return }
120+
121+
[scriptblock]::Create(@"
122+
$($validateScripts -join [Environment]::NewLine)$(
123+
if ($psCmdlet.ParameterSetName -eq 'Parameter') {
124+
'param()'
125+
} else {
126+
'$' + $VariableAST.variablePath.ToString()
127+
}
128+
)
129+
"@.Trim())
130+
131+
132+
}

action.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ runs:
4848
shell: pwsh
4949
env:
5050
SkipBuild: ${{inputs.SkipBuild}}
51+
UserName: ${{inputs.UserName}}
5152
CommitMessage: ${{inputs.CommitMessage}}
52-
PipeScript: ${{inputs.PipeScript}}
5353
UserEmail: ${{inputs.UserEmail}}
54-
UserName: ${{inputs.UserName}}
54+
PipeScript: ${{inputs.PipeScript}}
5555
run: |
5656
$Parameters = @{}
5757
$Parameters.PipeScript = ${env:PipeScript}
@@ -120,10 +120,15 @@ runs:
120120
::endgroup::
121121
"@ | Out-Host
122122
123-
if ($env:GITHUB_ACTION_PATH) {
123+
$PSD1Found = Get-ChildItem -Recurse -Filter "*.psd1" | Where-Object Name -eq 'PipeScript.psd1' | Select-Object -First 1
124+
125+
if ($PSD1Found) {
126+
$PipeScriptModulePath = $PSD1Found
127+
Import-Module $psd1Path -Force -PassThru | Out-Host
128+
} elseif ($env:GITHUB_ACTION_PATH) {
124129
$PipeScriptModulePath = Join-Path $env:GITHUB_ACTION_PATH 'PipeScript.psd1'
125130
if (Test-path $PipeScriptModulePath) {
126-
Import-Module $PipeScriptModulePath -Force -PassThru | Out-String
131+
Import-Module $PipeScriptModulePath -Force -PassThru | Out-Host
127132
} else {
128133
throw "PipeScript not found"
129134
}

0 commit comments

Comments
 (0)