2019-06-14 12:10:56 +00:00
# Source for this file was taken from https://github.com/microsoft/azure-pipelines-task-lib/blob/11c9439d4af17e6475d9fe058e6b2e03914d17e6/powershell/VstsTaskSdk/LoggingCommandFunctions.ps1 and modified.
2019-05-31 12:10:52 +00:00
# NOTE: You should not be calling these method directly as they are likely to change. Instead you should be calling the Write-Pipeline* functions defined in tools.ps1
$script:loggingCommandPrefix = '##vso['
$script:loggingCommandEscapeMappings = @ ( # TODO: WHAT ABOUT "="? WHAT ABOUT "%"?
New-Object psobject -Property @ { Token = ';' ; Replacement = '%3B' }
New-Object psobject -Property @ { Token = " `r " ; Replacement = '%0D' }
New-Object psobject -Property @ { Token = " `n " ; Replacement = '%0A' }
New-Object psobject -Property @ { Token = " ] " ; Replacement = '%5D' }
)
# TODO: BUG: Escape % ???
# TODO: Add test to verify don't need to escape "=".
2019-11-22 13:41:58 +00:00
# Specify "-Force" to force pipeline formatted output even if "$ci" is false or not set
2019-06-14 12:10:56 +00:00
function Write-PipelineTelemetryError {
[ CmdletBinding ( ) ]
param (
[ Parameter ( Mandatory = $true ) ]
[ string ] $Category ,
[ Parameter ( Mandatory = $true ) ]
[ string ] $Message ,
[ Parameter ( Mandatory = $false ) ]
[ string ] $Type = 'error' ,
[ string ] $ErrCode ,
[ string ] $SourcePath ,
[ string ] $LineNumber ,
[ string ] $ColumnNumber ,
2019-11-22 13:41:58 +00:00
[ switch ] $AsOutput ,
[ switch ] $Force )
2019-06-14 12:10:56 +00:00
2020-12-09 03:11:38 +00:00
$PSBoundParameters . Remove ( 'Category' ) | Out-Null
2019-06-14 12:10:56 +00:00
2020-12-09 03:11:38 +00:00
if ( $Force -Or ( ( Test-Path variable : ci ) -And $ci ) ) {
$Message = " (NETCORE_ENGINEERING_TELEMETRY= $Category ) $Message "
}
$PSBoundParameters . Remove ( 'Message' ) | Out-Null
$PSBoundParameters . Add ( 'Message' , $Message )
Write-PipelineTaskError @PSBoundParameters
2019-06-14 12:10:56 +00:00
}
2019-11-22 13:41:58 +00:00
# Specify "-Force" to force pipeline formatted output even if "$ci" is false or not set
2019-06-14 12:10:56 +00:00
function Write-PipelineTaskError {
[ CmdletBinding ( ) ]
param (
2019-11-22 13:41:58 +00:00
[ Parameter ( Mandatory = $true ) ]
[ string ] $Message ,
[ Parameter ( Mandatory = $false ) ]
[ string ] $Type = 'error' ,
[ string ] $ErrCode ,
[ string ] $SourcePath ,
[ string ] $LineNumber ,
[ string ] $ColumnNumber ,
[ switch ] $AsOutput ,
[ switch ] $Force
)
2020-12-09 03:11:38 +00:00
if ( ! $Force -And ( -Not ( Test-Path variable : ci ) -Or ! $ci ) ) {
if ( $Type -eq 'error' ) {
2019-11-22 13:41:58 +00:00
Write-Host $Message -ForegroundColor Red
return
2019-06-14 12:10:56 +00:00
}
elseif ( $Type -eq 'warning' ) {
2019-11-22 13:41:58 +00:00
Write-Host $Message -ForegroundColor Yellow
return
2019-06-14 12:10:56 +00:00
}
2019-11-22 13:41:58 +00:00
}
2020-12-09 03:11:38 +00:00
if ( ( $Type -ne 'error' ) -and ( $Type -ne 'warning' ) ) {
2020-03-31 13:04:58 +00:00
Write-Host $Message
return
2019-11-22 13:41:58 +00:00
}
$PSBoundParameters . Remove ( 'Force' ) | Out-Null
2020-12-09 03:11:38 +00:00
if ( -not $PSBoundParameters . ContainsKey ( 'Type' ) ) {
2020-03-31 13:04:58 +00:00
$PSBoundParameters . Add ( 'Type' , 'error' )
2019-11-22 13:41:58 +00:00
}
Write-LogIssue @PSBoundParameters
2020-12-09 03:11:38 +00:00
}
2019-06-14 12:10:56 +00:00
2020-12-09 03:11:38 +00:00
function Write-PipelineSetVariable {
2019-06-14 12:10:56 +00:00
[ CmdletBinding ( ) ]
param (
2020-12-09 03:11:38 +00:00
[ Parameter ( Mandatory = $true ) ]
[ string ] $Name ,
[ string ] $Value ,
[ switch ] $Secret ,
[ switch ] $AsOutput ,
[ bool ] $IsMultiJobVariable = $true )
if ( ( Test-Path variable : ci ) -And $ci ) {
2019-06-14 12:10:56 +00:00
Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data $Value -Properties @ {
2020-12-09 03:11:38 +00:00
'variable' = $Name
'isSecret' = $Secret
'isOutput' = $IsMultiJobVariable
2019-06-14 12:10:56 +00:00
} -AsOutput: $AsOutput
2020-12-09 03:11:38 +00:00
}
}
2019-06-14 12:10:56 +00:00
2020-12-09 03:11:38 +00:00
function Write-PipelinePrependPath {
2019-06-14 12:10:56 +00:00
[ CmdletBinding ( ) ]
param (
2020-12-09 03:11:38 +00:00
[ Parameter ( Mandatory = $true ) ]
[ string ] $Path ,
[ switch ] $AsOutput )
2019-11-22 13:41:58 +00:00
2020-12-09 03:11:38 +00:00
if ( ( Test-Path variable : ci ) -And $ci ) {
2019-06-14 12:10:56 +00:00
Write-LoggingCommand -Area 'task' -Event 'prependpath' -Data $Path -AsOutput: $AsOutput
2020-12-09 03:11:38 +00:00
}
}
function Write-PipelineSetResult {
[ CmdletBinding ( ) ]
param (
[ ValidateSet ( " Succeeded " , " SucceededWithIssues " , " Failed " , " Cancelled " , " Skipped " ) ]
[ Parameter ( Mandatory = $true ) ]
[ string ] $Result ,
[ string ] $Message )
if ( ( Test-Path variable : ci ) -And $ci ) {
Write-LoggingCommand -Area 'task' -Event 'complete' -Data $Message -Properties @ {
'result' = $Result
}
}
}
2019-06-14 12:10:56 +00:00
2019-05-31 12:10:52 +00:00
<# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Private functions.
########################################>
function Format-LoggingCommandData {
[ CmdletBinding ( ) ]
param ( [ string ] $Value , [ switch ] $Reverse )
if ( ! $Value ) {
return ''
}
if ( ! $Reverse ) {
foreach ( $mapping in $script:loggingCommandEscapeMappings ) {
$Value = $Value . Replace ( $mapping . Token , $mapping . Replacement )
}
2020-12-09 03:11:38 +00:00
}
else {
2019-05-31 12:10:52 +00:00
for ( $i = $script:loggingCommandEscapeMappings . Length - 1 ; $i -ge 0 ; $i - - ) {
$mapping = $script:loggingCommandEscapeMappings [ $i ]
$Value = $Value . Replace ( $mapping . Replacement , $mapping . Token )
}
}
return $Value
}
function Format-LoggingCommand {
[ CmdletBinding ( ) ]
param (
[ Parameter ( Mandatory = $true ) ]
[ string ] $Area ,
[ Parameter ( Mandatory = $true ) ]
[ string ] $Event ,
[ string ] $Data ,
[ hashtable ] $Properties )
# Append the preamble.
[ System.Text.StringBuilder ] $sb = New-Object -TypeName System . Text . StringBuilder
$null = $sb . Append ( $script:loggingCommandPrefix ) . Append ( $Area ) . Append ( '.' ) . Append ( $Event )
# Append the properties.
if ( $Properties ) {
$first = $true
foreach ( $key in $Properties . Keys ) {
[ string ] $value = Format-LoggingCommandData $Properties [ $key ]
if ( $value ) {
if ( $first ) {
$null = $sb . Append ( ' ' )
$first = $false
2020-12-09 03:11:38 +00:00
}
else {
2019-05-31 12:10:52 +00:00
$null = $sb . Append ( ';' )
}
$null = $sb . Append ( " $key = $value " )
}
}
}
# Append the tail and output the value.
$Data = Format-LoggingCommandData $Data
$sb . Append ( ']' ) . Append ( $Data ) . ToString ( )
}
function Write-LoggingCommand {
[ CmdletBinding ( DefaultParameterSetName = 'Parameters' ) ]
param (
[ Parameter ( Mandatory = $true , ParameterSetName = 'Parameters' ) ]
[ string ] $Area ,
[ Parameter ( Mandatory = $true , ParameterSetName = 'Parameters' ) ]
[ string ] $Event ,
[ Parameter ( ParameterSetName = 'Parameters' ) ]
[ string ] $Data ,
[ Parameter ( ParameterSetName = 'Parameters' ) ]
[ hashtable ] $Properties ,
[ Parameter ( Mandatory = $true , ParameterSetName = 'Object' ) ]
$Command ,
[ switch ] $AsOutput )
if ( $PSCmdlet . ParameterSetName -eq 'Object' ) {
Write-LoggingCommand -Area $Command . Area -Event $Command . Event -Data $Command . Data -Properties $Command . Properties -AsOutput: $AsOutput
return
}
$command = Format-LoggingCommand -Area $Area -Event $Event -Data $Data -Properties $Properties
if ( $AsOutput ) {
$command
2020-12-09 03:11:38 +00:00
}
else {
2019-05-31 12:10:52 +00:00
Write-Host $command
}
}
function Write-LogIssue {
[ CmdletBinding ( ) ]
param (
[ ValidateSet ( 'warning' , 'error' ) ]
[ Parameter ( Mandatory = $true ) ]
[ string ] $Type ,
[ string ] $Message ,
[ string ] $ErrCode ,
[ string ] $SourcePath ,
[ string ] $LineNumber ,
[ string ] $ColumnNumber ,
[ switch ] $AsOutput )
$command = Format-LoggingCommand -Area 'task' -Event 'logissue' -Data $Message -Properties @ {
2020-12-09 03:11:38 +00:00
'type' = $Type
'code' = $ErrCode
'sourcepath' = $SourcePath
'linenumber' = $LineNumber
'columnnumber' = $ColumnNumber
}
2019-05-31 12:10:52 +00:00
if ( $AsOutput ) {
return $command
}
if ( $Type -eq 'error' ) {
$foregroundColor = $host . PrivateData . ErrorForegroundColor
$backgroundColor = $host . PrivateData . ErrorBackgroundColor
if ( $foregroundColor -isnot [ System.ConsoleColor ] -or $backgroundColor -isnot [ System.ConsoleColor ] ) {
$foregroundColor = [ System.ConsoleColor ] :: Red
$backgroundColor = [ System.ConsoleColor ] :: Black
}
2020-12-09 03:11:38 +00:00
}
else {
2019-05-31 12:10:52 +00:00
$foregroundColor = $host . PrivateData . WarningForegroundColor
$backgroundColor = $host . PrivateData . WarningBackgroundColor
if ( $foregroundColor -isnot [ System.ConsoleColor ] -or $backgroundColor -isnot [ System.ConsoleColor ] ) {
$foregroundColor = [ System.ConsoleColor ] :: Yellow
$backgroundColor = [ System.ConsoleColor ] :: Black
}
}
Write-Host $command -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor
2019-12-06 13:31:33 +00:00
}