* Change logging functions to use Write-Host Change logging helpers to always write to host and either set color or use devops/gh formatting. We do not want to use Write-Error or Write-Warning directly because they can stop the script or not depending on preferences which makes it difficult to ensure local runs of scripts work the same as in the pipelines. So, we should never depend on these logging commands to cause a script to stop execution. * Clean up error handling in CommandInvocation-Helpers Removed legacy error handling for command failures. * Fix error logging for command execution * Add option to skip exiting --------- Co-authored-by: Wes Haggard <weshaggard@users.noreply.github.com> Co-authored-by: Wes Haggard <Wes.Haggard@microsoft.com>
85 lines
1.8 KiB
PowerShell
85 lines
1.8 KiB
PowerShell
. $PSScriptRoot/../logging.ps1
|
|
|
|
function Invoke-LoggedMsbuildCommand
|
|
{
|
|
[CmdletBinding()]
|
|
param
|
|
(
|
|
[string] $Command,
|
|
[string] $ExecutePath,
|
|
[switch] $GroupOutput,
|
|
[int[]] $AllowedExitCodes = @(0)
|
|
)
|
|
return Invoke-LoggedCommand $Command -ExecutePath $ExecutePath -GroupOutput:$GroupOutput -AllowedExitCodes $AllowedExitCodes -OutputProcessor { param($line) ProcessMsBuildLogLine $line }
|
|
|
|
}
|
|
|
|
function Invoke-LoggedCommand
|
|
{
|
|
[CmdletBinding()]
|
|
param
|
|
(
|
|
[string] $Command,
|
|
[string] $ExecutePath,
|
|
[switch] $GroupOutput,
|
|
[int[]] $AllowedExitCodes = @(0),
|
|
[switch] $DoNotExitOnFailedExitCode,
|
|
[scriptblock] $OutputProcessor
|
|
)
|
|
|
|
$startTime = Get-Date
|
|
|
|
if($GroupOutput) {
|
|
LogGroupStart $Command
|
|
} else {
|
|
Write-Host "> $Command"
|
|
}
|
|
|
|
if($ExecutePath) {
|
|
Push-Location $ExecutePath
|
|
}
|
|
|
|
if (!$OutputProcessor) {
|
|
$OutputProcessor = { param($line) $line }
|
|
}
|
|
|
|
try {
|
|
Invoke-Expression $Command | Foreach-Object { & $OutputProcessor $_ }
|
|
|
|
$duration = (Get-Date) - $startTime
|
|
|
|
if($GroupOutput) {
|
|
LogGroupEnd
|
|
}
|
|
|
|
if($LASTEXITCODE -notin $AllowedExitCodes)
|
|
{
|
|
LogError "Command failed to execute ($duration): $Command`n"
|
|
if (!$DoNotExitOnFailedExitCode) {
|
|
exit $LASTEXITCODE
|
|
}
|
|
}
|
|
else {
|
|
Write-Host "Command succeeded ($duration)`n"
|
|
}
|
|
}
|
|
finally {
|
|
if($ExecutePath) {
|
|
Pop-Location
|
|
}
|
|
}
|
|
}
|
|
|
|
function Set-ConsoleEncoding
|
|
{
|
|
[CmdletBinding()]
|
|
param
|
|
(
|
|
[string] $Encoding = 'utf-8'
|
|
)
|
|
|
|
$outputEncoding = [System.Text.Encoding]::GetEncoding($Encoding)
|
|
[Console]::OutputEncoding = $outputEncoding
|
|
[Console]::InputEncoding = $outputEncoding
|
|
}
|