InvokeBuildHelper.psm1

<#
    .SYNOPSIS
        Root module file.

    .DESCRIPTION
        The root module file loads all functions and helpers into the module
        context.
#>


[CmdletBinding()]
param
(
    # Enable debug mode for the module. This will allow to debug the module
    # functions and helpers using breakpoints but will slow down module loading
    # due to the slow dot-sourcing.
    [Parameter(Mandatory = $false)]
    [System.Boolean]
    $DebugModule = $false
)


## Module Core

# Module behavior
Set-StrictMode -Version 'Latest'
$Script:ErrorActionPreference = 'Stop'
$Script:ProgressPreference    = 'SilentlyContinue'


# Module metadata
$Script:PSModulePath    = [System.IO.Path]::GetDirectoryName($PSCommandPath)
$Script:PSModuleName    = [System.IO.Path]::GetFileName($PSCommandPath).Split('.')[0]
$Script:PSModuleVersion = (Import-PowerShellDataFile -Path "$Script:PSModulePath\$Script:PSModuleName.psd1")['ModuleVersion']


## Module Loader

# Get and dot source all functions
Get-ChildItem -Path "$Script:PSModulePath\Helpers", "$Script:PSModulePath\Functions" -Filter '*.ps1' -File -Recurse |
    ForEach-Object {
        if ($DebugModule -or $Env:PWSH_DEBUG_MODULE -eq 'true')
        {
            . $_.FullName
        }
        else
        {
            . ([System.Management.Automation.ScriptBlock]::Create(
                [System.IO.File]::ReadAllText($_.FullName, [System.Text.Encoding]::UTF8)
            ))
        }
    }


## Module Context

# Create an alias to the build script
Set-Alias -Name 'InvokeBuildHelperTasks' -Value "$Script:PSModulePath\Scripts\InvokeBuildHelperTasks.ps1"

# Get all task names
$Script:INVOKE_BUILD_HELPER_TASK_NAMES =
    Get-Content -Path "$Script:PSModulePath\Scripts\InvokeBuildHelperTasks.ps1" |
        Where-Object { $_ -match '^task (?<TaskName>[a-zA-Z]+)' } |
            ForEach-Object { $Matches['TaskName'] }