Pipelines/Read-Settings.ps1

function Invoke-ReadSettings {
    Param(
        [Parameter(Mandatory=$false)]
        [ValidateSet('Local', 'AzureDevOps', 'GithubActions', 'GitLab')]
        [string] $environment = 'Local',
        [string] $version = "",
        [string] $settingsFile = "",
        [string] $settingsFolder = $PSScriptRoot
    )

    function Merge-Hashtables {
        $Output = @{}
        ForEach ($Hashtable in ($Input + $Args)) {
            If ($Hashtable -is [Hashtable]) {
                ForEach ($Key in $Hashtable.Keys) {$Output.$Key = $Hashtable.$Key}
            }
        }
        $Output
    }

    $agentName = ""
    if ($environment -ne 'Local') {
        $agentName = $ENV:AGENT_NAME
    }

    $moduleRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
    $globalsettingsFile = Join-Path $moduleRoot 'AL\Pipelines\default.global.settings.json'
    $globalsettings = Join-Path $moduleRoot 'AL\Pipelines'
    ls $globalsettings
    cat $globalsettingsFile
    if (Test-Path -Path $globalsettingsFile) {
        $globalsettings = Get-Content $globalsettingsFile -Encoding UTF8 -Raw | ConvertFrom-Json -AsHashtable       # Powershell 7.0 only
    }

    if ($settingsFile -eq "") {
        $settingsFile = (Join-Path $settingsFolder "settings.json")
    }

    $settings = (Get-Content $settingsFile -Encoding UTF8 | ConvertFrom-Json -AsHashtable)      # Powershell 7.0 only

    if ($settings.versions) {
        if ("$version" -eq "")  {
            $version = $settings.versions[0].version
            Write-Host "Version not defined, using $version"
        }

        $buildversion = $settings.versions | Where-Object { $_.version -eq $version }
    } else {
        $buildversion = @{}
    }

    $settings = Merge-Hashtables $globalsettings $settings $buildversion

    $pipelineName = "$($settings.Name)-$version"
    Write-Host "Set pipelineName = $pipelineName"

    if ($agentName) {
        $containerName = "$($agentName -replace '[^a-zA-Z0-9---]', '')-$($pipelineName -replace '[^a-zA-Z0-9---]', '')".ToLowerInvariant()
    }
    else {
        $containerName = "$($pipelineName.Replace('.','-') -replace '[^a-zA-Z0-9---]', '')".ToLowerInvariant()
    }
    Write-Host "Set containerName = $containerName"
    if ($environment -eq 'AzureDevOps') {
        Write-Host "##vso[task.setvariable variable=containerName]$containerName"
    }

    $artifact = if ($settings.useArtifactWithBlob) { "$ENV:artifactVersionFromSecrets" } else { $settings.artifact }
    Write-Host "Set artifact = '$artifact'"

    if ($settings.installApps -isnot [array]) {
        $installApps = $settings.installApps.Split(',')
    } else {
        $installApps = $settings.installApps
    }
    if ($settings.previousApps -isnot [array]) {
        $previousApps = $settings.previousApps.Split(',')
    } else {
        $previousApps = $settings.previousApps
    }

    if ($settings.appendSasTokenFromSecrets) {
        $installApps = $installApps | ForEach-Object { if ($_ -like 'https://smartartifacts*') { $_ + $ENV:SASToken } else { $_ } }
        $previousApps = $previousApps | ForEach-Object { if ($_ -like 'https://smartartifacts*') { $_ + $ENV:SASToken } else { $_ } }

        Write-Host "Set modified installApps = '$installApps'"
        Write-Host "Set modified previousApps = '$previousApps'"
    }

    if ($settings.translationFolderBlobPath -ne "") {
        $translationFolderBlobPath = $settings.translationFolderBlobPath
        Write-Host "##vso[task.setvariable variable=translationFolderBlobPath]$translationFolderBlobPath"
    }

    $imageName = ""
    if ($settings.cacheImage -and ("$AgentName" -ne "Hosted Agent" -and "$agentName" -ne "" -and "$AgentName" -notlike "Azure Pipelines*")) {
        $imageName = "bcimage"
    }

    # SO >>
    #if ($previousApps -ne "")
    #{
    # if (Test-Path -Path $previousApps -PathType Container) {
    # $previousApps = Get-ChildItem $previousApps | Where-Object {$_.Name -notlike "Microsoft*.app"} | Foreach-Object {$_.FullName}
    # }
    #}
    # SO <<

    Write-Host "SETTINGS:"
    $settings
}