Build/Run-ALBuildProcess.ps1

function Run-ALBuildProcess {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$SourcePath,
        [Parameter(Mandatory=$true)]
        [string]$OutputPath,
        [Parameter(Mandatory=$true)]
        $BuildNumber,
        [Parameter(Mandatory=$false)]
        [bool]$SplitApps = $false
    )

    Import-Module navcontainerhelper
    Set-Location $SourcePath

    Write-Host 'Testing translation is complete'
    Test-TranslationIsComplete

    Write-Host 'Testing table permissions exist'
    Test-TablePermissionsExist

    $ContainerName = "Build$BuildNumber"

    if ((Get-ImageNameForRepo) -eq '') {
        error "Image name is not set for $SourcePath"
    }

    New-Container -ContainerName $ContainerName

    Get-ALDependencies -ContainerName $ContainerName -Install

    $CompilerPath = Get-CompilerFromContainer -ContainerName $ContainerName
    if ((Get-AppKeyValue -SourcePath $SourcePath -KeyName 'test') -ne '') {
        Download-Symbols -SourcePath $SourcePath -ContainerName $ContainerName -includeTestSymbols
    }
    else {
        Download-Symbols -SourcePath $SourcePath -ContainerName $ContainerName
    }

    #update the app version with the build number (actually store it in the Revision - the last element of the version no)
    $AppVersion = [System.Version]::new((Get-AppKeyValue -KeyName 'version'))

    Set-AppKeyValue -KeyName 'version' -KeyValue ('{0}.{1}.{2}.{3}' -f $AppVersion.Major, $AppVersion.Minor, $AppVersion.Build, $BuildNumber)
    Set-AppKeyValue -KeyName test -KeyValue ''

    if ($SplitApps) {
        #build the app without the tests
        $TestPath = Create-TempDirectory
        Copy-Item "$SourcePath/*" $TestPath -Recurse

        #set the target, if applicable
        if ($null -ne (Get-EnvironmentKeyValue -SourcePath $SourcePath -KeyName target)) {
            Set-AppKeyValue -SourcePath $SourcePath -KeyName target -KeyValue (Get-EnvironmentKeyValue -SourcePath $SourcePath -KeyName target)
        }

        Remove-Item (Join-Path $SourcePath 'Tests') -Recurse -Force

        if (!(Build-ALAppPackage -SourcePath $SourcePath -ContainerName $ContainerName -CompilerPath $CompilerPath -DoNotDownloadSymbols -Install)){
            exit
        }
        Copy-Item "$SourcePath/*.app" $OutputPath
        Copy-Item "$SourcePath/*.app" "$TestPath/.alpackages"

        Write-Host $SourcePath

        #build the test app
        $TestAppId = Get-EnvironmentKeyValue -SourcePath $SourcePath -KeyName 'testappid'
        if($null -eq $TestAppId) {
            $TestAppId = ([Guid]::NewGuid().Guid)
        }

        Set-AppKeyValue -SourcePath $TestPath -KeyName id -KeyValue $TestAppId
        Set-AppKeyValue -SourcePath $TestPath -KeyName name -KeyValue ('{0} Tests' -f (Get-AppKeyValue -SourcePath $TestPath -KeyName name))

        #add dependency on test symbols for test app
        Set-AppKeyValue -SourcePath $TestPath -KeyName 'test'-KeyValue (Get-AppKeyValue -KeyName 'application')
        Download-Symbols -SourcePath $TestPath -ContainerName $ContainerName -includeTestSymbols

        New-ALAppDependency -SourcePath $TestPath `
                            -Id (Get-AppKeyValue -SourcePath $SourcePath -KeyName id) `
                            -Name (Get-AppKeyValue -SourcePath $SourcePath -KeyName name) `
                            -Publisher (Get-AppKeyValue -SourcePath $SourcePath -KeyName publisher) `
                            -Version (Get-AppKeyValue -SourcePath $SourcePath -KeyName version)

        #remove all directories from the test app apart from those beginning with . and the Logo and Tests folders
        Get-ChildItem $TestPath -Directory | where Name -NotLike '.*' | where Name -NotLike Tests | where Name -NotLike Logo | ForEach-Object {Remove-Item $_.FullName -Force -Recurse}
    
        #remove dotnet.al file for test app
        Get-ChildItem $TestPath | where Name -Like 'dotnet.al' | ForEach-Object {Remove-Item $_.FullName -Force -Recurse}

        if ((Get-EnvironmentKeyValue -KeyName 'tests') -eq 'skip') {
            Build-ALAppPackage -SourcePath $TestPath -ContainerName $ContainerName -CompilerPath $CompilerPath -DoNotDownloadSymbols
        }
        else {
            Build-ALAppPackage -SourcePath $TestPath -ContainerName $ContainerName -CompilerPath $CompilerPath -DoNotDownloadSymbols -Install
        }

        Copy-Item "$TestPath/*.app" $OutputPath
        Remove-Item $TestPath -Recurse
    }
    else {
        if (!(Build-ALAppPackage -SourcePath $SourcePath -ContainerName $ContainerName -CompilerPath $CompilerPath -DoNotDownloadSymbols -Install)){
            exit
        }
        Copy-Item "$SourcePath/*.app" $OutputPath  
    }

    if ((Get-EnvironmentKeyValue -KeyName 'tests') -ne 'skip') {
        Create-EmptyDirectory "C:\Testing\$BuildNumber"

        $ResultPath = "C:\Testing\$BuildNumber\Results.xml"

        Run-BCTests -ContainerName $ContainerName -ResultPath $ResultPath
     }
 }

 Export-ModuleMember -Function Run-ALBuildProcess