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
  $dependencies = Get-ALDependency
 .Example
  $dependencies = Get-ALDependency -SourcePath "C:\Temp" -skipVerificatio
#>

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)]
        [securestring] $pfxPassword = $null,
        [switch] $SignApp
    )

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

    if ($SignApp.IsPresent) {
        if ($null -eq $pfxFile -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')) {
            Start-Process -FilePath $CompilerPath -ArgumentList (('/project:"{0}"' -f $SourcePath),('/packagecachepath:"{0}"' -f (Join-Path $SourcePath '.alpackages')),('/assemblyprobingpaths:"{0}"' -f (Join-Path $SourcePath '.netpackages'))) -Wait -NoNewWindow -PassThru
        }
        else {
            Start-Process -FilePath $CompilerPath -ArgumentList (('/project:"{0}"' -f $SourcePath),('/packagecachepath:"{0}"' -f (Join-Path $SourcePath '.alpackages'))) -Wait -NoNewWindow -PassThru
        }
    }
    else {
        Start-Process -FilePath $CompilerPath -ArgumentList (('/project:"{0}"' -f $SourcePath),('/packagecachepath:"{0}"' -f (Join-Path $SourcePath '.alpackages'))) -Wait -NoNewWindow -PassThru
    }

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

    if ($SignApp.IsPresent) {
        Get-ChildItem -Path $SourcePath -Filter '*.app' | ForEach-Object {
            $AppFile = $_.FullName
            $AppFile
            Invoke-SignFile -ContainerName $ContainerName -FilenAme $_.FullName -pfxFile $pfxFile -pfxPassword $pfxPassword
        }
    } else {
        Get-ChildItem -Path $SourcePath -Filter '*.app' | ForEach-Object {
            $AppFile = $_.FullName
            $AppFile
        }
    }

    if (Test-Path (Join-Path $SourcePath '.netpackages')) {
        $session = Get-NavContainerSession $ContainerName
        $ContainerScipt = Invoke-Command -Session $session -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 = $ContainerScipt[0]
        $ServiceTierAddinsExist = $ContainerScipt[1]
        if (!$ServiceTierAddinsExist){
            Copy-Item -Path (Join-Path $SourcePath '.netpackages') -Destination $ServiceTierAddins -Recurse -ToSession $session
        }
    }

    if ($Install.IsPresent) {
        if ($SignApp.IsPresent) {
            Publish-NavContainerApp -containerName $ContainerName -appFile $AppFile -sync -install
        } else {
            Publish-NavContainerApp -containerName $ContainerName -appFile $AppFile -skipVerification -sync -install
        }
    }
}

Export-ModuleMember -Function Invoke-BuildALAppPackage