Build/Run-ALBuildProcess.ps1

Param(
    [Parameter(Mandatory=$true,Position=1)]
    [string]$SourcePath,
    [Parameter(Mandatory=$true,Position=2)]
    [string]$OutputPath,
    [Parameter(Mandatory=$true,Position=3)]
    $BuildNumber,
    [Parameter(Mandatory=$false,Position=4)]
    [bool]$SplitApps = $false
)

Import-Module 'C:\TFS\TFS Tools\Tecman.Tfs.Tools.psm1'
Import-Module navcontainerhelper
cd $SourcePath

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
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 ((Get-EnvironmentKeyValue -SourcePath $SourcePath -KeyName target) -ne $null) {
        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($TestAppId -eq $null) {
        $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))

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