17
17
[Include('*-*.ps1')]$psScriptRoot
18
18
} | .>PipeScript
19
19
#>
20
+ [ValidateScript ({
21
+ if ($_ -is [Management.Automation.Language.CommandAst ]) {
22
+ return $_.CommandsElements [0 ].Value -in ' include' , ' includes'
23
+ }
24
+ })]
25
+ [Alias (' Includes' )]
20
26
param (
21
27
# The File Path to Include
22
28
[Parameter (Mandatory , Position = 0 )]
@@ -31,11 +37,45 @@ $AsByte,
31
37
[switch ]
32
38
$Passthru ,
33
39
40
+ # The exclusion pattern to use.
41
+ [Alias (' ExcludePattern' )]
42
+ [string []]
43
+ $Exclude = ' \.[^\.]+\.ps1$' ,
44
+
34
45
[Parameter (Mandatory , ParameterSetName = ' VariableAST' , ValueFromPipeline )]
35
46
[Management.Automation.Language.VariableExpressionast ]
36
- $VariableAst
47
+ $VariableAst ,
48
+
49
+
50
+ [Parameter (Mandatory , ParameterSetName = ' CommandAst' , ValueFromPipeline )]
51
+ [Management.Automation.Language.CommandAst ]
52
+ $CommandAst
37
53
)
38
54
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
+
39
79
# Determine the command we would be including (relative to the current path)
40
80
$includingCommand = $ExecutionContext.SessionState.InvokeCommand.GetCommand ($FilePath , ' All' )
41
81
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
140
180
[ScriptBlock ]::Create(" $ ( $VariableAst ) = $IncludedScript " )
141
181
} elseif ($VariableAst.VariablePath -notmatch ' ^null$' ) {
142
182
[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)) {
144
184
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
+ }
146
191
. `$ file.FullName$ (
147
192
if ($Passthru ) { [Environment ]::NewLine + (' ' * 4 ) + ' $file' }
148
193
)
149
194
}
150
195
"@ )
151
196
}
197
+
198
+ }
0 commit comments