1
# Source for this file was taken from https://github.com/microsoft/azure-pipelines-task-lib/blob/11c9439d4af17e6475d9fe058e6b2e03914d17e6/powershell/VstsTaskSdk/LoggingCommandFunctions.ps1 and modified.
3
# 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
5
$script:loggingCommandPrefix = '##vso['
6
$script:loggingCommandEscapeMappings = @( # TODO: WHAT ABOUT "="? WHAT ABOUT "%"?
7
New-Object psobject -Property @{ Token = ';' ; Replacement = '%3B' }
8
New-Object psobject -Property @{ Token = "`r" ; Replacement = '%0D' }
9
New-Object psobject -Property @{ Token = "`n" ; Replacement = '%0A' }
10
New-Object psobject -Property @{ Token = "]" ; Replacement = '%5D' }
12
# TODO: BUG: Escape % ???
13
# TODO: Add test to verify don't need to escape "=".
15
# Specify "-Force" to force pipeline formatted output even if "$ci" is false or not set
16
function Write-PipelineTelemetryError {
19
[Parameter(Mandatory = $true)]
21
[Parameter(Mandatory = $true)]
23
[Parameter(Mandatory = $false)]
24
[string]$Type = 'error',
28
[string]$ColumnNumber,
32
$PSBoundParameters.Remove('Category') | Out-Null
34
if ($Force -Or ((Test-Path variable:ci) -And $ci)) {
35
$Message = "(NETCORE_ENGINEERING_TELEMETRY=$Category) $Message"
37
$PSBoundParameters.Remove('Message') | Out-Null
38
$PSBoundParameters.Add('Message', $Message)
39
Write-PipelineTaskError @PSBoundParameters
42
# Specify "-Force" to force pipeline formatted output even if "$ci" is false or not set
43
function Write-PipelineTaskError {
46
[Parameter(Mandatory = $true)]
48
[Parameter(Mandatory = $false)]
49
[string]$Type = 'error',
53
[string]$ColumnNumber,
58
if (!$Force -And (-Not (Test-Path variable:ci) -Or !$ci)) {
59
if ($Type -eq 'error') {
60
Write-Host $Message -ForegroundColor Red
63
elseif ($Type -eq 'warning') {
64
Write-Host $Message -ForegroundColor Yellow
69
if (($Type -ne 'error') -and ($Type -ne 'warning')) {
73
$PSBoundParameters.Remove('Force') | Out-Null
74
if (-not $PSBoundParameters.ContainsKey('Type')) {
75
$PSBoundParameters.Add('Type', 'error')
77
Write-LogIssue @PSBoundParameters
80
function Write-PipelineSetVariable {
83
[Parameter(Mandatory = $true)]
88
[bool]$IsMultiJobVariable = $true)
90
if ((Test-Path variable:ci) -And $ci) {
91
Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data $Value -Properties @{
94
'isOutput' = $IsMultiJobVariable
99
function Write-PipelinePrependPath {
102
[Parameter(Mandatory = $true)]
106
if ((Test-Path variable:ci) -And $ci) {
107
Write-LoggingCommand -Area 'task' -Event 'prependpath' -Data $Path -AsOutput:$AsOutput
111
function Write-PipelineSetResult {
114
[ValidateSet("Succeeded", "SucceededWithIssues", "Failed", "Cancelled", "Skipped")]
115
[Parameter(Mandatory = $true)]
118
if ((Test-Path variable:ci) -And $ci) {
119
Write-LoggingCommand -Area 'task' -Event 'complete' -Data $Message -Properties @{
125
<########################################
127
########################################>
128
function Format-LoggingCommandData {
130
param([string]$Value, [switch]$Reverse)
137
foreach ($mapping in $script:loggingCommandEscapeMappings) {
138
$Value = $Value.Replace($mapping.Token, $mapping.Replacement)
142
for ($i = $script:loggingCommandEscapeMappings.Length - 1 ; $i -ge 0 ; $i--) {
143
$mapping = $script:loggingCommandEscapeMappings[$i]
144
$Value = $Value.Replace($mapping.Replacement, $mapping.Token)
151
function Format-LoggingCommand {
154
[Parameter(Mandatory = $true)]
156
[Parameter(Mandatory = $true)]
159
[hashtable]$Properties)
161
# Append the preamble.
162
[System.Text.StringBuilder]$sb = New-Object -TypeName System.Text.StringBuilder
163
$null = $sb.Append($script:loggingCommandPrefix).Append($Area).Append('.').Append($Event)
165
# Append the properties.
168
foreach ($key in $Properties.Keys) {
169
[string]$value = Format-LoggingCommandData $Properties[$key]
172
$null = $sb.Append(' ')
176
$null = $sb.Append(';')
179
$null = $sb.Append("$key=$value")
184
# Append the tail and output the value.
185
$Data = Format-LoggingCommandData $Data
186
$sb.Append(']').Append($Data).ToString()
189
function Write-LoggingCommand {
190
[CmdletBinding(DefaultParameterSetName = 'Parameters')]
192
[Parameter(Mandatory = $true, ParameterSetName = 'Parameters')]
194
[Parameter(Mandatory = $true, ParameterSetName = 'Parameters')]
196
[Parameter(ParameterSetName = 'Parameters')]
198
[Parameter(ParameterSetName = 'Parameters')]
199
[hashtable]$Properties,
200
[Parameter(Mandatory = $true, ParameterSetName = 'Object')]
204
if ($PSCmdlet.ParameterSetName -eq 'Object') {
205
Write-LoggingCommand -Area $Command.Area -Event $Command.Event -Data $Command.Data -Properties $Command.Properties -AsOutput:$AsOutput
209
$command = Format-LoggingCommand -Area $Area -Event $Event -Data $Data -Properties $Properties
218
function Write-LogIssue {
221
[ValidateSet('warning', 'error')]
222
[Parameter(Mandatory = $true)]
228
[string]$ColumnNumber,
231
$command = Format-LoggingCommand -Area 'task' -Event 'logissue' -Data $Message -Properties @{
234
'sourcepath' = $SourcePath
235
'linenumber' = $LineNumber
236
'columnnumber' = $ColumnNumber
242
if ($Type -eq 'error') {
243
$foregroundColor = $host.PrivateData.ErrorForegroundColor
244
$backgroundColor = $host.PrivateData.ErrorBackgroundColor
245
if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) {
246
$foregroundColor = [System.ConsoleColor]::Red
247
$backgroundColor = [System.ConsoleColor]::Black
251
$foregroundColor = $host.PrivateData.WarningForegroundColor
252
$backgroundColor = $host.PrivateData.WarningBackgroundColor
253
if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) {
254
$foregroundColor = [System.ConsoleColor]::Yellow
255
$backgroundColor = [System.ConsoleColor]::Black
259
Write-Host $command -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor