AL/Invoke-BuildALAppPackage.ps1

<#
 .Synopsis
  Builds the app for the current project
 .Description
  Builds the app, including the test app for the current project. It also installs the apps, if defined
 .Parameter SourcePath
  Path to the current project
 .Parameter ContainerName
  If blank, the container name will be retrieved from settings.json
 .Parameter Install
  Add this switch to install the apps
 .Parameter CompilerPath
  Optional path to the AL compiler. If path is not defined, it will download the compiler from the container
 .Parameter DoNotDownloadSymbols
  Add this switch to skip the downloading of new symbols before compiling
 .Parameter pfxFile
  The path to the code sign certificate to sign the apps
 .Parameter pfxPassword
  The password for the code sign certificate
 .Parameter SignApp
  Add this switch to sign the apps
 .Example
  Invoke-BuildALAppPackage
#>

function Invoke-BuildALAppPackage {
    Param(
        [Parameter(Mandatory=$false)]
        $SourcePath = (Get-Location),
        [Parameter(Mandatory=$false)]
        $ContainerName = (Split-Path $SourcePath -Leaf),
        [switch]$Install,
        [Parameter(Mandatory=$false)]
        $CompilerPath = '',
        [Parameter(Mandatory=$false)]
        [switch]$DoNotDownloadSymbols,
        [Parameter(Mandatory=$false)]
        [securestring] $pfxFile = $null,
        [Parameter(Mandatory=$false)]
        [string] $pfxCertificate = $null,
        [Parameter(Mandatory=$false)]
        [securestring] $pfxPassword = $null,
        [switch] $SignApp
    )

    if (!(Get-IsALRepo $SourcePath)) {
        "$SourcePath is not an AL repository"
    }

    if ($SignApp.IsPresent) {
        if (($null -eq $pfxFile -and $null -eq $pfxCertificate) -or $null -eq $pfxPassword) {
            throw "Missing Sign File parameters"
        }
    }

    Remove-Item -Path "$SourcePath/*.app" -Force

    if (!$DoNotDownloadSymbols.IsPresent) {
        Write-Output "Downloading Symbols"
        Get-AllSymbol -SourcePath $SourcePath -ContainerName $ContainerName -includeTestSymbols
    }

    if ($CompilerPath -eq '') {
        $CompilerPath = Get-CompilerFromContainer -ContainerName $ContainerName
    }

    [version]$platform = Get-AppKeyValue -SourcePath $SourcePath -KeyName 'platform'
    if ($platform.Major -ge 13) {
        if (Test-Path (Join-Path $SourcePath '.netpackages')) {
            $process = (Start-Process -FilePath $CompilerPath -ArgumentList ("/project:.","/packagecachepath:.\.alpackages","/assemblyprobingpaths:.\.netpackages") -WorkingDirectory "$SourcePath" -Wait -NoNewWindow -PassThru)
        }
        else {
            $process = (Start-Process -FilePath $CompilerPath -ArgumentList ("/project:.","/packagecachepath:.\.alpackages") -WorkingDirectory "$SourcePath" -Wait -NoNewWindow -PassThru)
        }
    }
    else {
        $process = (Start-Process -FilePath $CompilerPath -ArgumentList ("/project:.","/packagecachepath:.\.alpackages") -WorkingDirectory "$SourcePath" -Wait -NoNewWindow -PassThru)
    }

    if ($process.ExitCode -ne 0) {
        Write-Error "Error during compilation"
    }

    if (!(Get-ChildItem -Path $SourcePath -Filter '*.app')){
        Write-Error 'App not created'
    }

    if ($SignApp.IsPresent) {
        $SignParameters = @{
            pfxPassword = $pfxPassword
        }
        if ($null -ne $pfxFile) {
            $SignParameters.Add('pfxFile', $pfxFile)
        }
        else {
            $SignParameters.Add('pfxCertificate', $pfxCertificate)
        }

        Get-ChildItem -Path $SourcePath -Filter '*.app' | ForEach-Object {
            $AppFile = $_.FullName
            $AppFile
            Invoke-SignFile -ContainerName $ContainerName -FilenAme $_.FullName @SignParameters
        }
    } else {
        Get-ChildItem -Path $SourcePath -Filter '*.app' | ForEach-Object {
            $AppFile = $_.FullName
            $AppFile
        }
    }

    if (Test-Path (Join-Path $SourcePath '.netpackages')) {
        $ContainerScript = Invoke-ScriptInBCContainer -containerName $ContainerName -scriptblock {
                                Join-Path -ChildPath "\Service\Add-ins" -Path (Join-Path -ChildPath (Get-ChildItem -Path "C:\Program Files\Microsoft Dynamics NAV\")[0] -Path "C:\Program Files\Microsoft Dynamics NAV\")
                                Test-Path (Join-Path -Path (Join-Path -ChildPath "\Service\Add-ins" -Path (Join-Path -ChildPath (Get-ChildItem -Path "C:\Program Files\Microsoft Dynamics NAV\")[0] -Path "C:\Program Files\Microsoft Dynamics NAV\")) -ChildPath '.netpackages')
                            }
        $ServiceTierAddins = $ContainerScript[0]
        $ServiceTierAddinsExist = $ContainerScript[1]
        if (!$ServiceTierAddinsExist){
            $session = Get-BcContainerSession -ContainerName $ContainerName -silent
            Copy-Item -Path (Join-Path $SourcePath '.netpackages') -Destination $ServiceTierAddins -Recurse -ToSession $session | Out-Null
        }
    }

    try {
        if ($Install.IsPresent) {
            if ($SignApp.IsPresent) {
                Publish-BcContainerApp -containerName $ContainerName -appFile $AppFile -sync -install
            } else {
                Publish-BcContainerApp -containerName $ContainerName -appFile $AppFile -skipVerification -sync -install
            }
        }
    }
    catch {
        throw "App could not be published"
    }
}