From d0b1e413b0d0f958cb6434a85eaa7939cec062d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bere=C5=BCa=C5=84ski?= Date: Thu, 23 Nov 2017 22:09:33 +0100 Subject: [PATCH] chocolatey-visualstudio.extension: extract Get-VSChannelManifestItem out of Get-VSChannelManifestItemUrl GitHub-Issue: GH-7 GH-8 GH-10 GH-26 --- .../extensions/Get-VSChannelManifestItem.ps1 | 53 +++++++++++++++++ .../Get-VSChannelManifestItemUrl.ps1 | 58 ++++++------------- 2 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 chocolatey-visualstudio.extension/extensions/Get-VSChannelManifestItem.ps1 diff --git a/chocolatey-visualstudio.extension/extensions/Get-VSChannelManifestItem.ps1 b/chocolatey-visualstudio.extension/extensions/Get-VSChannelManifestItem.ps1 new file mode 100644 index 00000000..939fac11 --- /dev/null +++ b/chocolatey-visualstudio.extension/extensions/Get-VSChannelManifestItem.ps1 @@ -0,0 +1,53 @@ +function Get-VSChannelManifestItem +{ + [CmdletBinding()] + Param + ( + [Parameter(Mandatory = $true)] [System.Collections.IDictionary] $Manifest, + [ValidateSet('Bootstrapper', 'Manifest')] [Parameter(Mandatory = $true)] [string] $ChannelItemType + ) + + $result = $null + if ($Manifest -is [Collections.IDictionary] -and $Manifest.ContainsKey('channelItems')) + { + $channelItems = $Manifest['channelItems'] + if ($channelItems -is [array]) + { + $channelItem = $channelItems | Where-Object { $_ -is [Collections.IDictionary] -and $_.ContainsKey('type') -and $_['type'] -eq $ChannelItemType } + if (($channelItem | Measure-Object).Count -eq 1) + { + if ($channelItem -is [Collections.IDictionary]) + { + $result = $channelItem + } + else + { + Write-Debug 'Manifest parsing error: channelItem is not IDictionary' + } + } + else + { + Write-Debug 'Manifest parsing error: zero or more than one channelItem objects found' + } + } + else + { + Write-Debug 'Manifest parsing error: channelItems is not an array' + } + } + else + { + Write-Debug 'Manifest parsing error: manifest is not IDictionary or does not contain channelItems' + } + + if ($result -ne $null) + { + Write-Debug "Located channel manifest item of type $ChannelItemType" + } + else + { + Write-Debug "Could not locate the channel manifest item of type $ChannelItemType" + } + + return $result +} diff --git a/chocolatey-visualstudio.extension/extensions/Get-VSChannelManifestItemUrl.ps1 b/chocolatey-visualstudio.extension/extensions/Get-VSChannelManifestItemUrl.ps1 index 09896c20..d52d2f0a 100644 --- a/chocolatey-visualstudio.extension/extensions/Get-VSChannelManifestItemUrl.ps1 +++ b/chocolatey-visualstudio.extension/extensions/Get-VSChannelManifestItemUrl.ps1 @@ -3,76 +3,54 @@ function Get-VSChannelManifestItemUrl [CmdletBinding()] Param ( - [Parameter(Mandatory = $true)] [hashtable] $Manifest, + [Parameter(Mandatory = $true)] [System.Collections.IDictionary] $Manifest, [ValidateSet('Bootstrapper', 'Manifest')] [Parameter(Mandatory = $true)] [string] $ChannelItemType ) $url = $null $checksum = $null $checksumType = $null - if ($Manifest -is [Collections.IDictionary] -and $Manifest.ContainsKey('channelItems')) + $channelItem = Get-VSChannelManifestItem -Manifest $Manifest -ChannelItemType $ChannelItemType + if ($channelItem -is [Collections.IDictionary] -and $channelItem.ContainsKey('payloads')) { - $channelItems = $Manifest['channelItems'] - if ($channelItems -is [array]) + $payloads = $channelItem['payloads'] + if ($payloads -is [array]) { - $channelItem = $channelItems | Where-Object { $_ -is [Collections.IDictionary] -and $_.ContainsKey('type') -and $_['type'] -eq $ChannelItemType } - if (($channelItem | Measure-Object).Count -eq 1) + if (($payloads | Measure-Object).Count -eq 1) { - if ($channelItem -is [Collections.IDictionary] -and $channelItem.ContainsKey('payloads')) + $payload = $payloads[0] + if ($payload -is [Collections.IDictionary] -and $payload.ContainsKey('url')) { - $payloads = $channelItem['payloads'] - if ($payloads -is [array]) + $url = $payload['url'] + if (-not [string]::IsNullOrEmpty($url) -and $payload.ContainsKey('sha256')) { - if (($payloads | Measure-Object).Count -eq 1) - { - $payload = $payloads[0] - if ($payload -is [Collections.IDictionary] -and $payload.ContainsKey('url')) - { - $url = $payload['url'] - if (-not [string]::IsNullOrEmpty($url) -and $payload.ContainsKey('sha256')) - { - $checksum = $payload['sha256'] - $checksumType = 'sha256' - } - else - { - Write-Debug 'Manifest parsing error: payload url is empty or payload does not contain sha256' - # url will still be returned; it might be useful even without the checksum - } - } - else - { - Write-Debug 'Manifest parsing error: payload is not IDictionary or does not contain url' - } - } - else - { - Write-Debug 'Manifest parsing error: zero or more than one payload objects found' - } + $checksum = $payload['sha256'] + $checksumType = 'sha256' } else { - Write-Debug 'Manifest parsing error: payloads is not an array' + Write-Debug 'Manifest parsing error: payload url is empty or payload does not contain sha256' + # url will still be returned; it might be useful even without the checksum } } else { - Write-Debug 'Manifest parsing error: channelItem is not IDictionary or does not contain payloads' + Write-Debug 'Manifest parsing error: payload is not IDictionary or does not contain url' } } else { - Write-Debug 'Manifest parsing error: zero or more than one channelItem objects found' + Write-Debug 'Manifest parsing error: zero or more than one payload objects found' } } else { - Write-Debug 'Manifest parsing error: channelItems is not an array' + Write-Debug 'Manifest parsing error: payloads is not an array' } } else { - Write-Debug 'Manifest parsing error: manifest is not IDictionary or does not contain channelItems' + Write-Debug 'Manifest parsing error: channelItem is not IDictionary or does not contain payloads' } if (-not [string]::IsNullOrEmpty($url))