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 $Message = "(NETCORE_ENGINEERING_TELEMETRY=$Category) $Message"
35 $PSBoundParameters.Remove
('Message') | Out-Null
36 $PSBoundParameters.Add
('Message', $Message)
37 Write-PipelineTaskError @PSBoundParameters
40 # Specify "-Force" to force pipeline formatted output even if "$ci" is false or not set
41 function Write-PipelineTaskError
{
44 [Parameter
(Mandatory
= $true)]
46 [Parameter
(Mandatory
= $false)]
47 [string
]$Type = 'error',
51 [string
]$ColumnNumber,
56 if(!$Force -And
(-Not
(Test-Path variable
:ci
) -Or
!$ci)) {
57 if($Type -eq
'error') {
58 Write-Host $Message -ForegroundColor Red
61 elseif
($Type -eq
'warning') {
62 Write-Host $Message -ForegroundColor Yellow
67 if(($Type -ne
'error') -and
($Type -ne
'warning')) {
71 $PSBoundParameters.Remove
('Force') | Out-Null
72 if(-not
$PSBoundParameters.ContainsKey
('Type')) {
73 $PSBoundParameters.Add
('Type', 'error')
75 Write-LogIssue @PSBoundParameters
78 function Write-PipelineSetVariable
{
81 [Parameter
(Mandatory
= $true)]
86 [bool
]$IsMultiJobVariable=$true)
88 if((Test-Path variable
:ci
) -And
$ci) {
89 Write-LoggingCommand
-Area
'task' -Event
'setvariable' -Data
$Value -Properties @
{
92 'isOutput' = $IsMultiJobVariable
97 function Write-PipelinePrependPath
{
100 [Parameter
(Mandatory
=$true)]
104 if((Test-Path variable
:ci
) -And
$ci) {
105 Write-LoggingCommand
-Area
'task' -Event
'prependpath' -Data
$Path -AsOutput
:$AsOutput
109 <########################################
111 ########################################>
112 function Format-LoggingCommandData
{
114 param
([string
]$Value, [switch]$Reverse)
121 foreach ($mapping in $script:loggingCommandEscapeMappings
) {
122 $Value = $Value.Replace
($mapping.Token
, $mapping.Replacement
)
125 for
($i = $script:loggingCommandEscapeMappings
.Length
- 1 ; $i -ge
0 ; $i--) {
126 $mapping = $script:loggingCommandEscapeMappings
[$i]
127 $Value = $Value.Replace
($mapping.Replacement
, $mapping.Token
)
134 function Format-LoggingCommand
{
137 [Parameter
(Mandatory
= $true)]
139 [Parameter
(Mandatory
= $true)]
142 [hashtable
]$Properties)
144 # Append the preamble.
145 [System
.Text
.StringBuilder
]$sb = New-Object -TypeName System
.Text
.StringBuilder
146 $null = $sb.Append
($script:loggingCommandPrefix
).Append
($Area).Append
('.').Append
($Event)
148 # Append the properties.
151 foreach ($key in $Properties.Keys
) {
152 [string
]$value = Format-LoggingCommandData
$Properties[$key]
155 $null = $sb.Append
(' ')
158 $null = $sb.Append
(';')
161 $null = $sb.Append
("$key=$value")
166 # Append the tail and output the value.
167 $Data = Format-LoggingCommandData
$Data
168 $sb.Append
(']').Append
($Data).ToString
()
171 function Write-LoggingCommand
{
172 [CmdletBinding
(DefaultParameterSetName
= 'Parameters')]
174 [Parameter
(Mandatory
= $true, ParameterSetName
= 'Parameters')]
176 [Parameter
(Mandatory
= $true, ParameterSetName
= 'Parameters')]
178 [Parameter
(ParameterSetName
= 'Parameters')]
180 [Parameter
(ParameterSetName
= 'Parameters')]
181 [hashtable
]$Properties,
182 [Parameter
(Mandatory
= $true, ParameterSetName
= 'Object')]
186 if ($PSCmdlet.ParameterSetName
-eq
'Object') {
187 Write-LoggingCommand
-Area
$Command.Area
-Event
$Command.Event
-Data
$Command.Data
-Properties
$Command.Properties
-AsOutput
:$AsOutput
191 $command = Format-LoggingCommand
-Area
$Area -Event
$Event -Data
$Data -Properties
$Properties
199 function Write-LogIssue
{
202 [ValidateSet
('warning', 'error')]
203 [Parameter
(Mandatory
= $true)]
209 [string
]$ColumnNumber,
212 $command = Format-LoggingCommand
-Area
'task' -Event
'logissue' -Data
$Message -Properties @
{
215 'sourcepath' = $SourcePath
216 'linenumber' = $LineNumber
217 'columnnumber' = $ColumnNumber
223 if ($Type -eq
'error') {
224 $foregroundColor = $host.PrivateData
.ErrorForegroundColor
225 $backgroundColor = $host.PrivateData
.ErrorBackgroundColor
226 if ($foregroundColor -isnot
[System
.ConsoleColor
] -or
$backgroundColor -isnot
[System
.ConsoleColor
]) {
227 $foregroundColor = [System
.ConsoleColor
]::Red
228 $backgroundColor = [System
.ConsoleColor
]::Black
231 $foregroundColor = $host.PrivateData
.WarningForegroundColor
232 $backgroundColor = $host.PrivateData
.WarningBackgroundColor
233 if ($foregroundColor -isnot
[System
.ConsoleColor
] -or
$backgroundColor -isnot
[System
.ConsoleColor
]) {
234 $foregroundColor = [System
.ConsoleColor
]::Yellow
235 $backgroundColor = [System
.ConsoleColor
]::Black
239 Write-Host $command -ForegroundColor
$foregroundColor -BackgroundColor
$backgroundColor