-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathposh-vsts-cli.psm1
211 lines (200 loc) · 5.79 KB
/
posh-vsts-cli.psm1
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<#
.SYNOPSIS
Installs the VSTS CLI. Requires PowerShell to be launched as Administrator.
.EXAMPLE
Install-VstsCli
#>
Function Install-VstsCli
{
[CmdletBinding()]
param()
Import-Module BitsTransfer # in case the machine is locked down, one can use Invoke-WebRequest instead as well but BitsTransfer is much faster
Write-Verbose "Downloading installer" -Verbose
Start-BitsTransfer -Source https://aka.ms/vsts-cli-windows-installer -Destination vsts-cli_installer.msi
Write-Verbose "Installing VSTS-CLI" -Verbose
$result = Start-Process msiexec.exe -Wait -ArgumentList "/I $((Get-ChildItem .\vsts-cli_installer.msi).FullName) /quiet" -PassThru -Verb runas
Write-Verbose "Installer Exit Code: $($result.ExitCode)"
# refresh path in powershell https://gist.github.com/bill-long/230830312b70742321e0
foreach($level in "Machine","User") {
[Environment]::GetEnvironmentVariables($level).GetEnumerator() | ForEach-Object {
# For Path variables, append the new values, if they're not already in there
if($_.Name -match 'Path$') {
$_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select-Object -unique) -join ';'
}
$_
} | Set-Content -Path { "Env:$($_.Name)" }
}
Remove-Item .\vsts-cli_installer.msi
vsts --version
}
<#
.SYNOPSIS
Invokes the VSTS CLI and converts the output to a PowerShell object.
.EXAMPLE
ivc build list
.EXAMPLE
ivc build list --output table
#>
Function Invoke-VstsCli
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingInvokeExpression", "")]
param()
$arguments = $args -join " "
& Invoke-Expression "vsts $($arguments)" | ConvertFrom-VstsCli
}
<#
.SYNOPSIS
Converts the JSON or table output of the vsts-cli output to PowerShell objects
.EXAMPLE
vsts build list | ConvertFrom-VstsCli
.EXAMPLE
vsts build list --output table | ConvertFrom-VstsCli
#>
function ConvertFrom-VstsCli
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $True, ValueFromPipeline = $True)]
[string]$InputObject
)
begin
{
$positions = $null;
$scippedSecondLine = $false;
}
process
{
if ($null -eq $isTableFormat)
{
$isTableFormat = $InputObject.startswith("[")
}
if (-not $isTableFormat)
{
foreach ($oneInputObject in $InputObject)
{
if ($null -eq $positions)
{
$positions = GetColumnInfo -HeaderRow $oneInputObject
}
else
{
if ($scippedSecondLine)
{
ParseRow -row $oneInputObject -ColumnInfo $positions
}
else
{
$scippedSecondLine = $true # the second line consists only of dashes
}
}
}
}
else
{
# the JSON string comes in one character at a time in the pipeline
if ($null -eq $stringBuilder)
{
$stringBuilder = [System.Text.StringBuilder]::new()
}
foreach($oneInputObject in $InputObject)
{
[void]$stringBuilder.AppendLine($oneInputObject)
}
}
}
end
{
if ($null -ne $stringBuilder)
{
$stringBuilder.ToString() | ConvertFrom-Json
}
$isTableFormat = $null
}
}
#region Table output parsing
function PascalName($name)
{
$name = $name.Trim(' ') # get rid of leading whitespace
$parts = $name.Split(" ")
for ($i = 0 ; $i -lt $parts.Length ; $i++)
{
$parts[$i] = [char]::ToUpper($parts[$i][0]) + $parts[$i].SubString(1).ToLower();
}
$parts -join ""
}
function GetHeaderBreak($headerRow, $startPoint = 0)
{
$i = $startPoint
while ( $i + 1 -lt $headerRow.Length)
{
if ($headerRow[$i] -eq ' ' -and $headerRow[$i + 1] -eq ' ')
{
return $i
break
}
$i += 1
}
return -1
}
function GetHeaderNonBreak($headerRow, $startPoint = 0)
{
$i = $startPoint
while ( $i + 1 -lt $headerRow.Length)
{
if ($headerRow[$i] -ne ' ')
{
return $i
break
}
$i += 1
}
return -1
}
function GetColumnInfo($headerRow)
{
$lastIndex = 2 # the first 2 characters are just whitespace
$i = 4 # id starts at 2
while ($i -lt $headerRow.Length)
{
$i = GetHeaderBreak $headerRow $lastIndex
if ($i -lt 0)
{
$name = $headerRow.Substring($lastIndex)
New-Object PSObject -Property @{ HeaderName = $name; Name = PascalName $name; Start = $lastIndex; End = -1}
break
}
else
{
$name = $headerRow.Substring($lastIndex, $i - $lastIndex)
$temp = $lastIndex
$lastIndex = GetHeaderNonBreak $headerRow $i
if ($temp -eq 2) # the first ID columns is right aligned -> move position to the very left
{
$temp = 0
}
if ($lastIndex -lt 0)
{
$lastIndex = $headerRow.Length - 1 # last columns sometimes 'overflows'
}
New-Object PSObject -Property @{ HeaderName = $name; Name = PascalName $name; Start = $temp; End = $lastIndex}
}
}
}
function ParseRow($row, $columnInfo)
{
$values = @{}
$columnInfo | ForEach-Object {
if ($_.End -lt 0)
{
$len = $row.Length - $_.Start
}
else
{
$len = $_.End - $_.Start
}
$values[$_.Name] = $row.SubString($_.Start, $len).Trim()
}
New-Object PSObject -Property $values
}
#endregion