Skip to content

Commit 710d5e8

Browse files
Add DevOps logging support for invoke helper (#43136)
Co-authored-by: Wes Haggard <[email protected]>
1 parent f8a02ba commit 710d5e8

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
function Invoke-LoggedCommand
1+
. $PSScriptRoot/../logging.ps1
2+
3+
function Invoke-LoggedMsbuildCommand
24
{
35
[CmdletBinding()]
46
param
@@ -8,12 +10,26 @@ function Invoke-LoggedCommand
810
[switch] $GroupOutput,
911
[int[]] $AllowedExitCodes = @(0)
1012
)
13+
return Invoke-LoggedCommand $Command -ExecutePath $ExecutePath -GroupOutput:$GroupOutput -AllowedExitCodes $AllowedExitCodes -OutputProcessor { param($line) ProcessMsBuildLogLine $line }
14+
15+
}
16+
17+
function Invoke-LoggedCommand
18+
{
19+
[CmdletBinding()]
20+
param
21+
(
22+
[string] $Command,
23+
[string] $ExecutePath,
24+
[switch] $GroupOutput,
25+
[int[]] $AllowedExitCodes = @(0),
26+
[scriptblock] $OutputProcessor
27+
)
1128

12-
$pipelineBuild = !!$env:TF_BUILD
1329
$startTime = Get-Date
1430

15-
if($pipelineBuild -and $GroupOutput) {
16-
Write-Host "##[group]$Command"
31+
if($GroupOutput) {
32+
LogGroupStart $Command
1733
} else {
1834
Write-Host "> $Command"
1935
}
@@ -22,22 +38,22 @@ function Invoke-LoggedCommand
2238
Push-Location $ExecutePath
2339
}
2440

41+
if (!$OutputProcessor) {
42+
$OutputProcessor = { param($line) $line }
43+
}
44+
2545
try {
26-
Invoke-Expression $Command
46+
Invoke-Expression $Command | Foreach-Object { & $OutputProcessor $_ }
2747

2848
$duration = (Get-Date) - $startTime
2949

30-
if($pipelineBuild -and $GroupOutput) {
31-
Write-Host "##[endgroup]"
50+
if($GroupOutput) {
51+
LogGroupEnd
3252
}
3353

3454
if($LastExitCode -notin $AllowedExitCodes)
3555
{
36-
if($pipelineBuild) {
37-
Write-Error "##[error]Command failed to execute ($duration): $Command`n"
38-
} else {
39-
Write-Error "Command failed to execute ($duration): $Command`n"
40-
}
56+
LogError "Command failed to execute ($duration): $Command`n"
4157
}
4258
else {
4359
Write-Host "Command succeeded ($duration)`n"

eng/common/scripts/logging.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,15 @@ function LogJobFailure() {
111111
}
112112
# No equivalent for GitHub Actions. Failure is only determined by nonzero exit code.
113113
}
114+
115+
function ProcessMsBuildLogLine($line) {
116+
if (Test-SupportsDevOpsLogging) {
117+
if ($line -like "*: warning*") {
118+
return ("##vso[task.LogIssue type=warning;]$line" -replace "`n", "%0D%0A")
119+
}
120+
elseif ($line -like "*: error*") {
121+
return ("##vso[task.LogIssue type=error;]$line" -replace "`n", "%0D%0A")
122+
}
123+
}
124+
return $line
125+
}

0 commit comments

Comments
 (0)