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,
        [Parameter(Mandatory = $false)]
        [switch]$UseAppSourceCop
    )

    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
    }

    $BinPath = Split-Path $CompilerPath -Parent

    if ($null -ne (Get-EnvironmentKeyValue -SourcePath $SourcePath -KeyName 'errorOnWarning')) {
        $ErrorOnWarning = $true
    }

    $ArgumentList = ('/project:"{0}"' -f $SourcePath), ('/packagecachepath:"{0}"' -f (Join-Path $SourcePath '.alpackages'))
    if (Test-Path (Join-Path $SourcePath '.netpackages')) {
        $ArgumentList += ('/assemblyProbingPaths:"{0}"' -f (Join-Path $SourcePath '.netpackages'))
    }

    if ($ErrorOnWarning) {
        $ArgumentList += ('/errorlog:"{0}"' -f (Join-Path $SourcePath 'errorlog.json'))
    }

    if ($UseAppSourceCop) {
        $ArgumentList += ('/analyzer:"{0}"' -f (Join-Path $BinPath 'Analyzers\Microsoft.Dynamics.Nav.AppSourceCop.dll'))
    }

    Get-ChildItem -Path $SourcePath -Filter '*.ruleset.json' | ForEach-Object {
        Write-Host "Using ruleset $($_.FullName)"
        $ArgumentList += ('/ruleset:"{0}"' -f $_.FullName)
    }

    Start-Process -FilePath $CompilerPath -ArgumentList $ArgumentList -Wait -NoNewWindow -PassThru

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

    if($UseAppSourceCop){
        if ($ErrorOnWarning) {
            if (!(Test-ErrorLog (Join-Path $SourcePath 'errorlog.json'))) {
                Write-Error 'Code analyser warnings exists'
                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
    }
}

function Test-ErrorLog {
    Param(
        [Parameter(Mandatory = $true)]
        $ErrorLogPath
    )

    try {
        $ErrorLog = Get-Content $ErrorLogPath | ConvertFrom-Json
    }
    catch {
        $UnableToReadErorrLog = $true
    }

    if ($UnableToReadErorrLog) {
        try {
            $ErrorLog = (Get-Content $ErrorLogPath).Replace('\', '\\') | ConvertFrom-Json
            $UnableToReadErorrLog = $false
        }
        catch {
            Write-Host 'Unable to read compiler error log' -ForegroundColor Yellow
            exit $true;
        }    
    }

    foreach ($Error in $ErrorLog.issues | Where-Object { $_.fullMessage -like '*marked for removal*' }) {
        Write-Host $Error.fullMessage -ForegroundColor Red
        $ErrorsExists = $true
    }

    foreach ($Error in $ErrorLog.issues | Where-Object { $_.fullMessage -like '*will become an error*' }) {
        Write-Host $Error.fullMessage -ForegroundColor Red
        $ErrorsExists = $true
    }

    return !$ErrorsExists
}

Export-ModuleMember -Function Build-ALAppPackage