Invoking functions from nested modules in a script module do not always trigger a module to autoload

前端 未结 2 947
逝去的感伤
逝去的感伤 2021-02-08 18:37

If I create a manifest module with nested modules, exported functions from all nested modules after the first do not appear in the list of available commands and don\'t trigger

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-08 19:18

    Hy Paul, Hy Luken,

    After struggling for a few hours on this topic (with the implicit and declared manifest approach), I opted for the auto generated manifest. In essence, I wrote a ps1 script that generates the manifest, in two steps:

    1. with some 'business' logic, discover what you want to expose
    2. generate the module manifest

    IMHO, Advantage of that approach: I 'fully' understand and master the exposure of the module content.

    below you can find a PS code snippet that follows this approach, hoping it will benefit to other people.

    # module discovery
    $rootModule = "WdCore";
    $modules = Get-ChildItem *.psm1;
    $nestedmodulesNames = $modules | where { $_.BaseName -ne "WdCore"} | % { $_.BaseName } ;
    
    # functions discovery
    $modulesLines = $modules | Get-Content;
    $functionsLines = $modulesLines | where { $_.contains("Function") };
    
    $functionNamePattern = '^Function\s+(?([a-z][0-9|A-Z|-]+))\s?{.*$';
    $functionNames = $functionsLines | where { $_ -match $functionNamePattern } | select { $Matches['name'] } | % { $_.' $Matches[''name''] '};
    
    # generate manifest
    New-ModuleManifest `
        -Path ./WdTools.psd1 -RootModule $rootModule `
        -ModuleVersion '1.0' `
        -NestedModules $nestedmodulesNames `
        -FunctionsToExport $functionNames `
        -Guid '57D7F213-2316-4786-8D8A-3E4B9262B1E5' `
        -Author 'Blaise Braye' `
        -Description 'This module provides working directory tooling' `
        -PowerShellVersion '3.0' -ClrVersion '4.0';
    

提交回复
热议问题