Skip to content

Commit ed95220

Browse files
Merge pull request #108 from StartAutomating/PipeScriptFunAndFixes
Pipe script fun and fixes
2 parents f9d8529 + 76ecbe7 commit ed95220

12 files changed

+656
-35
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
## 0.0.7:
2+
* Syntax Improvements:
3+
* Support for Dot Notation (#107)
4+
* New Transpilers:
5+
* .>ModuleRelationships (#105)
6+
* .>ModuleExports (#104)
7+
* .>Aliases (#106)
8+
* Fixes:
9+
* Invoke-PipeScript improved error behavior (#103)
10+
* Explicit Transpiler returns modified ScriptBlock (#102)
11+
* .psm1 alias export fix (#100)
12+
* Include improvements (#96)
13+
---
14+
115
## 0.0.6:
216
* New Transpilers:
317
* ValidateScriptBlock

PipeScript.ps1.psm1

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
[Include('*-*')]$psScriptRoot
22

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-
}
3+
$transpilerNames = Get-Transpiler | Select-Object -ExpandProperty DisplayName
4+
$aliasList +=
5+
[SmartAlias(Command='Use-PipeScript',Prefix='.>',PassThru)]$transpilerNames
6+
7+
$aliasList +=
8+
[SmartAlias(Command='Use-PipeScript',Prefix='.<',Suffix='>',PassThru)]$transpilerNames
109

1110
$MyModule = $MyInvocation.MyCommand.ScriptBlock.Module
12-
foreach ($cmd in $ExecutionContext.SessionState.InvokeCommand.GetCommands('*','Function', $true)) {
13-
if ($cmd.ScriptBlock.Module -ne $MyModule) { continue }
14-
if ($cmd.ScriptBlock.Attributes.AliasNames) {
15-
$aliasNames += $cmd.ScriptBlock.Attributes.AliasNames
16-
}
17-
}
11+
$aliasList +=
12+
[GetExports("Alias")]$MyModule
1813

19-
Export-ModuleMember -Function * -Alias $aliasNames
14+
Export-ModuleMember -Function * -Alias $aliasList

PipeScript.psd1

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

1818
Tags = 'PipeScript','PowerShell', 'Transpilation', 'Compiler'
1919
ReleaseNotes = @'
20+
## 0.0.7:
21+
* Syntax Improvements:
22+
* Support for Dot Notation (#107)
23+
* New Transpilers:
24+
* .>ModuleRelationships (#105)
25+
* .>ModuleExports (#104)
26+
* .>Aliases (#106)
27+
* Fixes:
28+
* Invoke-PipeScript improved error behavior (#103)
29+
* Explicit Transpiler returns modified ScriptBlock (#102)
30+
* .psm1 alias export fix (#100)
31+
* Include improvements (#96)
32+
---
33+
2034
## 0.0.6:
2135
* New Transpilers:
2236
* ValidateScriptBlock

PipeScript.psm1

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
11
foreach ($file in (Get-ChildItem -Path "$psScriptRoot" -Filter "*-*" -Recurse)) {
22
if ($file.Extension -ne '.ps1') { continue } # Skip if the extension is not .ps1
3-
if ($file.Name -match '\.ps1\.ps1$') { continue } # Skip if the file is a source generator.
3+
if ($file.Name -match '\.[^\.]\.ps1$') { continue } # Skip if the file is an unrelated file.
44
. $file.FullName
55
}
66

7-
$aliasNames = @()
8-
foreach ($transpilerCmd in Get-Transpiler) {
9-
$aliasNames += ".>$($transpilerCmd.DisplayName)"
10-
Set-Alias ".>$($transpilerCmd.DisplayName)" Use-PipeScript
11-
$aliasNames += ".<$($transpilerCmd.DisplayName)>"
12-
Set-Alias ".<$($transpilerCmd.DisplayName)>" Use-PipeScript
13-
}
7+
$transpilerNames = Get-Transpiler | Select-Object -ExpandProperty DisplayName
8+
$aliasList +=
9+
10+
@(foreach ($alias in @($transpilerNames)) {
11+
Set-Alias ".>$alias" "Use-PipeScript" -PassThru:$True
12+
})
13+
14+
15+
$aliasList +=
16+
17+
@(foreach ($alias in @($transpilerNames)) {
18+
Set-Alias ".<$alias>" "Use-PipeScript" -PassThru:$True
19+
})
20+
1421

1522
$MyModule = $MyInvocation.MyCommand.ScriptBlock.Module
16-
foreach ($cmd in $ExecutionContext.SessionState.InvokeCommand.GetCommands('*','Function', $true)) {
17-
if ($cmd.ScriptBlock.Module -ne $MyModule) { continue }
18-
if ($cmd.ScriptBlock.Attributes.AliasNames) {
19-
$aliasNames += $cmd.ScriptBlock.Attributes.AliasNames
20-
}
21-
}
23+
$aliasList +=
24+
25+
@(
26+
if ($MyModule -isnot [Management.Automation.PSModuleInfo]) {
27+
Write-Error "'$MyModule' must be a [Management.Automation.PSModuleInfo]"
28+
} elseif ($MyModule.ExportedCommands.Count) {
29+
foreach ($cmd in $MyModule.ExportedCommands.Values) {
30+
if ($cmd.CommandType -in 'Alias') {
31+
$cmd
32+
}
33+
}
34+
} else {
35+
foreach ($cmd in $ExecutionContext.SessionState.InvokeCommand.GetCommands('*', 'Function,Cmdlet', $true)) {
36+
if ($cmd.Module -ne $MyModule) { continue }
37+
if ('Alias' -contains 'Alias' -and $cmd.ScriptBlock.Attributes.AliasNames) {
38+
foreach ($aliasName in $cmd.ScriptBlock.Attributes.AliasNames) {
39+
$ExecutionContext.SessionState.InvokeCommand.GetCommand($aliasName, 'Alias')
40+
}
41+
}
42+
if ('Alias' -contains $cmd.CommandType) {
43+
$cmd
44+
}
45+
}
46+
})
47+
2248

23-
Export-ModuleMember -Function * -Alias $aliasNames
49+
Export-ModuleMember -Function * -Alias $aliasList

Transpilers/Explicit.psx.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ process {
6868
$astReplacements[$pipeline] = [ScriptBlock]::Create("`$null = $pipeline")
6969
}
7070
}
71-
$astReplacements
71+
72+
Update-PipeScript -ScriptBlock $ScriptBlock -AstReplacement $astReplacements
7273
# @{AstReplacement=$astReplacements}
7374
}

Transpilers/Include.psx.ps1

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
} | .>PipeScript
1515
.EXAMPLE
1616
{
17-
[Include('*-*.ps1')]$psScriptRoot
17+
[Include('*-*.ps1')]$psScriptRoot
1818
} | .>PipeScript
1919
#>
2020
param(
@@ -62,8 +62,7 @@ function IncludeFileContents {
6262
"'@" + @'
6363
-split "[\r\n]{1,2}" -replace "^@''", "@'" -replace "^''@", "'@" -join [Environment]::NewLine
6464
'@
65-
)
66-
65+
)
6766
}
6867
}
6968
}
@@ -131,7 +130,7 @@ if ($psCmdlet.ParameterSetName -eq 'ScriptBlock' -or
131130
[ScriptBlock]::Create(@"
132131
foreach (`$file in (Get-ChildItem -Path "$($VariableAst)" -Filter "$FilePath" -Recurse)) {
133132
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.
133+
if (`$file.Name -match '\.[^\.]\.ps1$') { continue } # Skip if the file is an unrelated file.
135134
. `$file.FullName
136135
}
137136
"@)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<#
2+
.Synopsis
3+
Gets Module Exports
4+
.Description
5+
Gets Exported Commands from a module.
6+
.EXAMPLE
7+
.> {
8+
$PipeScriptModule = Get-Module PipeScript
9+
$exports = [ModuleExports()]$PipeScriptModule
10+
$exports
11+
}
12+
#>
13+
[CmdletBinding(DefaultParameterSetName='None',PositionalBinding=$false)]
14+
[ValidateScript({
15+
$val = $_
16+
if (
17+
($val.Parent -is [Management.Automation.Language.AttributedExpressionAst]) -and
18+
($val.Parent.Attribute.TypeName.Name -in 'ModuleExport', 'GetExport', 'GetExports', 'ListExports', 'ModuleExport', 'GetModuleExport', 'GetModuleExports')
19+
) {
20+
return $true
21+
}
22+
return $false
23+
})]
24+
[Alias('GetExport','GetExports', 'ListExport','ListExports','ModuleExport','GetModuleExport', 'GetModuleExports')]
25+
param(
26+
# The command type
27+
[Parameter(Position=0)]
28+
[Management.Automation.CommandTypes[]]
29+
$CommandType = @('Alias','Function','Cmdlet'),
30+
31+
# A VariableExpression. This variable must contain a module.
32+
[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='VariableExpressionAST')]
33+
[Management.Automation.Language.VariableExpressionAST]
34+
$VariableAST
35+
)
36+
37+
process {
38+
39+
$var = $VariableAST.Extent.ToString()
40+
[scriptblock]::Create($(
41+
{
42+
@(
43+
if ($var -isnot [Management.Automation.PSModuleInfo]) {
44+
Write-Error "'$var' must be a [Management.Automation.PSModuleInfo]"
45+
} elseif ($var.ExportedCommands.Count) {
46+
foreach ($cmd in $var.ExportedCommands.Values) {
47+
if ($cmd.CommandType -in $CommandType) {
48+
$cmd
49+
}
50+
}
51+
} else {
52+
foreach ($cmd in $ExecutionContext.SessionState.InvokeCommand.GetCommands('*', 'Function,Cmdlet', $true)) {
53+
if ($cmd.Module -ne $var) { continue }
54+
if ($CommandType -contains 'Alias' -and $cmd.ScriptBlock.Attributes.AliasNames) {
55+
foreach ($aliasName in $cmd.ScriptBlock.Attributes.AliasNames) {
56+
$ExecutionContext.SessionState.InvokeCommand.GetCommand($aliasName, 'Alias')
57+
}
58+
}
59+
if ($CommandType -contains $cmd.CommandType) {
60+
$cmd
61+
}
62+
}
63+
})
64+
} -replace '\$var', "$var" -replace '\$CommandType', "'$($CommandType -join "','")'"))
65+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
<#
3+
.SYNOPSIS
4+
Gets Module Relationships
5+
.DESCRIPTION
6+
Gets Modules that are related to a given module.
7+
8+
Modules can be related to each other by a few mechanisms:
9+
10+
* A Module Can Include another Module's Name in it's ```.PrivateData.PSData.Tags```
11+
* A Module Can include data for another module it it's ```.PrivataData.```
12+
.EXAMPLE
13+
.> {
14+
$Module = Get-Module PipeScript
15+
[ModuleRelationships()]$Module
16+
}
17+
#>
18+
[ValidateScript({
19+
$val = $_
20+
if (
21+
($val.Parent -is [Management.Automation.Language.AttributedExpressionAst]) -and
22+
($val.Parent.Attribute.TypeName.Name -in 'RelatedModules', 'RelatedModule', 'ModuleRelationships')
23+
) {
24+
return $true
25+
}
26+
return $false
27+
})]
28+
[Alias('RelatedModules', 'RelatedModule','ModuleRelationships')]
29+
param(
30+
# A VariableExpression. This variable must contain a module or name of module.
31+
[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='VariableExpressionAST')]
32+
[Management.Automation.Language.VariableExpressionAST]
33+
$VariableAST
34+
)
35+
36+
37+
process {
38+
39+
[scriptblock]::Create($({
40+
41+
@(
42+
43+
$MyModuleName, $myModule =
44+
if ($targetModule -is [string]) {
45+
$targetModule, (Get-Module $targetModule)
46+
} elseif ($targetModule -is [Management.Automation.PSModuleInfo]) {
47+
$targetModule.Name, $targetModule
48+
} else {
49+
Write-Error "$targetModule must be a [string] or [Management.Automation.PSModuleInfo]"
50+
}
51+
52+
53+
#region Search for Module Relationships
54+
if ($myModule -and $MyModuleName) {
55+
foreach ($loadedModule in Get-Module) { # Walk over all modules.
56+
if ( # If the module has PrivateData keyed to this module
57+
$loadedModule.PrivateData.$myModuleName
58+
) {
59+
# Determine the root of the module with private data.
60+
$relationshipData = $loadedModule.PrivateData.$myModuleName
61+
[PSCustomObject][Ordered]@{
62+
PSTypeName = 'Module.Relationship'
63+
Module = $myModule
64+
RelatedModule = $loadedModule
65+
PrivateData = $loadedModule.PrivateData.$myModuleName
66+
}
67+
}
68+
elseif ($loadedModule.PrivateData.PSData.Tags -contains $myModuleName) {
69+
[PSCustomObject][Ordered]@{
70+
PSTypeName = 'Module.Relationship'
71+
Module = $myModule
72+
RelatedModule = $loadedModule
73+
PrivateData = @{}
74+
}
75+
}
76+
}
77+
}
78+
#endregion Search for Module Relationships
79+
80+
)
81+
82+
} -replace '\$TargetModule', "$VariableAST"))
83+
84+
}

0 commit comments

Comments
 (0)