msbuild

Форк
0
/
pipeline-logging-functions.ps1 
260 строк · 8.3 Кб
1
# Source for this file was taken from https://github.com/microsoft/azure-pipelines-task-lib/blob/11c9439d4af17e6475d9fe058e6b2e03914d17e6/powershell/VstsTaskSdk/LoggingCommandFunctions.ps1 and modified.
2

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
4

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' }
11
)
12
# TODO: BUG: Escape % ???
13
# TODO: Add test to verify don't need to escape "=".
14

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)
31

32
    $PSBoundParameters.Remove('Category') | Out-Null
33

34
    if ($Force -Or ((Test-Path variable:ci) -And $ci)) {
35
        $Message = "(NETCORE_ENGINEERING_TELEMETRY=$Category) $Message"
36
    }
37
    $PSBoundParameters.Remove('Message') | Out-Null
38
    $PSBoundParameters.Add('Message', $Message)
39
    Write-PipelineTaskError @PSBoundParameters
40
}
41

42
# Specify "-Force" to force pipeline formatted output even if "$ci" is false or not set
43
function Write-PipelineTaskError {
44
    [CmdletBinding()]
45
    param(
46
        [Parameter(Mandatory = $true)]
47
        [string]$Message,
48
        [Parameter(Mandatory = $false)]
49
        [string]$Type = 'error',
50
        [string]$ErrCode,
51
        [string]$SourcePath,
52
        [string]$LineNumber,
53
        [string]$ColumnNumber,
54
        [switch]$AsOutput,
55
        [switch]$Force
56
    )
57

58
    if (!$Force -And (-Not (Test-Path variable:ci) -Or !$ci)) {
59
        if ($Type -eq 'error') {
60
            Write-Host $Message -ForegroundColor Red
61
            return
62
        }
63
        elseif ($Type -eq 'warning') {
64
            Write-Host $Message -ForegroundColor Yellow
65
            return
66
        }
67
    }
68

69
    if (($Type -ne 'error') -and ($Type -ne 'warning')) {
70
        Write-Host $Message
71
        return
72
    }
73
    $PSBoundParameters.Remove('Force') | Out-Null      
74
    if (-not $PSBoundParameters.ContainsKey('Type')) {
75
        $PSBoundParameters.Add('Type', 'error')
76
    }
77
    Write-LogIssue @PSBoundParameters
78
}
79
  
80
function Write-PipelineSetVariable {
81
    [CmdletBinding()]
82
    param(
83
        [Parameter(Mandatory = $true)]
84
        [string]$Name,
85
        [string]$Value,
86
        [switch]$Secret,
87
        [switch]$AsOutput,
88
        [bool]$IsMultiJobVariable = $true)
89

90
    if ((Test-Path variable:ci) -And $ci) {
91
        Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data $Value -Properties @{
92
            'variable' = $Name
93
            'isSecret' = $Secret
94
            'isOutput' = $IsMultiJobVariable
95
        } -AsOutput:$AsOutput
96
    }
97
}
98
  
99
function Write-PipelinePrependPath {
100
    [CmdletBinding()]
101
    param(
102
        [Parameter(Mandatory = $true)]
103
        [string]$Path,
104
        [switch]$AsOutput)
105

106
    if ((Test-Path variable:ci) -And $ci) {
107
        Write-LoggingCommand -Area 'task' -Event 'prependpath' -Data $Path -AsOutput:$AsOutput
108
    }
109
}
110

111
function Write-PipelineSetResult {
112
    [CmdletBinding()]
113
    param(
114
        [ValidateSet("Succeeded", "SucceededWithIssues", "Failed", "Cancelled", "Skipped")]
115
        [Parameter(Mandatory = $true)]
116
        [string]$Result,
117
        [string]$Message)
118
    if ((Test-Path variable:ci) -And $ci) {
119
        Write-LoggingCommand -Area 'task' -Event 'complete' -Data $Message -Properties @{
120
            'result' = $Result
121
        }
122
    }
123
}
124

125
<########################################
126
# Private functions.
127
########################################>
128
function Format-LoggingCommandData {
129
    [CmdletBinding()]
130
    param([string]$Value, [switch]$Reverse)
131

132
    if (!$Value) {
133
        return ''
134
    }
135

136
    if (!$Reverse) {
137
        foreach ($mapping in $script:loggingCommandEscapeMappings) {
138
            $Value = $Value.Replace($mapping.Token, $mapping.Replacement)
139
        }
140
    }
141
    else {
142
        for ($i = $script:loggingCommandEscapeMappings.Length - 1 ; $i -ge 0 ; $i--) {
143
            $mapping = $script:loggingCommandEscapeMappings[$i]
144
            $Value = $Value.Replace($mapping.Replacement, $mapping.Token)
145
        }
146
    }
147

148
    return $Value
149
}
150

151
function Format-LoggingCommand {
152
    [CmdletBinding()]
153
    param(
154
        [Parameter(Mandatory = $true)]
155
        [string]$Area,
156
        [Parameter(Mandatory = $true)]
157
        [string]$Event,
158
        [string]$Data,
159
        [hashtable]$Properties)
160

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)
164

165
    # Append the properties.
166
    if ($Properties) {
167
        $first = $true
168
        foreach ($key in $Properties.Keys) {
169
            [string]$value = Format-LoggingCommandData $Properties[$key]
170
            if ($value) {
171
                if ($first) {
172
                    $null = $sb.Append(' ')
173
                    $first = $false
174
                }
175
                else {
176
                    $null = $sb.Append(';')
177
                }
178

179
                $null = $sb.Append("$key=$value")
180
            }
181
        }
182
    }
183

184
    # Append the tail and output the value.
185
    $Data = Format-LoggingCommandData $Data
186
    $sb.Append(']').Append($Data).ToString()
187
}
188

189
function Write-LoggingCommand {
190
    [CmdletBinding(DefaultParameterSetName = 'Parameters')]
191
    param(
192
        [Parameter(Mandatory = $true, ParameterSetName = 'Parameters')]
193
        [string]$Area,
194
        [Parameter(Mandatory = $true, ParameterSetName = 'Parameters')]
195
        [string]$Event,
196
        [Parameter(ParameterSetName = 'Parameters')]
197
        [string]$Data,
198
        [Parameter(ParameterSetName = 'Parameters')]
199
        [hashtable]$Properties,
200
        [Parameter(Mandatory = $true, ParameterSetName = 'Object')]
201
        $Command,
202
        [switch]$AsOutput)
203

204
    if ($PSCmdlet.ParameterSetName -eq 'Object') {
205
        Write-LoggingCommand -Area $Command.Area -Event $Command.Event -Data $Command.Data -Properties $Command.Properties -AsOutput:$AsOutput
206
        return
207
    }
208

209
    $command = Format-LoggingCommand -Area $Area -Event $Event -Data $Data -Properties $Properties
210
    if ($AsOutput) {
211
        $command
212
    }
213
    else {
214
        Write-Host $command
215
    }
216
}
217

218
function Write-LogIssue {
219
    [CmdletBinding()]
220
    param(
221
        [ValidateSet('warning', 'error')]
222
        [Parameter(Mandatory = $true)]
223
        [string]$Type,
224
        [string]$Message,
225
        [string]$ErrCode,
226
        [string]$SourcePath,
227
        [string]$LineNumber,
228
        [string]$ColumnNumber,
229
        [switch]$AsOutput)
230

231
    $command = Format-LoggingCommand -Area 'task' -Event 'logissue' -Data $Message -Properties @{
232
        'type'         = $Type
233
        'code'         = $ErrCode
234
        'sourcepath'   = $SourcePath
235
        'linenumber'   = $LineNumber
236
        'columnnumber' = $ColumnNumber
237
    }
238
    if ($AsOutput) {
239
        return $command
240
    }
241

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
248
        }
249
    }
250
    else {
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
256
        }
257
    }
258

259
    Write-Host $command -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor
260
}
261

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.