Templates/Module.psm1

#region load private and public functions
# Scripts are loaded from Private and Public and all their subfolders (for example Public/Generated/Functions and
# Private/Generated from New-FunctionsFromSwagger), except Pester tests (*.Tests.ps1 and Tests folders) and
# Public/Generated/FunctionCustomizations.ps1 (settings read by New-FunctionsFromSwagger, not module code).
$ScriptFilter = {
    $RelativePath = $_.FullName.Substring($PSScriptRoot.Length)
    $_.Name -notlike '*.Tests.ps1' -and $_.Name -ne 'FunctionCustomizations.ps1' -and
    @($RelativePath -split '[\\/]' | Where-Object { $_ -eq 'Tests' }).Count -eq 0
}
$Private = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Private') -Filter '*.ps1' -File -Recurse -ErrorAction SilentlyContinue |
        Where-Object $ScriptFilter | Sort-Object -Property FullName)
$Public = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Public') -Filter '*.ps1' -File -Recurse -ErrorAction SilentlyContinue |
        Where-Object $ScriptFilter | Sort-Object -Property FullName)

foreach ($File in @($Private + $Public)) {
    try {
        . $File.FullName
    }
    catch {
        Write-Error -Message "Failed to import '$($File.FullName)': $_"
    }
}
#endregion

#region optional module settings from the user's config folder (see Config.ps1)
#. (Join-Path -Path $PSScriptRoot -ChildPath 'Config.ps1')
#endregion

#region module config, load telemetry and update check (never blocks import)
try {
    $CurrentConfig = Get-ModuleConfig -CommandPath $PSCommandPath -ErrorAction Stop
    Invoke-TelemetryCollection -ModuleName $CurrentConfig.ModuleName -ModuleVersion $CurrentConfig.ModuleVersion -CommandName 'Import-Module' -ExecutionID ([guid]::NewGuid().ToString()) -Stage 'Module-Load'
    if ($CurrentConfig.UpdateWarning -eq $true) {
        $null = Get-ModuleStatus -ShowMessage -ModuleName $CurrentConfig.ModuleName -ModulePath $CurrentConfig.ModulePath -CacheHours $CurrentConfig.UpdateCheckIntervalHours
    }
}
catch {
    Write-Warning "###TEMPLATE_MODULE_NAME configuration could not be loaded; defaults will be used. $($_.Exception.Message)"
}
#endregion

Export-ModuleMember -Function $Public.BaseName