tasks/common.tasks.ps1

# Synopsis: Allows build properties to be overriden by environment variables
task ApplyEnvironmentVariableOverrides {

    # dot-source the function to load into the same scope as
    # the InvokeBuild process, otherwise as a module function it
    # won't havea ccess to any of the variables it needs to update
    . $PSScriptRoot/../functions/_Set-VariableFromEnvVar.ps1
    
    $buildEnvVars = Get-ChildItem env:BUILDVAR_*
    foreach ($buildEnvVar in $buildEnvVars) {
        # strip the 'BUILDVAR_' prefix to leave the variable name to be overridden
        $varName = $buildEnvVar.Name -replace "^BUILDVAR_",""

        $res = Set-VariableFromEnvVar -VariableName $varName -EnvironmentVariableName $buildEnvVar.Name

        if ($res) {
            Write-Build Yellow "Overriding '$varName' from environment variable [Value=$((Get-Item variable:/$varName).Value)] [Type=$((Get-Item variable:/$varName).Value.GetType().Name)]"
        }
    }
}