Skip to content

Commit e1c598f

Browse files
author
James Brundage
committed
feat: Use-Module ( Fixes #1130 )
Fixing parameters and fulfilling promise
1 parent 3606c06 commit e1c598f

File tree

1 file changed

+59
-13
lines changed

1 file changed

+59
-13
lines changed

Commands/Module/Use-Module.ps.ps1

+59-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ function Use-Module
1010
1111
This can either:
1212
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+
1416
* Run a module's `Use` function, if present
1517
* Call a module's `Use` method, if present
1618
@@ -30,13 +32,16 @@ function Use-Module
3032
[ScriptBlock]
3133
$ScriptBlock = {},
3234

35+
# The list of arguments to pass to the script block.
3336
[Parameter(ValueFromRemainingArguments)]
3437
[Alias('Arguments','Args')]
3538
[PSObject[]]
3639
$ArgumentList,
3740

41+
# Any named parameters to pass to the script block.
3842
[Parameter(ValueFromPipelineByPropertyName)]
39-
[Collection.IDictionary]
43+
[Alias('Parameters')]
44+
[Collections.IDictionary]
4045
$Parameter
4146
)
4247

@@ -46,26 +51,67 @@ function Use-Module
4651
# If we have no name, return
4752
return if -not $name
4853

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+
}
5563

56-
64+
# Return if there is no module context.
5765
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
58100

101+
# The rest of the code is tedium.
102+
# If there were arguments and parameters, pass them both with splatting.
59103
if ($ArgumentList) {
60104
if ($Parameter) {
61-
. $moduleContext $ScriptBlock @ArgumentList @Parameter
105+
. $runningIn $ToRun @ArgumentList @Parameter
62106
} else {
63-
. $moduleContext $ScriptBlock @ArgumentList
107+
. $runningIn $ToRun @ArgumentList
64108
}
65109
} elseif ($Parameter) {
66-
. $moduleContext $ScriptBlock @Parameter
110+
# If there were only parameters, pass them with splatting.
111+
. $runningIn $ToRun @Parameter
67112
} else {
68-
. $moduleContext $ScriptBlock
113+
# If there were no arguments or parameters, just run the script block.
114+
. $runningIn $ToRun
69115
}
70116
}
71117
}

0 commit comments

Comments
 (0)