Skip to content

Commit

Permalink
ref: Add -Simple to Resolve-ManifestInformation (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash258 committed Dec 26, 2021
1 parent 6154d76 commit 4d879ef
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 35 deletions.
102 changes: 71 additions & 31 deletions lib/manifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -178,32 +178,43 @@ function Get-LocalManifest {
Get "metadata" about local manifest with support for archived manifests.
.PARAMETER Query
Specifies the file path where manifest is stored.
.PARAMETER Simple
Specifies to return limited information about the resolved manifest.
#>
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param([Parameter(Mandatory, ValueFromPipeline)] [String] $Query)
param([Parameter(Mandatory, ValueFromPipeline)] [String] $Query, [Switch] $Simple)

process {
# TODO: Try to implement it without Get-Item, with regex approach
# TODO: In this case the archived manifest regex could not work properly??
$localPath = $reqVersion = $manifest = $null
try {
$manifest = ConvertFrom-Manifest -LiteralPath $Query
$localPath = Get-Item -LiteralPath $Query
} catch {
throw [ScoopException]::new("File is not a valid manifest ($($_.Exception.Message))") # TerminatingError thrown
throw [ScoopException]::new("Cannot get file '$Query'")
}

$localPath = Get-Item -LiteralPath $Query
$applicationName = $localPath.BaseName
$reqVersion = $null

# Check if archived version was provided
if ($localPath.FullName -match $_archivedManifestRegex) {
$applicationName = $Matches['manifestName']
$reqVersion = $manifest.version
$reqVersion = $Matches['manifestVersion']
}
# Check if downloaded manfiest was provided
if ($localPath.Name -match $_localDownloadedRegex) {
$applicationName = $Matches['app']
}

if (!$Simple) {
try {
$manifest = ConvertFrom-Manifest -LiteralPath $localPath.FullName
} catch {
throw [ScoopException]::new("File is not a valid manifest ($($_.Exception.Message))") # TerminatingError thrown
}
}

return @{
'Name' = $applicationName
'RequestedVersion' = $reqVersion
Expand All @@ -220,15 +231,39 @@ function Get-RemoteManifest {
Download manifest from provided URL.
.PARAMETER URL
Specifies the URL pointing to manifest.
.PARAMETER Simple
Specifies to return limited information about the resolved manifest.
#>
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param([Parameter(Mandatory, ValueFromPipeline)] [String] $URL)
param([Parameter(Mandatory, ValueFromPipeline)] [String] $URL, [Switch] $Simple)

process {
# Parse name and extension from URL
# TODO: Will this be enough? Consider more advanced approach
$name = Split-Path $URL -Leaf
$extension = ($name -split '\.')[-1]
$name = $name -replace "\.($ALLOWED_MANIFEST_EXTENSION_REGEX)$"
$requestedVersion = $null

if ($URL -match $_archivedManifestRegex) {
$name = $Matches['manifestName']
$extension = $Matches['manifestExtension']
$requestedVersion = $Matches['manifestVersion']
}

if ($Simple) {
return @{
'Name' = $name
'RequestedVersion' = $requestedVersion
'Print' = $URL
'Manifest' = $null
'Path' = $null
}
}

$str = $null
try {
# TODO: Implement proxy
$wc = New-Object System.Net.Webclient
$wc.Headers.Add('User-Agent', $SHOVEL_USERAGENT)
$str = $wc.DownloadString($URL)
Expand All @@ -244,16 +279,6 @@ function Get-RemoteManifest {

Confirm-DirectoryExistence -Directory $SHOVEL_GENERAL_MANIFESTS_DIRECTORY | Out-Null

# Parse name and extension from URL
$name = Split-Path $URL -Leaf
$extension = ($name -split '\.')[-1]
$name = $name -replace "\.($ALLOWED_MANIFEST_EXTENSION_REGEX)$"

if ($URL -match $_archivedManifestRegex) {
$name = $Matches['manifestName']
$extension = $Matches['manifestExtension']
}

$rand = "$_localAdditionalSeed$(Get-Random)-$(Get-Random)"
$outName = "$name-$rand.$extension"
$manifestFile = Join-Path $SHOVEL_GENERAL_MANIFESTS_DIRECTORY $outName
Expand All @@ -271,10 +296,11 @@ function Get-RemoteManifest {
$manifest = ConvertFrom-Manifest -Path $manifestFile

return @{
'Name' = $name
'Manifest' = $manifest
'Path' = Get-Item -LiteralPath $manifestFile
'Print' = $URL
'Name' = $name
'Manifest' = $manifest
'Path' = Get-Item -LiteralPath $manifestFile
'RequestedVersion' = $requestedVersion
'Print' = $URL
}
}
}
Expand All @@ -285,10 +311,12 @@ function Get-ManifestFromLookup {
Lookup for manifest in all local buckets and return required information.
.PARAMETER Query
Specifies the lookup query.
.PARAMETER Simple
Specifies to return limited information about the resolved manifest.
#>
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param([Parameter(Mandatory, ValueFromPipeline)] [String] $Query)
param([Parameter(Mandatory, ValueFromPipeline)] [String] $Query, [Switch] $Simple)

process {
# Get all requested information
Expand All @@ -298,6 +326,18 @@ function Get-ManifestFromLookup {
$requestedBucket = $null
}
$requestedName, $requestedVersion = $requestedName -split '@'
$printableRepresentation = if ($requestedVersion) { "@$requestedVersion" } else { '' }

if ($Simple) {
return @{
'Name' = $requestedName
'Bucket' = $requestedBucket
'RequestedVersion' = $requestedVersion
'Print' = "$requestedBucket/$requestedName$printableRepresentation"
'Manifest' = $null
'Path' = $null
}
}

# Local manifest with specific name in all buckets
$found = @()
Expand All @@ -324,12 +364,9 @@ function Get-ManifestFromLookup {

$manifestBucket = $valid.Bucket
$manifestPath = $valid.Path
$printableRepresentation = ''

# Select versioned manifest or generate it
if ($requestedVersion) {
$printableRepresentation = "@$requestedVersion"

try {
$path = manifest_path -app $requestedName -bucket $manifestBucket -version $requestedVersion
if ($null -eq $path) { throw 'trigger' }
Expand Down Expand Up @@ -378,6 +415,8 @@ function Resolve-ManifestInformation {
Find and parse manifest file according to search query. Return universal object with all relevant information about manifest.
.PARAMETER ApplicationQuery
Specifies the string used for looking for manifest.
.PARAMETER Simple
Specifies to return limited information about the resolved manifest.
.EXAMPLE
Resolve-ManifestInformation -ApplicationQuery 'pwsh'
Resolve-ManifestInformation -ApplicationQuery 'pwsh@7.2.0'
Expand All @@ -393,29 +432,30 @@ function Resolve-ManifestInformation {
#>
[CmdletBinding()]
[OutputType([PSCustomObject])]
param([Parameter(Mandatory, ValueFromPipeline)] [String] $ApplicationQuery)
param([Parameter(Mandatory, ValueFromPipeline)] [String] $ApplicationQuery, [Switch] $Simple)

process {
$manifest = $applicationName = $applicationVersion = $requestedVersion = $bucket = $localPath = $url = $print = $calcBucket = $calcURL = $null

if (Test-Path -LiteralPath $ApplicationQuery) {
$res = Get-LocalManifest -Query $ApplicationQuery
$res = Get-LocalManifest -Query $ApplicationQuery -Simple:$Simple
$applicationName = $res.Name
$applicationVersion = $res.Manifest.version
$requestedVersion = $res.RequestedVersion
$manifest = $res.Manifest
$localPath = $res.Path
$print = $res.Print
} elseif ($ApplicationQuery -match '^https?://') {
$res = Get-RemoteManifest -URL $ApplicationQuery
$res = Get-RemoteManifest -URL $ApplicationQuery -Simple:$Simple
$applicationName = $res.Name
$applicationVersion = $res.Manifest.version
$requestedVersion = $res.RequestedVersion
$manifest = $res.Manifest
$localPath = $res.Path
$url = $ApplicationQuery
$print = $res.Print
} elseif ($ApplicationQuery -match $_lookupRegex) {
$res = Get-ManifestFromLookup -Query $ApplicationQuery
$res = Get-ManifestFromLookup -Query $ApplicationQuery -Simple:$Simple
$applicationName = $res.Name
$requestedVersion = $res.RequestedVersion
$applicationVersion = $res.Manifest.version
Expand All @@ -430,7 +470,7 @@ function Resolve-ManifestInformation {
debug $res

# TODO: Validate manifest object
if ($null -eq $manifest.version) {
if (!$Simple -and ($null -eq $manifest.version)) {
debug $manifest
throw [ScoopException]::new('Not a valid manifest') # TerminatingError thrown
}
Expand Down
102 changes: 98 additions & 4 deletions test/Shovel-Manifest.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ Describe 'Manifests operations' -Tag 'Scoop' {
It 'Versioned manifest URL' {
$result = Resolve-ManifestInformation 'https://raw.githubusercontent.com/Ash258/GithubActionsBucketForTesting/068225b07cad6baeb46eb1adc26f8207fa423508/bucket/old/alfa/0.0.15-12060.yaml'
$result.ApplicationName | Should -Be 'alfa'
$result.RequestedVersion | Should -Be '0.0.15-12060'
$result.Version | Should -Be '0.0.15-12060'
$result.ManifestObject.bin | Should -Be 'rpcs3.exe'
$result = $null

$result = Resolve-ManifestInformation 'https://raw.githubusercontent.com/Ash258/GithubActionsBucketForTesting/8117ddcbadc606f5d4576778676e81bfc6dc2e78/bucket/old/aaaaa/0.0.15-11936.json'
$result.ApplicationName | Should -Be 'aaaaa'
$result.RequestedVersion | Should -Be '0.0.15-11936'
$result.Version | Should -Be '0.0.15-11936'
$result.ManifestObject.bin | Should -Be 'rpcs3.exe'
$result = $null
Expand Down Expand Up @@ -291,10 +293,102 @@ Describe 'Manifests operations' -Tag 'Scoop' {
}
}

It 'Not supported query' {
{ Resolve-ManifestInformation '@@cosi@@' } | Should -Throw 'Not supported way how to provide manifest'
{ Resolve-ManifestInformation '@1.2.5.8' } | Should -Throw 'Not supported way how to provide manifest'
{ Resolve-ManifestInformation 'ftp://test.json' } | Should -Throw 'Not supported way how to provide manifest'
Describe 'Resolve-ManifestInformation -Simple' {
It 'All types' {
# Local path
$result = Resolve-ManifestInformation "$working_dir\bucket\pwsh.json" -Simple
$result.ApplicationName | Should -Be 'pwsh'
$result = $null

$result = Resolve-ManifestInformation "$working_dir\bucket/cosi.yaml"
$result.ApplicationName | Should -Be 'cosi'
$result = $null

$result = Resolve-ManifestInformation "$working_dir\bucket\old\pwsh\6.2.3.yml"
$result.ApplicationName | Should -Be 'pwsh'
$result.RequestedVersion | Should -Be '6.2.3'
$result = $null

$result = Resolve-ManifestInformation "$working_dir\bucket\old\cosi\7.1.0.yaml"
$result.ApplicationName | Should -Be 'cosi'
$result.RequestedVersion | Should -Be '7.1.0'
$result = $null

$result = Resolve-ManifestInformation "$working_dir\bucket/old\cosi/7.1.0.yaml"
$result.ApplicationName | Should -Be 'cosi'
$result.RequestedVersion | Should -Be '7.1.0'
$result = $null

# Local usermanifest
$path = Get-ChildItem $SHOVEL_GENERAL_MANIFESTS_DIRECTORY -File | Select-Object -First 1

# TODO: Is this relatable?
$result = Resolve-ManifestInformation $path.FullName -Simple
$result.ApplicationName | Should -Be 'aaaaa'
$result = $null

$result = Resolve-ManifestInformation "$($working_dir -replace '\\', '/')/bucket/old/cosi/7.1.0.yaml"
$result.ApplicationName | Should -Be 'cosi'
$result.RequestedVersion | Should -Be '7.1.0'
$result = $null

# Simple lookup
$result = Resolve-ManifestInformation 'cosi' -Simple
$result.ApplicationName | Should -Be 'cosi'
$result.Bucket | Should -BeNullOrEmpty
$result = $null

$result = Resolve-ManifestInformation 'pwsh@1.2.4' -Simple
$result.ApplicationName | Should -Be 'pwsh'
$result.RequestedVersion | Should -Be '1.2.4'
$result.Bucket | Should -BeNullOrEmpty
$result = $null

# Bucket lookup
$result = Resolve-ManifestInformation 'main/pwsh@7.0.6' -Simple 6>$null
$result.ApplicationName | Should -Be 'pwsh'
$result.Print | Should -Be 'main/pwsh@7.0.6'
$result.RequestedVersion | Should -Be '7.0.6'
$result.Bucket | Should -Be 'main'
$result = $null

# URL lookup
$result = Resolve-ManifestInformation 'https://raw.githubusercontent.com/Ash258/GithubActionsBucketForTesting/184d2f072798441e8eb03a655dea16f2695ee699/bucket/alfa.yaml' -Simple
$result.ApplicationName | Should -Be 'alfa'
$result = $null

$result = Resolve-ManifestInformation 'https://raw.githubusercontent.com/Ash258/GithubActionsBucketForTesting/068225b07cad6baeb46eb1adc26f8207fa423508/bucket/old/alfa/0.0.15-12060.yaml' -Simple
$result.ApplicationName | Should -Be 'alfa'
$result.RequestedVersion | Should -Be '0.0.15-12060'
$result = $null

$result = Resolve-ManifestInformation 'https://raw.githubusercontent.com/Ash258/GithubActionsBucketForTesting/8117ddcbadc606f5d4576778676e81bfc6dc2e78nonExistentURL/bucket/old/aaaaa/0.0.15-11936.json' -Simple
$result.ApplicationName | Should -Be 'aaaaa'
$result.RequestedVersion | Should -Be '0.0.15-11936'
$result = $null

# Even non-exitent stuff should work
$result = Resolve-ManifestInformation 'nonexistent/cosi' -Simple
$result.ApplicationName | Should -Be 'cosi'
$result.Bucket | Should -Be 'nonexistent'
$result = $null

$result = Resolve-ManifestInformation 'main/tryharder' -Simple
$result.ApplicationName | Should -Be 'tryharder'
$result.Bucket | Should -Be 'main'

# Error state
{ Resolve-ManifestInformation '@@cosi@@' -Simple } | Should -Throw 'Not supported way how to provide manifest'
{ Resolve-ManifestInformation '@@cosi@@' -Simple } | Should -Throw 'Not supported way how to provide manifest'
{ Resolve-ManifestInformation '@1.2.5.8' -Simple } | Should -Throw 'Not supported way how to provide manifest'
{ Resolve-ManifestInformation 'ws://test.json' -Simple } | Should -Throw 'Not supported way how to provide manifest'
}

It 'Not supported query' {
{ Resolve-ManifestInformation '@@cosi@@' } | Should -Throw 'Not supported way how to provide manifest'
{ Resolve-ManifestInformation '@1.2.5.8' } | Should -Throw 'Not supported way how to provide manifest'
{ Resolve-ManifestInformation 'ws://test.json' } | Should -Throw 'Not supported way how to provide manifest'
}
}
}
}

0 comments on commit 4d879ef

Please sign in to comment.