AppHandling/Convert-ALCOutputToAzureDevOps.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<#
 .Synopsis
  Convert Output from AL Compiler and output to host in Azure DevOps format
 .Description
  This function is a contribution from Microsoft MVP Kamil Sacek, read about it here:
  https://dynamicsuser.net/nav/b/kine/posts/alc-exe-output-formatting-for-tfs-vsts
 .Parameter AlcOutput
  One or more lines of outout from the AL Compiler
 .Parameter Failon
  Specify if you want the AzureDevOps output to fail on Error or Warning
 .Example
  Compile-AppInNavContainer -containerName test -credential $credential -appProjectFolder "C:\Users\freddyk\Documents\AL\Test" -AzureDevOps
 .Example
  & .\alc.exe /project:$appProjectFolder /packagecachepath:$appSymbolsFolder /out:$appOutputFile | Convert-AlcOutputToAzureDevOps
#>

Function Convert-AlcOutputToAzureDevOps {
    Param (
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
        $AlcOutput,
        [Parameter(Position=1)]
        [ValidateSet('none','error','warning')]
        [string] $FailOn
    )

    Process {
        $hasError = $false
        $hasWarning = $false

        foreach($line in $AlcOutput) {
            switch -regex ($line) {
                "^warning (\w{2}\d{4}):(.*('.*').*|.*)$" {
                    if (Test-Path $Matches[3]) {
                        Write-Host "##vso[task.logissue type=warning;sourcepath=$($Matches[3]);code=$($Matches[1]);]$($Matches[2])"
                    } else {
                        Write-Host "##vso[task.logissue type=warning;code=$($Matches[1]);]$($Matches[2])"
                    }
            
                    $hasWarning = $true
                }
                "^(.*)\((\d+),(\d+)\): error (\w{2,3}\d{4}): (.*)$"
                #Objects\codeunit\Cod50130.name.al(62,30): error AL0118: The name '"Parent Object"' does not exist in the current context
                {
                    Write-Host "##vso[task.logissue type=error;sourcepath=$($Matches[1]);linenumber=$($Matches[2]);columnnumber=$($Matches[3]);code=$($Matches[4]);]$($Matches[5])"
                    $hasError = $true
                }
                "^(.*)error (\w{2}\d{4}): (.*)$"
                #error AL0999: Internal error: System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException
                {
                    Write-Host "##vso[task.logissue type=error;code=$($Matches[2]);]$($Matches[3])"
                    $hasError = $true
                }
                "^(.*)\((\d+),(\d+)\): warning (\w{2}\d{4}): (.*)$"
                #Prepared for unified warning format
                #Objects\codeunit\Cod50130.name.al(62,30): warning AL0118: The name '"Parent Object"' does not exist in the current context
                {
                    Write-Host "##vso[task.logissue type=warning;sourcepath=$($Matches[1]);linenumber=$($Matches[2]);columnnumber=$($Matches[3]);code=$($Matches[4]);]$($Matches[5])"
                    $hasWarning = $true
                }
                default {
                    Write-Host $line
                }
            }
        }

        if (($FailOn -eq 'error' -and $hasError) -or ($FailOn -eq 'warning' -and ($hasError -or $hasWarning))) {
            Write-Host "##vso[task.complete result=Failed;]Failed."
        }
    }
}
Export-ModuleMember -Function Convert-AlcOutputToAzureDevOps