-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCobalt.ps1
451 lines (421 loc) · 19.2 KB
/
Cobalt.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
$BaseOriginalName = 'WinGet'
$BaseOriginalCommandElements = @()
$BaseParameters = @()
$BaseOutputHandlers = @{
ParameterSetName = 'Default'
Handler = {
param ( $output )
}
}
$outputHanderHeader = {param ($output)}
$i18nHandlerHelper = {
$language = (Get-UICulture).Name
$languageData = $(
$hash = @{}
$(try {
# We have to trim the leading BOM for .NET's XML parser to correctly read Microsoft's own files - go figure
([xml](((Invoke-WebRequest -Uri "https://raw.githubusercontent.com/microsoft/winget-cli/v1.3.2691/Localization/Resources/$language/winget.resw" -ErrorAction Stop ).Content -replace "\uFEFF", ""))).root.data
} catch {
# Fall back to English if a locale file doesn't exist
(
('SearchName','Name'),
('SearchID','Id'),
('SearchVersion','Version'),
('AvailableHeader','Available'),
('SearchSource','Source'),
('ShowVersion','Version'),
('GetManifestResultVersionNotFound','No version found matching:'),
('InstallerFailedWithCode','Installer failed with exit code:'),
('UninstallFailedWithCode','Uninstall failed with exit code:'),
('AvailableUpgrades','upgrades available.')
) | ForEach-Object {[pscustomobject]@{name = $_[0]; value = $_[1]}}
}) | ForEach-Object {
# Convert the array into a hashtable
$hash[$_.name] = $_.value
}
$hash
)
}
$GetPackageOutputHandler = {
$nameHeader = $output -Match "^$($languageData.SearchName)"
if ($nameHeader) {
$headerLine = $output.IndexOf(($nameHeader | Select-Object -First 1))
if ($headerLine -ne -1) {
$idIndex = $output[$headerLine].IndexOf(($languageData.SearchID))
$versionIndex = $output[$headerLine].IndexOf(($languageData.SearchVersion))
$availableIndex = $output[$headerLine].IndexOf(($languageData.AvailableHeader))
$sourceIndex = $output[$headerLine].IndexOf(($languageData.SearchSource))
# Stop gathering version data at the 'Available' column if it exists, if not continue on to the 'Source' column (if it exists)
$versionEndIndex = $(
if ($availableIndex -ne -1) {
$availableIndex
} else {
$sourceIndex
}
)
# Only attempt to parse output if it contains a 'version' column
if ($versionIndex -ne -1) {
# The -replace cleans up errant characters that come from WinGet's poor treatment of truncated columnar output
($output | Select-String -Pattern $languageData.AvailableUpgrades,'--include-unknown' -NotMatch) -replace '[^i\p{IsBasicLatin}]+',' ' | Select-Object -Skip ($headerLine+2) | ForEach-Object {
Remove-Variable -Name 'package' -ErrorAction SilentlyContinue
$package = [ordered]@{
ID = $_.SubString($idIndex,$versionIndex-$idIndex).Trim()
}
if ($package) {
# I'm so sorry, blame WinGet
# If neither the 'Available' or 'Source' column exist, gather version data to the end of the string
$package.Version = $(
if ($versionEndIndex -ne -1) {
$_.SubString($versionIndex,$versionEndIndex-$versionIndex)
} else {
$_.SubString($versionIndex)
}
).Trim() -replace '[^\.\d]'
# Only attempt to add 'Available Version' data if the column exists
if ($availableIndex -ne -1) {
$package.Available = $(
if ($sourceIndex -ne -1) {
$_.SubString($availableIndex,$sourceIndex-$availableIndex)
} else {
$_.SubString($availableIndex)
}
).Trim() -replace '[^\.\d]'
}
# If the 'Source' column was included in the output, include it in our output, too
if (($sourceIndex -ne -1) -And ($_.Length -ge $sourceIndex)) {
$package.Source = $_.SubString($sourceIndex).Trim() -split ' ' | Select-Object -Last 1
}
[pscustomobject]$package
}
}
}
}
}
}
$InstallPackageOutputHandler = {
if ($output) {
if ($output -match $languageData.InstallerFailedWithCode) {
# Only show output that matches or comes after the 'failed' keyword
Write-Error ($output[$output.IndexOf($($output -match $languageData.InstallerFailedWithCode | Select-Object -First 1))..($output.Length-1)] -join "`r`n")
} else {
$output | ForEach-Object {
if ($_ -match 'Found .+ \[(?<id>[\S]+)\] Version (?<version>[\S]+)' -and $Matches.id -and $Matches.version) {
[pscustomobject]@{
ID = $Matches.id
Version = $Matches.version
}
}
}
}
}
}
$UnInstallPackageOutputHandler = {
if ($output) {
if ($output -match $languageData.UninstallFailedWithCode) {
# Only show output that matches or comes after the 'failed' keyword
Write-Error ($output[$output.IndexOf($($output -match $languageData.UninstallFailedWithCode | Select-Object -First 1))..($output.Length-1)] -join "`r`n")
}
}
}
$PackageInfoVersionOutputHandler = {
if ($output) {
if ($output | Select-String -Pattern $languageData.GetManifestResultVersionNotFound) {
# Only show output that matches or comes after the 'failed' keyword
Write-Error ($output[$output.IndexOf($($output | Select-String -Pattern $languageData.GetManifestResultVersionNotFound | Select-Object -First 1))..($output.Length-1)] -join "`r`n")
} else {
$versionHeader = $output -Match "^$($languageData.ShowVersion)"
if ($versionHeader) {
$headerLine = $output.IndexOf(($versionHeader | Select-Object -First 1))
if ($headerLine -ne -1) {
$output | Select-Object -Skip ($headerLine+2)
}
}
}
}
}
$RenderedGetPackageOutputHandler = [scriptblock]::Create($outputHanderHeader.ToString() + $i18nHandlerHelper.ToString() + $GetPackageOutputHandler.ToString())
$RenderedInstallPackageOutputHandler = [scriptblock]::Create($outputHanderHeader.ToString() + $i18nHandlerHelper.ToString() + $InstallPackageOutputHandler.ToString())
$RenderedUnInstallPackageOutputHandler = [scriptblock]::Create($outputHanderHeader.ToString() + $i18nHandlerHelper.ToString() + $UnInstallPackageOutputHandler.ToString())
$RenderedPackageInfoVersionOutputHandler = [scriptblock]::Create($outputHanderHeader.ToString() + $i18nHandlerHelper.ToString() + $PackageInfoVersionOutputHandler.ToString())
# The general structure of this hashtable is to define noun-level attributes, which are -probably- common across all commands for the same noun, but still allow for customization at more specific verb-level defition for that noun.
# The following three command attributes have the following order of precedence:
# OriginalCommandElements will be MERGED in the order of Noun + Verb + Base
# Example: Noun WinGetSource's element 'source', Verb Register's element 'add', and Base elements are merged to become 'WinGet source add --limit-output --yes'
# Parameters will be MERGED in the order of Noun + Verb + Base
# Example: Noun WinGetPackage's parameters for package name and version and Verb Install's parameter specifying source information are merged to become '<packageName> --version=<packageVersion> --source=<packageSource>'.
# These are then appended to the merged original command elements, to create 'WinGet install <packageName> --version=<packageVersion> --source=<packageSource> --limit-output --yes'
# OutputHandler sets will SUPERCEDE each other in the order of: Verb -beats-> Noun -beats-> Base. This allows reusability of PowerShell parsing code.
# Example: Noun WinGetPackage has inline output handler PowerShell code with complex regex that works for both Install-WinGetPackage and Uninstall-WinGetPackage, but Get-WinGetPackage's native output uses simple vertical bar delimiters.
# Example 2: The native commands for Register-WinGetSource and Unregister-WinGetSource don't return any output, and until Crescendo supports error handling by exit codes, a base required default output handler that doesn't do anything can be defined and reused in multiple places.
$Commands = @(
@{
Noun = 'WinGetSource'
OriginalCommandElements = @('source')
Verbs = @(
@{
Verb = 'Get'
Description = 'Return WinGet package sources'
OriginalCommandElements = @('export')
Parameters = @(
@{
Name = 'Name'
ParameterType = 'string'
Description = 'Source Name'
OriginalName = '--name='
NoGap = $true
}
)
OutputHandlers = @{
ParameterSetName = 'Default'
Handler = {
param ($output)
if ($output) {
$output | ConvertFrom-Json
}
}
}
},
@{
Verb = 'Register'
Description = 'Register a new WinGet package source'
OriginalCommandElements = @('add')
Parameters = @(
@{
Name = 'Name'
ParameterType = 'string'
Description = 'Source Name'
OriginalName = '--name='
NoGap = $true
Mandatory = $true
},
@{
Name = 'Argument'
OriginalName = '--arg='
ParameterType = 'string'
Description = 'Source Argument'
NoGap = $true
Mandatory = $true
}
)
OutputHandlers = @{
ParameterSetName = 'Default'
Handler = {
param ($output)
if ($output) {
if ($output[-1] -ne 'Done') {
Write-Error ($output -join "`r`n")
}
}
}
}
},
@{
Verb = 'Unregister'
Description = 'Unegister an existing WinGet package source'
OriginalCommandElements = @('remove')
Parameters = @(
@{
Name = 'Name'
ParameterType = 'string'
Description = 'Source Name'
OriginalName = '--name='
NoGap = $true
Mandatory = $true
ValueFromPipelineByPropertyName = $true
}
)
OutputHandlers = @{
ParameterSetName = 'Default'
Handler = {
param ($output)
if ($output) {
if ($output[-1] -match 'Did not find a source') {
Write-Error ($output -join "`r`n")
}
}
}
}
}
)
},
@{
Noun = 'WinGetPackage'
Parameters = @(
@{
Name = 'ID'
OriginalName = '--id='
ParameterType = 'string'
Description = 'Package ID'
NoGap = $true
ValueFromPipelineByPropertyName = $true
},
@{
Name = 'Exact'
OriginalName = '--exact'
ParameterType = 'switch'
Description = 'Search by exact package name'
},
@{
Name = 'Source'
OriginalName = '--source='
ParameterType = 'string'
Description = 'Package Source'
NoGap = $true
ValueFromPipelineByPropertyName = $true
}
)
OutputHandlers = @{
ParameterSetName = 'Default'
Handler = $RenderedGetPackageOutputHandler
}
Verbs = @(
@{
Verb = 'Install'
Description = 'Install a new package with WinGet'
OriginalCommandElements = @('install','--accept-package-agreements','--accept-source-agreements','--silent')
Parameters = @(
@{
Name = 'Version'
OriginalName = '--version='
ParameterType = 'string'
Description = 'Package Version'
NoGap = $true
ValueFromPipelineByPropertyName = $true
}
)
OutputHandlers = @{
ParameterSetName = 'Default'
Handler = $RenderedInstallPackageOutputHandler
}
},
@{
Verb = 'Get'
Description = 'Get a list of installed WinGet packages'
OriginalCommandElements = @('list','--accept-source-agreements')
},
@{
Verb = 'Find'
Description = 'Find a list of available WinGet packages'
OriginalCommandElements = @('search','--accept-source-agreements')
},
@{
Verb = 'Update'
Description = 'Updates an installed package to the latest version'
OriginalCommandElements = @('upgrade','--accept-source-agreements','--silent')
Parameters = @(
@{
Name = 'All'
OriginalName = '--all'
ParameterType = 'switch'
Description = 'Upgrade all packages'
}
)
OutputHandlers = @{
ParameterSetName = 'Default'
Handler = $RenderedInstallPackageOutputHandler
}
},
@{
Verb = 'Uninstall'
Description = 'Uninstall an existing package with WinGet'
OriginalCommandElements = @('uninstall','--accept-source-agreements','--silent')
OutputHandlers = @{
ParameterSetName = 'Default'
Handler = $RenderedUnInstallPackageOutputHandler
}
}
)
},
@{
Noun = 'WinGetPackageInfo'
Verbs = @(
@{
Verb = 'Get'
Description = 'Shows information on a specific WinGet package'
OriginalCommandElements = @('show','--accept-source-agreements')
DefaultParameterSetName = 'Default'
Parameters = @(
@{
Name = 'ID'
OriginalName = '--id='
ParameterType = 'string'
Description = 'Package ID'
NoGap = $true
Mandatory = $true
ValueFromPipelineByPropertyName = $true
Position = 0
ParameterSetName = @('Default','Versions')
},
@{
Name = 'Exact'
OriginalName = '--exact'
ParameterType = 'switch'
Description = 'Search by exact package name'
ParameterSetName = @('Default','Versions')
},
@{
Name = 'Version'
OriginalName = '--version='
ParameterType = 'string'
Description = 'Package Version'
NoGap = $true
ValueFromPipelineByPropertyName = $true
ParameterSetName = @('Default','Versions')
},
@{
Name = 'Source'
OriginalName = '--source='
ParameterType = 'string'
Description = 'Package Source'
NoGap = $true
ValueFromPipelineByPropertyName = $true
ParameterSetName = @('Default','Versions')
},
@{
Name = 'Versions'
OriginalName = '--versions'
ParameterType = 'switch'
Description = 'Show available versions of the package'
ParameterSetName = 'Versions'
}
)
OutputHandlers = @(
@{
ParameterSetName = 'Default'
Handler = {
param ( $output )
$packageInfo = @{}
$output | Select-String -AllMatches -Pattern '^\s*([\w\s]+):\s(.+)$' | ForEach-Object -MemberName Matches | ForEach-Object{
$match = ($_.Groups | Select-Object -Skip 1).Value
$packageInfo.add($match[0],$match[1])
}
$packageInfo
}
},
@{
ParameterSetName = 'Versions'
Handler = $RenderedPackageInfoVersionOutputHandler
}
)
}
)
},
@{
Noun = 'WinGetPackageUpdate'
OutputHandlers = @{
ParameterSetName = 'Default'
Handler = $RenderedGetPackageOutputHandler
}
Verbs = @(
@{
Verb = 'Get'
Description = 'Get a list of installed WinGet packages'
# Add this back in after WinGet 1.3 is released
# https://github.com/microsoft/winget-cli/issues/1869
# https://github.com/microsoft/winget-cli/pull/1874
# OriginalCommandElements = @('upgrade','--accept-source-agreements')
OriginalCommandElements = @('upgrade')
}
)
}
)