From 404cbf8aa4f4901e833cde1095f16449515aba64 Mon Sep 17 00:00:00 2001 From: StartAutomating Date: Sat, 1 Jun 2024 21:45:04 +0000 Subject: [PATCH] feat: Use-Module ( Fixes #1130 ) Simplifying by narrowing scope. --- Commands/Module/Use-Module.ps1 | 51 ++++------------------------------ 1 file changed, 5 insertions(+), 46 deletions(-) diff --git a/Commands/Module/Use-Module.ps1 b/Commands/Module/Use-Module.ps1 index 264fa7c86..ca1ffbd07 100644 --- a/Commands/Module/Use-Module.ps1 +++ b/Commands/Module/Use-Module.ps1 @@ -6,17 +6,7 @@ function Use-Module { .SYNOPSIS Uses a module. .DESCRIPTION - Uses a module. - - This can either: - - * If present, run a module's `Use` function, method, or script property. - * Otherwise, run a script block in the module's context. - - * Run a module's `Use` function, if present - * Call a module's `Use` method, if present - - In all cases, the script block should be run in the module's context, using dot-sourcing. + Uses a module's context to run a `[ScriptBlock]` (with dot-sourcing). .EXAMPLE Get-Module PipeScript | Use-Module -ScriptBlock { $myInvocation.MyCommand.ScriptBlock.Module } #> @@ -61,43 +51,12 @@ function Use-Module { Get-Module $Name | Select-Object -First 1 } - # Return if there is no module context. - if (-not $moduleContext) { return } - # Get the use commands. - $useCommands = $moduleContent.ExportedCommands[@( - "Use-$($moduleContext.Name)", - "Use.$($moduleContext.Name)" - "$($moduleContext.Name).Use" - )] - - # Get the use method. - $useMethod = $moduleContext.psobject.methods["Use"] - - $ToRun = - # If we have a method - if ($useMethod) - { - $useMethod.Script # use it - # (and pass the script block as an argument) - $ArgumentList = @($ScriptBlock) + @($ArgumentList) - } - # If we have any use commands, use the first one - elseif ($useCommands -ne $null) - { - @($useCommands -ne $null)[0] - # (and pass the script block as an argument) - $ArgumentList = @($ScriptBlock) + @($ArgumentList) - } - else - { - # Otherwise, use the script block - $ScriptBlock - } - - # We're running in the module context, and now we know what we want `$toRun`. + # We're running in the module context, $runningIn = $moduleContext - + # and we want `$toRun` the ScriptBlock. + $ToRun = $ScriptBlock + # The rest of the code is tedium. # If there were arguments and parameters, pass them both with splatting. if ($ArgumentList) {