forked from StartAutomating/PSDevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrite-ADOProgress.ps1
128 lines (112 loc) · 3.97 KB
/
Write-ADOProgress.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
function Write-ADOProgress {
<#
.Synopsis
Writes AzureDevOps Progress
.Description
Writes a progress record to the Azure DevOps pipeline.
.Example
Write-ADOProgress -Activity "Doing Stuff" -Status "And Things" -PercentComplete 50
.Link
Write-ADOError
.Link
Write-ADOWarning
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "", Justification="Directly outputs in certain scenarios")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("Test-ForUnusableFunction", "", Justification="Directly outputs in certain scenarios")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("Test-ForPipelineParameter", "", Justification="Proxying Write-Progress")]
[OutputType([string])]
param(
# This text describes the activity whose progress is being reported.
[Parameter(Mandatory=$true, Position=0)]
[string]
$Activity,
# This text describes current state of the activity.
[Parameter(Position=1)]
[ValidateNotNullOrEmpty()]
[string]
$Status,
# Specifies an ID that distinguishes each progress bar from the others. Use this parameter when you are creating more than one progress bar in a single command.
# If the progress bars do not have different IDs, they are superimposed instead of being displayed in a series.
[Parameter(Position=2)]
[ValidateRange(0, 2147483647)]
[int]
$Id,
# Specifies the percentage of the activity that is completed.
# Use the value -1 if the percentage complete is unknown or not applicable.
[ValidateRange(-1, 100)]
[int]
$PercentComplete,
# Specifies the projected number of seconds remaining until the activity is completed.
[int]
$SecondsRemaining,
# This text describes the operation that is currently taking place.
[string]
$CurrentOperation,
# Specifies the parent activity of the current activity.
[ValidateRange(-1, 2147483647)]
[int]
$ParentId,
# Indicates the progress timeline operation is completed.
[switch]
$Completed
)
begin {
if (-not $script:ADOProgressIds) {
$script:ADOProgressIds = @{}
}
}
process {
$isFirst = $false
if (-not $script:ADOProgressIds[$id]) {
$script:ADOProgressIds[$id] = [GUID]::NewGuid()
$isFirst = $true
}
if ($ParentId -and -not $script:ADOProgressIds[$ParentId]) {
$script:ADOProgressIds[$ParentId] = [GUID]::NewGuid()
$isFirst = $true
}
#region Prepare Progress Message
$properties = @(
"id=$($script:ADOProgressIds[$ID])"
if ($ParentId) {
"parentid=$($script:ADOProgressIds[$id])"
}
if ($isFirst) {
if ($CurrentOperation) {
"name=$CurrentOperation"
} else {
"name=$Activity"
}
"type=build"
"order=$($script:ADOProgressIds.Count)"
}
if ($PercentComplete -ge 0) {
"progress=$PercentComplete"
}
if ($Completed) {
"state=Completed"
$script:ADOProgressIds.Remove($id)
} else {
"state=InProgress"
}
)
$msg = @(
$Activity
'-'
$Status
if ($CurrentOperation) {
"($CurrentOperation)"
}
if ($SecondsRemaining) {
"(${SecondsRemaining}s remaining)"
}
) -join ' '
$out = "##vso[task.logdetail $($properties -join ';')]$msg"
#endregion Prepare Progress Message
if ($env:Agent_ID -and $DebugPreference -eq 'continue') {
Write-Host $out
} else {
$out
}
}
}