[2020-02] Bump msbuild to track mono-2019-12 (#19661)
[mono-project.git] / eng / common / pipeline-logging-functions.ps1
bloba3e1317ad43498b9835e6ba255b947d8a5ff461d
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 {
17 [CmdletBinding()]
18 param(
19 [Parameter(Mandatory = $true)]
20 [string]$Category,
21 [Parameter(Mandatory = $true)]
22 [string]$Message,
23 [Parameter(Mandatory = $false)]
24 [string]$Type = 'error',
25 [string]$ErrCode,
26 [string]$SourcePath,
27 [string]$LineNumber,
28 [string]$ColumnNumber,
29 [switch]$AsOutput,
30 [switch]$Force)
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 {
42 [CmdletBinding()]
43 param(
44 [Parameter(Mandatory = $true)]
45 [string]$Message,
46 [Parameter(Mandatory = $false)]
47 [string]$Type = 'error',
48 [string]$ErrCode,
49 [string]$SourcePath,
50 [string]$LineNumber,
51 [string]$ColumnNumber,
52 [switch]$AsOutput,
53 [switch]$Force
56 if(!$Force -And (-Not (Test-Path variable:ci) -Or !$ci)) {
57 if($Type -eq 'error') {
58 Write-Host $Message -ForegroundColor Red
59 return
61 elseif ($Type -eq 'warning') {
62 Write-Host $Message -ForegroundColor Yellow
63 return
67 if(($Type -ne 'error') -and ($Type -ne 'warning')) {
68 Write-Host $Message
69 return
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 {
79 [CmdletBinding()]
80 param(
81 [Parameter(Mandatory = $true)]
82 [string]$Name,
83 [string]$Value,
84 [switch]$Secret,
85 [switch]$AsOutput,
86 [bool]$IsMultiJobVariable=$true)
88 if((Test-Path variable:ci) -And $ci) {
89 Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data $Value -Properties @{
90 'variable' = $Name
91 'isSecret' = $Secret
92 'isOutput' = $IsMultiJobVariable
93 } -AsOutput:$AsOutput
97 function Write-PipelinePrependPath {
98 [CmdletBinding()]
99 param(
100 [Parameter(Mandatory=$true)]
101 [string]$Path,
102 [switch]$AsOutput)
104 if((Test-Path variable:ci) -And $ci) {
105 Write-LoggingCommand -Area 'task' -Event 'prependpath' -Data $Path -AsOutput:$AsOutput
109 <########################################
110 # Private functions.
111 ########################################>
112 function Format-LoggingCommandData {
113 [CmdletBinding()]
114 param([string]$Value, [switch]$Reverse)
116 if (!$Value) {
117 return ''
120 if (!$Reverse) {
121 foreach ($mapping in $script:loggingCommandEscapeMappings) {
122 $Value = $Value.Replace($mapping.Token, $mapping.Replacement)
124 } else {
125 for ($i = $script:loggingCommandEscapeMappings.Length - 1 ; $i -ge 0 ; $i--) {
126 $mapping = $script:loggingCommandEscapeMappings[$i]
127 $Value = $Value.Replace($mapping.Replacement, $mapping.Token)
131 return $Value
134 function Format-LoggingCommand {
135 [CmdletBinding()]
136 param(
137 [Parameter(Mandatory = $true)]
138 [string]$Area,
139 [Parameter(Mandatory = $true)]
140 [string]$Event,
141 [string]$Data,
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.
149 if ($Properties) {
150 $first = $true
151 foreach ($key in $Properties.Keys) {
152 [string]$value = Format-LoggingCommandData $Properties[$key]
153 if ($value) {
154 if ($first) {
155 $null = $sb.Append(' ')
156 $first = $false
157 } else {
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')]
173 param(
174 [Parameter(Mandatory = $true, ParameterSetName = 'Parameters')]
175 [string]$Area,
176 [Parameter(Mandatory = $true, ParameterSetName = 'Parameters')]
177 [string]$Event,
178 [Parameter(ParameterSetName = 'Parameters')]
179 [string]$Data,
180 [Parameter(ParameterSetName = 'Parameters')]
181 [hashtable]$Properties,
182 [Parameter(Mandatory = $true, ParameterSetName = 'Object')]
183 $Command,
184 [switch]$AsOutput)
186 if ($PSCmdlet.ParameterSetName -eq 'Object') {
187 Write-LoggingCommand -Area $Command.Area -Event $Command.Event -Data $Command.Data -Properties $Command.Properties -AsOutput:$AsOutput
188 return
191 $command = Format-LoggingCommand -Area $Area -Event $Event -Data $Data -Properties $Properties
192 if ($AsOutput) {
193 $command
194 } else {
195 Write-Host $command
199 function Write-LogIssue {
200 [CmdletBinding()]
201 param(
202 [ValidateSet('warning', 'error')]
203 [Parameter(Mandatory = $true)]
204 [string]$Type,
205 [string]$Message,
206 [string]$ErrCode,
207 [string]$SourcePath,
208 [string]$LineNumber,
209 [string]$ColumnNumber,
210 [switch]$AsOutput)
212 $command = Format-LoggingCommand -Area 'task' -Event 'logissue' -Data $Message -Properties @{
213 'type' = $Type
214 'code' = $ErrCode
215 'sourcepath' = $SourcePath
216 'linenumber' = $LineNumber
217 'columnnumber' = $ColumnNumber
219 if ($AsOutput) {
220 return $command
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
230 } else {
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