functions/depends/Convert-AlcOutputToDevOpsShort.ps1

<#
 .Synopsis
  Convert Output from AL Compiler to Azure DevOps or GitHub format
 .Description
  Rewritten version of Convert-AlcOutputToDevOps from BcContainerHelper 6.0.19
 .Parameter AlcOutput
  One or more lines of outout from the AL Compiler
 .Parameter DevOpsType
  Specify Azure or GitHub to contol the output format (Default: Azure)
 .Parameter AlProjectPath
  Path to strip from warnings and errors
 .Example
  & .\alc.exe /project:$appProjectFolder /packagecachepath:$appSymbolsFolder /out:$appOutputFile | Convert-AlcOutputToDevOps -AlProjectPath $appProjectFolder
#>

function Convert-AlcOutputToDevOpsShort {
    param (
        $AlcOutput,
        [Parameter()]
        [ValidateSet('Azure', 'GitHub')]
        [string]$DevOpsType = 'Azure',
        [string]$AlProjectPath = ''
    )

    $IsAzureDevOps = $DevOpsType -eq 'Azure'
    $IsGitHub = $DevOpsType -eq 'GitHub'

    $hasError = $false
    $hasWarning = $false

    foreach($CurrLine in $AlcOutput) {
        switch -regex ($CurrLine) { 
            "^(.*)error (\w{2,3}\d{4}): (.*)$"
            {
                switch ($true)
                {
                    $IsAzureDevOps { $Result = "##vso[task.logissue type=error;code=$($Matches[2]);]$($Matches[3])" }
                    $IsGitHub { $Result = "::error::$($Matches[2]) $($Matches[3])" }
                    default { $Result = $CurrLine }
                }

                $hasError = $true
                break
            }
            "^(.*)\((\d+),(\d+)\): error (\w{2,3}\d{4}): (.*)$"
            {
                $AlFilePath = $Matches[1]
                if ($AlFilePath -like "$($AlProjectPath)*") { $AlFilePath = ".\$(ConvertTo-RelativePath -AbsolutePath $AlFilePath -BasePath $AlProjectPath)".Replace('\','/') }
                
                switch ($true)
                {
                    $IsGitHub { $Result = "::error file=$($AlFilePath),line=$($Matches[2]),col=$($Matches[3])::$($Matches[4]) $($Matches[5])" }
                    $IsAzureDevOps { $Result = "##vso[task.logissue type=error;sourcepath=$($AlFilePath);linenumber=$($Matches[2]);columnnumber=$($Matches[3]);code=$($Matches[4]);]$($Matches[5])" }
                    default { $Result = $CurrLine }
                }

                $hasError = $true
                break
            }
            "^(.*)\((\d+),(\d+)\): warning (\w{2,3}\d{4}): (.*)$"
            {
                $AlFilePath = $Matches[1]
                if ($AlFilePath -like "$($AlProjectPath)*") {
                    $AlFilePath = ".\$(ConvertTo-RelativePath -AbsolutePath $AlFilePath -BasePath $AlProjectPath)".Replace('\','/')
                }
                
                switch ($true)
                {
                    $IsGitHub { $Result = "::warning file=$($AlFilePath),line=$($Matches[2]),col=$($Matches[3])::$($Matches[4]) $($Matches[5])" }
                    $IsAzureDevOps { $Result = "##vso[task.logissue type=warning;sourcepath=$($AlFilePath);linenumber=$($Matches[2]);columnnumber=$($Matches[3]);code=$($Matches[4]);]$($Matches[5])" }
                    default { $Result = $CurrLine }
                }

                $hasWarning = $true
                break
            }
            "^warning (\w{2,3}\d{4}):(.*('.*').*|.*)$"
            {
                if ($null -ne $Matches[3]) {
                    $AlFilePath = $Matches[3]
                    if ($AlFilePath -like "$($AlProjectPath)*") { $AlFilePath = ".\$(ConvertTo-RelativePath -AbsolutePath $AlFilePath -BasePath $AlProjectPath)".Replace('\','/') }

                    switch ($true)
                    {
                        $IsAzureDevOps { $Result = "##vso[task.logissue type=warning;sourcepath=$($AlFilePath);$($Matches[1]);]$($Matches[2])" } 
                        $IsGitHub { $Result = "::warning file=$($AlFilePath)::code=$($Matches[1]) $($Matches[2])" }
                        default { $Result = $CurrLine}
                    }
                }
                else
                {
                    switch ($true)
                    {
                        $IsAzureDevOps { $Result = "##vso[task.logissue type=warning;code=$($Matches[1]);]$($Matches[2])" }
                        $IsGitHub { $Result = "::warning::$($Matches[1]) $($Matches[2])" }
                        default { $Result = $CurrLine }
                    }
                }
        
                $hasWarning = $true
                break
            }
            default
            {
                $Result = $CurrLine
                break
            }
        }
        Write-Output $Result
    }

    
    if ($hasError -and $IsAzureDevOps)
    {
        Write-Output "##vso[task.complete result=Failed;]Failed."
    }
    elseif ($hasWarning -and $IsAzureDevOps)
    {
        Write-Output "##vso[task.complete result=SucceededWithIssues;]Succeeded With Issues."
    }
}