Skip to content

Commit f7cddce

Browse files
author
James Brundage
committed
Adding Include (#47)
1 parent 745adbc commit f7cddce

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

Transpilers/Include.psx.ps1

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<#
2+
.SYNOPSIS
3+
Includes Files
4+
.DESCRIPTION
5+
Includes Files or Functions into a Script.
6+
.Example
7+
{
8+
[Include("Invoke-PipeScript")]$null
9+
} | .>PipeScript
10+
.Example
11+
{
12+
[Include("Invoke-PipeScript")]
13+
param()
14+
} | .>PipeScript
15+
#>
16+
param(
17+
# The File Path to Include
18+
[Parameter(Mandatory,Position=0)]
19+
[string]
20+
$FilePath,
21+
22+
# If set, will include the content as a byte array
23+
[switch]
24+
$AsByte,
25+
26+
[Parameter(Mandatory,ParameterSetName='VariableAST', ValueFromPipeline)]
27+
[Management.Automation.Language.VariableExpressionast]
28+
$VariableAst
29+
)
30+
31+
$includingCommand = $ExecutionContext.SessionState.InvokeCommand.GetCommand($FilePath, 'All')
32+
if (-not $includingCommand) {
33+
Write-Error "Could not resolve $($FilePath)"
34+
return
35+
}
36+
37+
function IncludeFileContents {
38+
param($FilePath)
39+
if ($AsByte) {
40+
[ScriptBlock]::Create(
41+
"@'" + [Environment]::NewLine +
42+
[Convert]::ToBase64String(
43+
[IO.File]::ReadAllBytes($FilePath),
44+
'InsertLineBreaks'
45+
) + [Environment]::NewLine +
46+
"'@")
47+
48+
} else {
49+
[ScriptBlock]::Create(
50+
"@'" +
51+
[Environment]::NewLine +
52+
([IO.File]::ReadAllLines($FilePath) -replace "^@'", "@''" -replace "^'@", "''@" -join [Environment]::NewLine) +
53+
[Environment]::NewLine +
54+
"'@" + @'
55+
-split "[\r\n]{1,2}" -replace "^@''", "@'" -replace "^''@", "'@" -join [Environment]::NewLine
56+
'@
57+
)
58+
59+
}
60+
}
61+
62+
$includedScript, $assignable =
63+
if ($includingCommand -is [Management.Automation.CmdletInfo]) {
64+
Write-Error "Cannot Include Cmdlets"
65+
return
66+
}
67+
elseif ($includingCommand -is [Management.Automation.FunctionInfo]) {
68+
if ($VariableAst -and $VariableAst.VariablePath -notmatch '^null$') {
69+
# If we're including a function as a variable, define it as a ScriptBlock
70+
[ScriptBlock]::Create(@"
71+
{
72+
$($includingCommand.ScriptBlock)
73+
}
74+
"@)
75+
} else {
76+
# If we're including a function, define it inline
77+
[ScriptBlock]::Create(@"
78+
function $($includingCommand.Name) {
79+
$($includingCommand.ScriptBlock)
80+
}
81+
"@)
82+
}
83+
} elseif ($includingCommand.ScriptBlock) {
84+
# If we're including a command with a ScriptBlock, assign it to a variable
85+
[ScriptBlock]::Create(@"
86+
`${$($includingCommand.Name)} = {
87+
$($includingCommand.ScriptBlock)
88+
}
89+
"@)
90+
91+
}
92+
elseif ($includingCommand.Source -match '\.ps1{0,}\.(?<ext>[^.]+$)') {
93+
$transpiledFile = Invoke-PipeScript -CommandInfo $includingCommand
94+
if (-not $transpiledFile) {
95+
Write-Error "Could not transpile $($includingCommand.Source)"
96+
return
97+
}
98+
IncludeFileContents $transpiledFile.Fullname
99+
}
100+
elseif ($includingCommand.Source -match '\.ps$') {
101+
[ScriptBlock]::Create(@"
102+
`${$($includingCommand.Name)} = {
103+
$([ScriptBlock]::Create([IO.File]::ReadAllText($includingCommand.Source)) | .<PipeScript>)
104+
}
105+
"@)
106+
107+
} else {
108+
IncludeFileContents $includingCommand.Source
109+
}
110+
111+
if ($psCmdlet.ParameterSetName -eq 'ScriptBlock' -or
112+
$VariableAst.VariablePath -match '^null$') {
113+
if ($ScriptBlock -and $ScriptBlock.ToString().Length) {
114+
[ScriptBlock]::Create("$ScriptBlock" + [Environment]::NewLine + $includedScript)
115+
} else {
116+
$includedScript
117+
}
118+
119+
} elseif ($VariableAst.VariablePath) {
120+
[ScriptBlock]::Create("$($VariableAst.VariablePath) = $IncludedScript")
121+
}

0 commit comments

Comments
 (0)