Skip to content

Commit 9f085c8

Browse files
author
James Brundage
committed
feat: Use-Module ( Fixes #1130 )
Simplifying by narrowing scope.
1 parent 1a52341 commit 9f085c8

File tree

1 file changed

+5
-46
lines changed

1 file changed

+5
-46
lines changed

Commands/Module/Use-Module.ps.ps1

+5-46
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@ function Use-Module
66
.SYNOPSIS
77
Uses a module.
88
.DESCRIPTION
9-
Uses a module.
10-
11-
This can either:
12-
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-
16-
* Run a module's `Use` function, if present
17-
* Call a module's `Use` method, if present
18-
19-
In all cases, the script block should be run in the module's context, using dot-sourcing.
9+
Uses a module's context to run a `[ScriptBlock]` (with dot-sourcing).
2010
.EXAMPLE
2111
Get-Module PipeScript | Use-Module -ScriptBlock { $myInvocation.MyCommand.ScriptBlock.Module }
2212
#>
@@ -61,43 +51,12 @@ function Use-Module
6151
Get-Module $Name | Select-Object -First 1
6252
}
6353

64-
# Return if there is no module context.
65-
return if -not $moduleContext
6654

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`.
55+
# We're running in the module context,
9956
$runningIn = $moduleContext
100-
57+
# and we want `$toRun` the ScriptBlock.
58+
$ToRun = $ScriptBlock
59+
10160
# The rest of the code is tedium.
10261
# If there were arguments and parameters, pass them both with splatting.
10362
if ($ArgumentList) {

0 commit comments

Comments
 (0)