AL/Build-ALAppPackage.ps1

function Build-ALAppPackage {
    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
    )

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

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

    if (!$DoNotDownloadSymbols.IsPresent) {
        Download-Symbols -SourcePath $SourcePath -ContainerName $ContainerName
    }
    
    if ($CompilerPath -eq '') {
        $CompilerPath = Get-CompilerFromContainer -ContainerName $ContainerName
    }

    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
    }

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

    Get-ChildItem -Path $SourcePath -Filter '*.app' | foreach {
        $AppFile = $_.FullName
        $AppFile
        Sign-File $_.FullName
    }

    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) {
        Publish-NavContainerApp -containerName $ContainerName -appFile $AppFile -sync -install
    }
}

Export-ModuleMember -Function Build-ALAppPackage