Skip to content

Commit b948eca

Browse files
author
James Brundage
committed
Include - Adding -Exclude (Fixes #418)
1 parent 38cd3e1 commit b948eca

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

Transpilers/Include.psx.ps1

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
[Include('*-*.ps1')]$psScriptRoot
1818
} | .>PipeScript
1919
#>
20+
[ValidateScript({
21+
if ($_ -is [Management.Automation.Language.CommandAst]) {
22+
return $_.CommandsElements[0].Value -in 'include','includes'
23+
}
24+
})]
25+
[Alias('Includes')]
2026
param(
2127
# The File Path to Include
2228
[Parameter(Mandatory,Position=0)]
@@ -31,11 +37,45 @@ $AsByte,
3137
[switch]
3238
$Passthru,
3339

40+
# The exclusion pattern to use.
41+
[Alias('ExcludePattern')]
42+
[string[]]
43+
$Exclude = '\.[^\.]+\.ps1$',
44+
3445
[Parameter(Mandatory,ParameterSetName='VariableAST', ValueFromPipeline)]
3546
[Management.Automation.Language.VariableExpressionast]
36-
$VariableAst
47+
$VariableAst,
48+
49+
50+
[Parameter(Mandatory,ParameterSetName='CommandAst',ValueFromPipeline)]
51+
[Management.Automation.Language.CommandAst]
52+
$CommandAst
3753
)
3854

55+
process {
56+
57+
if ($psCmdlet.ParameterSetName -eq 'CommandAst') {
58+
# Gather some information about our calling context
59+
$myParams = [Ordered]@{} + $PSBoundParameters
60+
# and attempt to parse it as a sentance (only allowing it to match this particular command)
61+
$mySentence = $commandAst.AsSentence($MyInvocation.MyCommand)
62+
$myCmd = $MyInvocation.MyCommand
63+
64+
# Walk thru all mapped parameters in the sentence
65+
foreach ($paramName in $mySentence.Parameters.Keys) {
66+
if (-not $myParams[$paramName]) { # If the parameter was not directly supplied
67+
$myParams[$paramName] = $mySentence.Parameters[$paramName] # grab it from the sentence.
68+
foreach ($myParam in $myCmd.Parameters.Values) {
69+
if ($myParam.Aliases -contains $paramName) { # set any variables that share the name of an alias
70+
$ExecutionContext.SessionState.PSVariable.Set($myParam.Name, $mySentence.Parameters[$paramName])
71+
}
72+
}
73+
# and set this variable for this value.
74+
$ExecutionContext.SessionState.PSVariable.Set($paramName, $mySentence.Parameters[$paramName])
75+
}
76+
}
77+
}
78+
3979
# Determine the command we would be including (relative to the current path)
4080
$includingCommand = $ExecutionContext.SessionState.InvokeCommand.GetCommand($FilePath, 'All')
4181
if (-not $includingCommand) { # if we could not determine the command, we may need to error out.
@@ -140,12 +180,19 @@ if ($psCmdlet.ParameterSetName -eq 'ScriptBlock' -or
140180
[ScriptBlock]::Create("$($VariableAst) = $IncludedScript")
141181
} elseif ($VariableAst.VariablePath -notmatch '^null$') {
142182
[ScriptBlock]::Create(@"
143-
foreach (`$file in (Get-ChildItem -Path "$($VariableAst)" -Filter "$FilePath" -Recurse)) {
183+
:ToIncludeFiles foreach (`$file in (Get-ChildItem -Path "$($VariableAst)" -Filter "$FilePath" -Recurse)) {
144184
if (`$file.Extension -ne '.ps1') { continue } # Skip if the extension is not .ps1
145-
if (`$file.Name -match '\.[^\.]+\.ps1$') { continue } # Skip if the file is an unrelated file.
185+
foreach (`$exclusion in '$($Exclude -replace "'","''" -join "','")') {
186+
if (-not `$exclusion) { continue }
187+
if (`$file.Name -match `$exclusion) {
188+
continue ToIncludeFiles # Skip excluded files
189+
}
190+
}
146191
. `$file.FullName$(
147192
if ($Passthru) { [Environment]::NewLine + (' ' * 4) + '$file'}
148193
)
149194
}
150195
"@)
151196
}
197+
198+
}

0 commit comments

Comments
 (0)