Build/Run-ALBuildProcess.ps1

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

    Import-Module bccontainerhelper
    Set-Location $SourcePath

    Write-Host 'Testing app json file'
    Test-AppJsonFile -BuildSourceBranch $BuildSourceBranch

    if (($BuildSourceBranch.Contains('feature')) -or ($BuildSourceBranch.Contains('bug'))) {
        if (!(Test-TranslationIsComplete -SurpressError)) {
            Write-Host 'Translation is incomplete - continuing build as this is a bug or a feature branch'
        }
        else {
            Write-Host 'Translation is complete'
        }
    }
    else {
        Write-Host 'Testing translation is complete'
        Test-TranslationIsComplete
    }

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

    $ContainerName = "Build$BuildNumber"

    $NewContainerParams = @{
        ContainerName = $ContainerName
    }

    [version]$Platform = Get-AppKeyValue -KeyName 'platform'
    if ($BuildSourceBranch.Contains('build')) {
        if ($BuildSourceBranch -eq 'refs/heads/build/insider') {
            $NewContainerParams.Add('SecondToLastMajor', $true)
        }

        $NewContainerParams.Add('storageAccount', 'bcinsider')
    }
    else {
        $BuildVersion = $Platform.Major
    }

    if ($null -ne (Get-EnvironmentKeyValue -KeyName 'buildVersion')) {
        $BuildVersion = Get-EnvironmentKeyValue -KeyName 'buildVersion'
    }
    [version]$BCFallPlatform = '16.0.0.0'
    $UseAppSourceCop = $Platform -ge $BCFallPlatform
    if (!$UseAppSourceCop){
        $NewContainerParams.Add('ContainerType', 'OnPrem')
    }
    $NewContainerParams.Add('version', $BuildVersion)
    Write-Host "Using version $BuildVersion to create container"
    New-NavContainer2 @NewContainerParams

    Get-ALDependencies -ContainerName $ContainerName -Install

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

    if ($UseAppSourceCop) {
        #if specified in environment.json find the previous version of the app to test
        if (Get-EnvironmentKeyValue -KeyName 'previousVersion') {
            $PreviousAppVersion = Download-AppPackage -AppVersion (Get-EnvironmentKeyValue -KeyName 'previousVersion')
        }
        else {
            $PreviousAppVersion = Download-AppPackage
        }

        if ($null -ne $PreviousAppVersion) {
            Set-AppSourceCopKeyValue -KeyName version -KeyValue $PreviousAppVersion
        }
    }
    #set preprocessorSymbols from environment.json
    Add-PreprocessorSymbolsToAppJson

    #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 ''
    $ExtensionIdToTest = ''

    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 (!$UseAppSourceCop){
            if (!(Build-ALAppPackage -SourcePath $SourcePath -ContainerName $ContainerName -CompilerPath $CompilerPath -DoNotDownloadSymbols -Install)) {
                exit
            }
        }
        else {
            if (!(Build-ALAppPackage -SourcePath $SourcePath -ContainerName $ContainerName -CompilerPath $CompilerPath -DoNotDownloadSymbols -Install -UseAppSourceCop)) {
                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
        [version]$BCFallPlatform = '15.0.0.0'
        [version]$platform = Get-AppKeyValue -SourcePath $TestPath -KeyName 'platform'
        if ($platform -ge $BCFallPlatform) {
            Add-TestAppsToAppJson -SourcePath $TestPath
            Download-Symbols -SourcePath $TestPath -ContainerName $ContainerName
        }
        else {
            Set-AppKeyValue -SourcePath $TestPath -KeyName 'test'-KeyValue (Get-AppKeyValue -KeyName 'application')
            Download-Symbols -SourcePath $TestPath -ContainerName $ContainerName -includeTestSymbols
        }

        [version]$runtime = Get-AppKeyValue -SourcePath $TestPath -KeyName 'runtime'
        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) `
            -Runtime $runtime

        #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 }
        
        #remove version from AppSourceCop.json
        if ($null -ne $PreviousAppVersion) {
            Set-AppSourceCopKeyValue -SourcePath $TestPath -KeyName version -KeyValue ''
        }

        if ($UseAppSourceCop) {
            Build-ALAppPackage -SourcePath $TestPath -ContainerName $ContainerName -CompilerPath $CompilerPath -DoNotDownloadSymbols -Install -UseAppSourceCop
        }
        else {
            Build-ALAppPackage -SourcePath $TestPath -ContainerName $ContainerName -CompilerPath $CompilerPath -DoNotDownloadSymbols -Install
        }

        $ExtensionIdToTest = Get-AppKeyValue -SourcePath $TestPath -KeyName id
        Copy-Item "$TestPath/*.app" $OutputPath
        Remove-Item $TestPath -Recurse
    }
    else {
        if ($UseAppSourceCop) {
            Build-ALAppPackage -SourcePath $SourcePath -ContainerName $ContainerName -CompilerPath $CompilerPath -DoNotDownloadSymbols -Install -UseAppSourceCop
        }
        else {
            Build-ALAppPackage -SourcePath $SourcePath -ContainerName $ContainerName -CompilerPath $CompilerPath -DoNotDownloadSymbols -Install 
        }
        Copy-Item "$SourcePath/*.app" $OutputPath
        $ExtensionIdToTest = Get-AppKeyValue -SourcePath $SourcePath -KeyName id
    }

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

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

        Run-BCTests -ContainerName $ContainerName -ResultPath $ResultPath  -ExtensionId $ExtensionIdToTest
    }
}

Export-ModuleMember -Function Run-ALBuildProcess