AL/Invoke-CompileApp.ps1

function Invoke-CompileApp {
    Param(
        [Parameter(Mandatory=$false)]
        [string] $ContainerName,
        [Parameter(Mandatory=$true)]
        [securestring] $pfxFile,
        [Parameter(Mandatory=$true)]
        [securestring] $pfxPassword,
        [Parameter(Mandatory=$false)]
        [string] $SourcePath = (Get-Location),
        [Parameter(Mandatory=$true)]
        [string] $OutputPath,
        [Parameter(Mandatory=$false)]
        [string] $BuildNumber = "",
        [switch] $DoNotUpdateVersion
    )

    if ($null -eq $ContainerName -or $ContainerName -eq "") {
        $ContainerName = (Get-EnvironmentKeyValue -KeyName 'name')
    }

    if (!(Test-Path $OutputPath)) {
        New-EmptyDirectory $OutputPath
    }
    if (!(Test-Path (Join-Path $OutputPath "app"))) {
        New-EmptyDirectory (Join-Path $OutputPath "app")
    }
    if (!(Test-Path (Join-Path $OutputPath "runtimeapp"))) {
        New-EmptyDirectory (Join-Path $OutputPath "runtimeapp")
    }
    if (!(Test-Path (Join-Path $OutputPath "testapp"))) {
        New-EmptyDirectory (Join-Path $OutputPath "testapp")
    }
    if (!(Test-Path (Join-Path $OutputPath "tests"))) {
        New-EmptyDirectory (Join-Path $OutputPath "tests")
    }

    $CompilerPath = Get-CompilerFromContainer -ContainerName $ContainerName
    Get-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)
    if (!$DoNotUpdateVersion.IsPresent) {
        if ($BuildNumber -eq "") {
            throw "BuildNumber missing"
        }
        $AppVersion = [System.Version]::new((Get-AppKeyValue -SourcePath $SourcePath -KeyName 'version'))
        Set-AppKeyValue -SourcePath $SourcePath -KeyName 'version' -KeyValue ('{0}.{1}.{2}.{3}' -f $AppVersion.Major, $AppVersion.Minor, $AppVersion.Build, $BuildNumber)
    }

    # update dependencies
    Remove-ALTestDependencies -sourcePath $SourcePath

    # first compile the app only (remove tests)
    $TestPath = New-TempDirectory
    Copy-Item "$SourcePath/*" $TestPath -Recurse

    #set the target, if applicable
    if ($null -ne (Get-EnvironmentKeyValue -SourcePath $SourcePath -KeyName target)) {
        Set-AppKeyValue -SourcePath $TestPath -KeyName target -KeyValue (Get-EnvironmentKeyValue -SourcePath $SourcePath -KeyName target)
    }
    
    Remove-Item (Join-Path $SourcePath 'Tests') -Recurse -Force

    if (!(Invoke-BuildALAppPackage -SourcePath $SourcePath -ContainerName $ContainerName -CompilerPath $CompilerPath -SignApp -DoNotDownloadSymbols -Install -pfxFile $pfxFile -pfxPassword $pfxPassword)){
        exit
    }
    Copy-Item "$SourcePath/*.app" "$OutputPath/app"
    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))
    Set-AppKeyValue -SourcePath $TestPath -KeyName screenshots ''

    #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
        Get-Symbols -SourcePath $TestPath -ContainerName $ContainerName
    }
    else{
        Set-AppKeyValue -SourcePath $TestPath -KeyName 'test'-KeyValue (Get-AppKeyValue -KeyName 'application')
        Get-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-Object Name -NotLike '.*' | Where-Object Name -NotLike Tests | Where-Object Name -NotLike *Logo* | ForEach-Object {Remove-Item $_.FullName -Force -Recurse}
    
    #remove dotnet.al file for test app
    Get-ChildItem $TestPath | Where-Object Name -Like 'dotnet.al' | ForEach-Object {Remove-Item $_.FullName -Force -Recurse}

    if ((Get-EnvironmentKeyValue -KeyName 'tests') -eq 'skip') {
        Invoke-BuildALAppPackage -SourcePath $TestPath -ContainerName $ContainerName -CompilerPath $CompilerPath -DoNotDownloadSymbols -pfxFile $pfxFile -pfxPassword $pfxPassword
    }
    else {
        Invoke-BuildALAppPackage -SourcePath $TestPath -ContainerName $ContainerName -CompilerPath $CompilerPath -DoNotDownloadSymbols -Install -pfxFile $pfxFile -pfxPassword $pfxPassword
    }

    Copy-Item "$TestPath/*.app" (Join-Path $OutputPath "testapp")
    Remove-Item $TestPath -Recurse

    if ($CompilerPath -ne "") {
        Remove-Item (($CompilerPath -split '\\')[0..(($CompilerPath -split '\\').count -4)] -join '\') -Recurse
    }
}
Export-ModuleMember Invoke-CompileApp