Skip to content

Commit

Permalink
refactor(Manifest): Support remote manifests in Resolve (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash258 authored May 27, 2021
1 parent 223974d commit 4a2ba6b
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
83 changes: 83 additions & 0 deletions lib/manifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ function New-VersionedManifest {
$_br = '[/\\]'
$_archivedManifestRegex = "${_br}bucket${_br}old${_br}(?<manifestName>.+?)${_br}(?<manifestVersion>.+?)\.(?<manifestExtension>$ALLOWED_MANIFEST_EXTENSION_REGEX)$"

$_bucketLookup = '(?<bucket>[a-zA-Z\d.-]+)'
$_applicationLookup = '(?<app>[a-zA-Z\d_.-]+)'
$_versionLookup = '@(?<version>.+)'
$_lookupRegex = "^($_bucketLookup/)?$_applicationLookup($_versionLookup)?$"

$_localAdditionalSeed = '258258--' # Everything before this seed and dash is considered as manifest name
$_localDownloadedRegex = "^$_applicationLookup-$_localAdditionalSeed.*\.($ALLOWED_MANIFEST_EXTENSION_REGEX)$"

function Get-LocalManifest {
<#
.SYNOPSIS
Expand All @@ -172,6 +180,10 @@ function Get-LocalManifest {
if ($localPath.FullName -match $_archivedManifestRegex) {
$applicationName = $Matches['manifestName']
}
# Check if downloaded manfiest was provided
if ($localPath.Name -match $_localDownloadedRegex) {
$applicationName = $Matches['app']
}

return @{
'Name' = $applicationName
Expand All @@ -180,6 +192,70 @@ function Get-LocalManifest {
}
}
}

function Get-RemoteManifest {
<#
.SYNOPSIS
Download manifest from provided URL.
.PARAMETER URL
Specifies the URL pointing to manifest.
#>
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param([Parameter(Mandatory, ValueFromPipeline)] [String] $URL)

process {
$str = $null
try {
# TODO: Implement proxy
$wc = New-Object System.Net.Webclient
$wc.Headers.Add('User-Agent', (Get-UserAgent))
$str = $wc.DownloadString($URL)
} catch [System.Management.Automation.MethodInvocationException] {
Write-UserMessage -Message "${URL}: $($_.Exception.InnerException.Message)" -Warning
} catch {
throw $_.Exception.Message
}

if (!$str) {
throw [ScoopException] "'$URL' does not contain valid manifest" # TerminatingError thrown
}

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

# This use case should never happen. The probability should be really low.
if (Test-Path $manifestFile) {
# TODO: Consider while loop when it could be considered as real issue
$new = "$name-$rand-$(Get-Random)"
$manifestFile = Join-Path $SHOVEL_GENERAL_MANIFESTS_DIRECTORY "$new.$extension"

Write-UserMessage -Message "Downloaded manifest file with name '$outName' already exists. Using '$new.$extension'." -Warning
}

Out-UTF8File -Path $manifestFile -Content $str
$manifest = ConvertFrom-Manifest -Path $manifestFile

return @{
'Name' = $name
'Manifest' = $manifest
'Path' = Get-Item -LiteralPath $manifestFile
}
}
}
#endregion Resolve Helpers

function Resolve-ManifestInformation {
Expand Down Expand Up @@ -214,6 +290,13 @@ function Resolve-ManifestInformation {
$applicationVersion = $res.Manifest.version
$manifest = $res.Manifest
$localPath = $res.Path
} elseif ($ApplicationQuery -match '^https?://') {
$res = Get-RemoteManifest -URL $ApplicationQuery
$applicationName = $res.Name
$applicationVersion = $res.Manifest.version
$manifest = $res.Manifest
$localPath = $res.Path
$url = $ApplicationQuery
} else {
throw 'Not supported way how to provide manifest'
}
Expand Down
45 changes: 45 additions & 0 deletions test/Shovel-Manifest.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,51 @@ Describe 'Manifests operations' -Tag 'Scoop' {
}
}

Describe 'Get-RemoteManifest' {
# TODO: Mockup to not download the file

It 'Direct manifest URL' {
$result = Resolve-ManifestInformation 'https://raw.githubusercontent.com/Ash258/GithubActionsBucketForTesting/068225b07cad6baeb46eb1adc26f8207fa423508/bucket/aaaaa.json'
$result.ApplicationName | Should -Be 'aaaaa'
$result.Version | Should -Be '0.0.15-12154'
$result.ManifestObject.checkver.github | Should -Be 'https://github.com/RPCS3/rpcs3-binaries-win'
$result = $null

$result = Resolve-ManifestInformation 'https://raw.githubusercontent.com/Ash258/GithubActionsBucketForTesting/184d2f072798441e8eb03a655dea16f2695ee699/bucket/alfa.yaml'
$result.ApplicationName | Should -Be 'alfa'
$result.Version | Should -Be '0.0.15-12154'
$result.ManifestObject.checkver.github | Should -Be 'https://github.com/RPCS3/rpcs3-binaries-win'
$result = $null
}

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.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.Version | Should -Be '0.0.15-11936'
$result.ManifestObject.bin | Should -Be 'rpcs3.exe'
$result = $null
}

It 'Downloaded manifest load' {
$result = Resolve-ManifestInformation 'https://raw.githubusercontent.com/Ash258/GithubActionsBucketForTesting/184d2f072798441e8eb03a655dea16f2695ee699/bucket/alfa.yaml'
$result.ApplicationName | Should -Be 'alfa'
$result.Version | Should -Be '0.0.15-12154'
$result.ManifestObject.checkver.github | Should -Be 'https://github.com/RPCS3/rpcs3-binaries-win'

$resultNew = Resolve-manifestInformation $result.LocalPath
$resultNew.ApplicationName | Should -Be 'alfa'
$resultNew.LocalPath.Basename | Should -BeLike 'alfa-258258--*'
$resultNew.Version | Should -Be '0.0.15-12154'
$result = $resultNew = $null
}
}

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'
Expand Down

0 comments on commit 4a2ba6b

Please sign in to comment.