AL/Invoke-CompileApp.ps1

<#
 .Synopsis
  Compiles the app
 .Description
  Compiles the app for the current project and optionally updates the build number of the app and installs it
 .Parameter ContainerName
  Token to access Azure DevOps. Can be provided in settings.json
 .Parameter pfxFile
  Path to the code sign certificate
 .Parameter pfxPassword
  Password for the code sign certificate
 .Parameter SourcePath
  Path to the current project. If not defined, the current folder will be used
 .Parameter OutputPath
  Path where the compiled app will be saved
 .Parameter BuildNumber
  Defines the new build number that should be used to update the app. If not specified, the build numbers revision will be updated (major.minor.revision.build)
 .Parameter DoNotUpdateVersion
  Add this switch to not update the build number of the app
 .Parameter DoNotInstallApp
  Add this switch to skip the install of the compiled app
 .Example
  Invoke-CompileApp -pfxFile "C:\pfx.pfx" -pfxPassword (ConvertTo-SecureString "P@ssword1" -AsPlainText -Force) -OutputPath "C:\Output" -DoNotUpdateVersion
#>

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,
        [switch] $DoNotInstallApp
    )

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

    try {
    Remove-Item (Join-Path $SourcePath '.altemplates') -Recurse -Force
    Remove-Item (Join-Path $SourcePath '.altestrunner') -Recurse -Force
    }
    catch {
        Write-Output "Did not remove folders"
    }

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

    # update dependencies
    Remove-ALTestDependencyList -sourcePath $SourcePath

    #set the target, if applicable
    if ((Get-EnvironmentKeyValue -SourcePath $SourcePath -KeyName target) -ne "") {
        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:(!$DoNotInstallApp.IsPresent) -pfxFile $pfxFile -pfxPassword $pfxPassword)){
        exit
    }
    Copy-Item "$SourcePath/*.app" "$OutputPath/app"
    Copy-Item "$SourcePath/*.app" "$TestPath/.alpackages"

    #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-AllSymbol -SourcePath $TestPath -ContainerName $ContainerName
    }
    else{
        # for 2018, it doesn't properly work to have "test" set, since it updates the symbols when importing the objects (due to possible base object compiles)
        if ($platform.Major -gt 11) {
            Set-AppKeyValue -SourcePath $TestPath -KeyName 'test' -KeyValue (Get-AppKeyValue -KeyName 'application')
            Get-AllSymbol -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:(!$DoNotInstallApp.IsPresent) -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