Scripts/Load-AllModuleFunctions.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
param([Parameter(mandatory=$true)][string]$ModuleRoot)

<#
        .SYNOPSIS
        Auto loads all functions. Requires that functions have a 1 to 1 mapping to files.
 
        ********************************************************************
        NOTE: This is boiler plate code that can be copied to any new module
        ********************************************************************
#>

$moduleName = [io.path]::GetFileNameWithoutExtension((gci $moduleRoot -Include *.psm1,*psd1 -Recurse -Depth 1 | select -First 1).FullName)
$functionNamesToExport = @()
#All *.ps1 files in these folders will be included in the module
$functionPaths = @("$ModuleRoot\functions\*.ps1")
$excludeFunctionPaths = @('*.Tests.ps1','*.Test.ps1')

foreach($functionPath in $functionPaths) {
    # load all functions from files in the "Functions" folder, by convention, only functions matching file names are exported
    Gci $functionPath -exclude $excludeFunctionPaths -recurse -force  |
        % { 
            Write-Debug "Importing functions from '$($_.FullName)'"
            . $_.FullName;
            $functionName = [io.path]::GetFileNameWithoutExtension($_.FullName)
            #Only export the function if the name matches
            
            if(gci function: | ? { $_.Name -like $functionName -and $_.ModuleName -like $moduleName}) {
                $functionNamesToExport += $functionName
            } else {
                write-verbose "A function for module '$moduleName' with the name '$functionName' was expected to exist the file '$($_.FullName)' but was not found"
            }
        }
}

return $functionNamesToExport