@@ -10,7 +10,9 @@ function Use-Module
10
10
11
11
This can either:
12
12
13
- * Invoke a ScriptBlock in the module's context
13
+ * If present, run a module's `Use` function, method, or script property.
14
+ * Otherwise, run a script block in the module's context.
15
+
14
16
* Run a module's `Use` function, if present
15
17
* Call a module's `Use` method, if present
16
18
@@ -30,13 +32,16 @@ function Use-Module
30
32
[ScriptBlock ]
31
33
$ScriptBlock = {},
32
34
35
+ # The list of arguments to pass to the script block.
33
36
[Parameter (ValueFromRemainingArguments )]
34
37
[Alias (' Arguments' , ' Args' )]
35
38
[PSObject []]
36
39
$ArgumentList ,
37
40
41
+ # Any named parameters to pass to the script block.
38
42
[Parameter (ValueFromPipelineByPropertyName )]
39
- [Collection.IDictionary ]
43
+ [Alias (' Parameters' )]
44
+ [Collections.IDictionary ]
40
45
$Parameter
41
46
)
42
47
@@ -46,26 +51,67 @@ function Use-Module
46
51
# If we have no name, return
47
52
return if -not $name
48
53
49
- $moduleContext = if ($pipedIn -is [Management.Automation.PSModuleInfo ]) {
50
- $name = $pipedIn
51
- $pipedIn
52
- } else {
53
- Get-Module $Name | Select-Object - First 1
54
- }
54
+ # Get the module context
55
+ $moduleContext =
56
+ # (if it was already piped in, we already have it)
57
+ if ($pipedIn -is [Management.Automation.PSModuleInfo ]) {
58
+ $name = $pipedIn
59
+ $pipedIn
60
+ } else {
61
+ Get-Module $Name | Select-Object - First 1
62
+ }
55
63
56
-
64
+ # Return if there is no module context.
57
65
return if -not $moduleContext
66
+
67
+ # Get the use commands.
68
+ $useCommands = $moduleContent.ExportedCommands [@ (
69
+ " Use-$ ( $moduleContext.Name ) " ,
70
+ " Use.$ ( $moduleContext.Name ) "
71
+ " $ ( $moduleContext.Name ) .Use"
72
+ )]
73
+
74
+ # Get the use method.
75
+ $useMethod = $moduleContext.psobject.methods [" Use" ]
76
+
77
+ $ToRun =
78
+ # If we have a method
79
+ if ($useMethod )
80
+ {
81
+ $useMethod.Script # use it
82
+ # (and pass the script block as an argument)
83
+ $ArgumentList = @ ($ScriptBlock ) + @ ($ArgumentList )
84
+ }
85
+ # If we have any use commands, use the first one
86
+ elseif ($useCommands -ne $null )
87
+ {
88
+ @ ($useCommands -ne $null )[0 ]
89
+ # (and pass the script block as an argument)
90
+ $ArgumentList = @ ($ScriptBlock ) + @ ($ArgumentList )
91
+ }
92
+ else
93
+ {
94
+ # Otherwise, use the script block
95
+ $ScriptBlock
96
+ }
97
+
98
+ # We're running in the module context, and now we know what we want `$toRun`.
99
+ $runningIn = $moduleContext
58
100
101
+ # The rest of the code is tedium.
102
+ # If there were arguments and parameters, pass them both with splatting.
59
103
if ($ArgumentList ) {
60
104
if ($Parameter ) {
61
- . $moduleContext $ScriptBlock @ArgumentList @Parameter
105
+ . $runningIn $ToRun @ArgumentList @Parameter
62
106
} else {
63
- . $moduleContext $ScriptBlock @ArgumentList
107
+ . $runningIn $ToRun @ArgumentList
64
108
}
65
109
} elseif ($Parameter ) {
66
- . $moduleContext $ScriptBlock @Parameter
110
+ # If there were only parameters, pass them with splatting.
111
+ . $runningIn $ToRun @Parameter
67
112
} else {
68
- . $moduleContext $ScriptBlock
113
+ # If there were no arguments or parameters, just run the script block.
114
+ . $runningIn $ToRun
69
115
}
70
116
}
71
117
}
0 commit comments