Pipelines/Read-Settings.ps1

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

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

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

$settings = (Get-Content $settingsFile | ConvertFrom-Json)      # TODO
if ("$version" -eq "")  {
    $version = $settings.versions[0].version
    Write-Host "Version not defined, using $version"
}

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

if ($buildversion) {
    Write-Host "Set artifact = $($buildVersion.artifact)"
    Set-Variable -Name "artifact" -Value $buildVersion.artifact
}
else {
    throw "Unknown version: $version"
}

$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"
}

"installApps", "previousApps", "installTestApps", "rulesetFile", "appSourceCopMandatoryAffixes", "appSourceCopSupportedCountries", "appFolders", "testFolders", "bcptTestFolders", "memoryLimit", "additionalCountries", "genericImageName", "vaultNameForLocal", "bcContainerHelperVersion", "companyNameForTests", "restoreDbFromBackupUrl", "translationFolderBlobPath" | ForEach-Object {
    $str = ""
    if ($buildversion.PSObject.Properties.Name -eq $_) {
        $str = $buildversion."$_"
    }
    elseif ($settings.PSObject.Properties.Name -eq $_) {
        $str = $settings."$_"
    }
    Write-Host "Set $_ = '$str'"
    Set-Variable -Name $_ -Value "$str"
}

"installTestRunner", "installTestFramework", "installTestLibraries", "installPerformanceToolkit", "enableCodeCop", "enableAppSourceCop", "enablePerTenantExtensionCop", "enableUICop", "enableLinterCop", "doNotRunBcptTests", "useDefaultAppSourceRuleSet", "doNotSignApps", "doNotRunTests", "cacheImage", "CreateRuntimePackages", "useCompilerFolder", "doNotPublishApps", "appendSasTokenFromSecrets", "useArtifactWithBlob" | ForEach-Object {
    $str = "False"
    if ($buildversion.PSObject.Properties.Name -eq $_) {
        $str = $buildversion."$_"
    }
    elseif ($settings.PSObject.Properties.Name -eq $_) {
        $str = $settings."$_"
    }
    Write-Host "Set $_ = $str"
    Set-Variable -Name $_ -Value ($str -eq "True")
}

"copyLibraries" | ForEach-Object {
    $arr = @()
    if ($buildversion.PSObject.Properties.Name -eq $_) {
        $arr = $buildversion."$_"
    }
    elseif ($settings.PSObject.Properties.Name -eq $_) {
        $arr = $settings."$_"
    }
    Write-Host "Set $_ = '$arr'"
    Set-Variable -Name $_ -Value $arr
}

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

if ($appendSasTokenFromSecrets) {
    $modifiedInstallApps = ($installApps -split ',' | ForEach-Object { if ($_ -like 'https://smartartifacts*') { $_ + $ENV:SASToken } else { $_ } }) -join ','
    $modifiedPreviousApps = ($previousApps -split ',' | ForEach-Object { if ($_ -like 'https://smartartifacts*') { $_ + $ENV:SASToken } else { $_ } }) -join ','
} else {
    $modifiedInstallApps = $installApps
    $modifiedPreviousApps = $previousApps
}

Write-Host "Set modified installApps = '$modifiedInstallApps'"
Set-Variable -Name "installApps" -Value "$modifiedInstallApps"
Write-Host "Set modified previousApps = '$modifiedPreviousApps'"
Set-Variable -Name "previousApps" -Value "$modifiedPreviousApps"

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

$imageName = ""
if ($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 <<