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 function Write-PipelineTelemetryError
{
18 [Parameter
(Mandatory
= $true)]
20 [Parameter
(Mandatory
= $true)]
22 [Parameter
(Mandatory
= $false)]
23 [string
]$Type = 'error',
27 [string
]$ColumnNumber,
30 $PSBoundParameters.Remove
("Category") | Out-Null
32 $Message = "(NETCORE_ENGINEERING_TELEMETRY=$Category) $Message"
33 $PSBoundParameters.Remove
("Message") | Out-Null
34 $PSBoundParameters.Add
("Message", $Message)
36 Write-PipelineTaskError @PSBoundParameters
39 function Write-PipelineTaskError
{
42 [Parameter
(Mandatory
= $true)]
44 [Parameter
(Mandatory
= $false)]
45 [string
]$Type = 'error',
49 [string
]$ColumnNumber,
53 if($Type -eq
'error') {
54 Write-Host $Message -ForegroundColor Red
57 elseif
($Type -eq
'warning') {
58 Write-Host $Message -ForegroundColor Yellow
63 if(($Type -ne
'error') -and
($Type -ne
'warning')) {
67 if(-not
$PSBoundParameters.ContainsKey
('Type')) {
68 $PSBoundParameters.Add
('Type', 'error')
70 Write-LogIssue @PSBoundParameters
73 function Write-PipelineSetVariable
{
76 [Parameter
(Mandatory
= $true)]
81 [bool
]$IsMultiJobVariable=$true)
84 Write-LoggingCommand
-Area
'task' -Event
'setvariable' -Data
$Value -Properties @
{
87 'isOutput' = $IsMultiJobVariable
92 function Write-PipelinePrependPath
{
95 [Parameter
(Mandatory
=$true)]
99 Write-LoggingCommand
-Area
'task' -Event
'prependpath' -Data
$Path -AsOutput
:$AsOutput
103 <########################################
105 ########################################>
106 function Format-LoggingCommandData
{
108 param
([string
]$Value, [switch]$Reverse)
115 foreach ($mapping in $script:loggingCommandEscapeMappings
) {
116 $Value = $Value.Replace
($mapping.Token
, $mapping.Replacement
)
119 for
($i = $script:loggingCommandEscapeMappings
.Length
- 1 ; $i -ge
0 ; $i--) {
120 $mapping = $script:loggingCommandEscapeMappings
[$i]
121 $Value = $Value.Replace
($mapping.Replacement
, $mapping.Token
)
128 function Format-LoggingCommand
{
131 [Parameter
(Mandatory
= $true)]
133 [Parameter
(Mandatory
= $true)]
136 [hashtable
]$Properties)
138 # Append the preamble.
139 [System
.Text
.StringBuilder
]$sb = New-Object -TypeName System
.Text
.StringBuilder
140 $null = $sb.Append
($script:loggingCommandPrefix
).Append
($Area).Append
('.').Append
($Event)
142 # Append the properties.
145 foreach ($key in $Properties.Keys
) {
146 [string
]$value = Format-LoggingCommandData
$Properties[$key]
149 $null = $sb.Append
(' ')
152 $null = $sb.Append
(';')
155 $null = $sb.Append
("$key=$value")
160 # Append the tail and output the value.
161 $Data = Format-LoggingCommandData
$Data
162 $sb.Append
(']').Append
($Data).ToString
()
165 function Write-LoggingCommand
{
166 [CmdletBinding
(DefaultParameterSetName
= 'Parameters')]
168 [Parameter
(Mandatory
= $true, ParameterSetName
= 'Parameters')]
170 [Parameter
(Mandatory
= $true, ParameterSetName
= 'Parameters')]
172 [Parameter
(ParameterSetName
= 'Parameters')]
174 [Parameter
(ParameterSetName
= 'Parameters')]
175 [hashtable
]$Properties,
176 [Parameter
(Mandatory
= $true, ParameterSetName
= 'Object')]
180 if ($PSCmdlet.ParameterSetName
-eq
'Object') {
181 Write-LoggingCommand
-Area
$Command.Area
-Event
$Command.Event
-Data
$Command.Data
-Properties
$Command.Properties
-AsOutput
:$AsOutput
185 $command = Format-LoggingCommand
-Area
$Area -Event
$Event -Data
$Data -Properties
$Properties
193 function Write-LogIssue
{
196 [ValidateSet
('warning', 'error')]
197 [Parameter
(Mandatory
= $true)]
203 [string
]$ColumnNumber,
206 $command = Format-LoggingCommand
-Area
'task' -Event
'logissue' -Data
$Message -Properties @
{
209 'sourcepath' = $SourcePath
210 'linenumber' = $LineNumber
211 'columnnumber' = $ColumnNumber
217 if ($Type -eq
'error') {
218 $foregroundColor = $host.PrivateData
.ErrorForegroundColor
219 $backgroundColor = $host.PrivateData
.ErrorBackgroundColor
220 if ($foregroundColor -isnot
[System
.ConsoleColor
] -or
$backgroundColor -isnot
[System
.ConsoleColor
]) {
221 $foregroundColor = [System
.ConsoleColor
]::Red
222 $backgroundColor = [System
.ConsoleColor
]::Black
225 $foregroundColor = $host.PrivateData
.WarningForegroundColor
226 $backgroundColor = $host.PrivateData
.WarningBackgroundColor
227 if ($foregroundColor -isnot
[System
.ConsoleColor
] -or
$backgroundColor -isnot
[System
.ConsoleColor
]) {
228 $foregroundColor = [System
.ConsoleColor
]::Yellow
229 $backgroundColor = [System
.ConsoleColor
]::Black
233 Write-Host $command -ForegroundColor
$foregroundColor -BackgroundColor
$backgroundColor