From 1f3901e8def12e2633a900688cd2d1a11915026b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C4=8C=C3=A1bera?= Date: Sat, 16 Jan 2021 19:30:57 +0100 Subject: [PATCH] refactor: Support YAML typed manifests in some executables (#95) --- CHANGELOG.md | 1 + lib/Search.ps1 | 6 +++--- lib/buckets.ps1 | 5 ++++- lib/core.ps1 | 5 ++++- lib/install.ps1 | 3 ++- lib/manifest.ps1 | 43 +++++++++++++++++++++++++++++++++--------- libexec/scoop-list.ps1 | 3 ++- 7 files changed, 50 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a58ce1ebd7..11ab5fdca5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### 0.6-pre1 +- Support YAML typed manifests in some commands - **virustotal**: Command now works again with V3 API - Requires Api key for all operations - **decompress**: Add `Expand-ZstdArchive` function for extracting standalone zstd archives diff --git a/lib/Search.ps1 b/lib/Search.ps1 index c94bc35837..c76efa080f 100644 --- a/lib/Search.ps1 +++ b/lib/Search.ps1 @@ -79,7 +79,7 @@ function Search-RemoteBucket { debug $rateLimitRemaining if ($rateLimitRemaining -eq 0) { Test-GithubApiRateLimitBreached -Breach | Out-Null } } - $result = $response.tree | Where-Object -Property 'path' -Match "(^(?:bucket/)?(.*$Query.*)\.json$)" | ForEach-Object { $Matches[2] } + $result = $response.tree | Where-Object -Property 'path' -Match "(^(?:bucket/)?(.*$Query.*)\.($ALLOWED_MANIFEST_EXTENSION_REGEX)$)" | ForEach-Object { $Matches[2] } } return $result @@ -149,9 +149,9 @@ function Search-LocalBucket { 'name' = $app 'version' = $manifest.version 'description' = $manifest.description - 'bin' = @(arch_specific 'bin' $manifest $arch) + 'bin' = @(arch_specific 'bin' $manifest $architecture) 'matchingBinaries' = @() - 'shortcuts' = @(arch_specific 'shortcuts' $manifest $arch) + 'shortcuts' = @(arch_specific 'shortcuts' $manifest $architecture) 'matchingShortcuts' = @() } } diff --git a/lib/buckets.ps1 b/lib/buckets.ps1 index 7ac9f36c55..dd7718896a 100644 --- a/lib/buckets.ps1 +++ b/lib/buckets.ps1 @@ -67,7 +67,10 @@ function Get-KnownBucket { } function apps_in_bucket($dir) { - return Get-ChildItem $dir | Where-Object { $_.Name.EndsWith('.json') } | ForEach-Object { $_.Name -replace '.json$' } + $files = Get-ChildItem $dir -File + $allowed = $files | Where-Object -Property 'Extension' -Match -Value "\.($ALLOWED_MANIFEST_EXTENSION_REGEX)$" + + return $allowed.BaseName } function find_manifest($app, $bucket) { diff --git a/lib/core.ps1 b/lib/core.ps1 index 16f233b388..80a99cfd99 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -395,6 +395,7 @@ function app_status($app, $global) { return $status } +# TODO: YML function appname_from_url($url) { return (Split-Path $url -Leaf) -replace '\.json$' } # paths @@ -438,7 +439,7 @@ function ensure { param([Parameter(Mandatory, ValueFromPipeline)] [Alias('Dir', 'Path', 'LiteralPath')] $Directory) process { - if (!(Test-Path $Directory)) { New-Item $Directory -ItemType Directory | Out-Null } + if (!(Test-Path $Directory)) { New-Item $Directory -ItemType 'Directory' | Out-Null } return Resolve-Path $Directory } @@ -860,6 +861,8 @@ function applist($apps, $global, $bucket = $null) { } function parse_app([string] $app) { + # TODO: YAML + # if ($app -match "(?:(?[a-zA-Z0-9-]+)\/)?(?.*\.$ALLOWED_MANIFESTS_EXTENSIONS_REGEX$|[a-zA-Z0-9-_.]+)(?:@(?.*))?") { if ($app -match '(?:(?[a-zA-Z0-9-]+)\/)?(?.*.json$|[a-zA-Z0-9-_.]+)(?:@(?.*))?') { return $matches['app'], $matches['bucket'], $matches['version'] } diff --git a/lib/install.ps1 b/lib/install.ps1 index 90921f85b4..1e20e69862 100644 --- a/lib/install.ps1 +++ b/lib/install.ps1 @@ -125,8 +125,9 @@ function Find-Manifest($app, $bucket) { $manifest, $bucket = find_manifest $app $bucket if (!$manifest) { - # Couldn't find app in buckets: check if it's a local path + # Could not find app in buckets: check if it's a local path $path = $app + # TODO: YAML if (!$path.endswith('.json')) { $path += '.json' } if (Test-Path $path) { $url = "$(Resolve-Path $path)" diff --git a/lib/manifest.ps1 b/lib/manifest.ps1 index 4d2229537d..f9512952f0 100644 --- a/lib/manifest.ps1 +++ b/lib/manifest.ps1 @@ -5,6 +5,7 @@ Join-Path $PSScriptRoot '..\supporting\yaml\bin\powershell-yaml.psd1' | Import-Module -Prefix 'CloudBase' -Verbose:$false $ALLOWED_MANIFEST_EXTENSION = @('json', 'yaml', 'yml') +$ALLOWED_MANIFEST_EXTENSION_REGEX = $ALLOWED_MANIFEST_EXTENSION -join '|' function ConvertFrom-Manifest { <# @@ -98,9 +99,16 @@ function ConvertTo-Manifest { function manifest_path($app, $bucket) { $name = sanitary_path $app + $buc = Find-BucketDirectory -Bucket $bucket + $file = Get-ChildItem -LiteralPath $buc -Filter "$name.*" + $path = $null - # TODO: YAML - return Find-BucketDirectory $bucket | Join-Path -ChildPath "$name.json" + if ($file) { + if ($file.Count -gt 1) { $file = $file[0] } + $path = $file.FullName + } + + return $path } function parse_json { @@ -126,13 +134,21 @@ function url_manifest($url) { } if (!$str) { return $null } + # TODO: YAML return $str | ConvertFrom-Json } function manifest($app, $bucket, $url) { if ($url) { return url_manifest $url } - return parse_json (manifest_path $app $bucket) + $path = manifest_path $app $bucket + try { + $manifest = ConvertFrom-Manifest -Path $path + } catch { + $manifest = $null + } + + return $manifest } function save_installed_manifest($app, $bucket, $dir, $url) { @@ -148,18 +164,27 @@ function save_installed_manifest($app, $bucket, $dir, $url) { } function installed_manifest($app, $version, $global) { - # TODO: YML - $old = 'manifest.json' - $new = 'scoop-manifest.json' $d = versiondir $app $version $global - # Migration - if (Join-Path $d $old | Test-Path ) { + #region Migration from non-generic file name + $old = 'manifest.json' + $new = 'scoop-manifest.json' + if (Join-Path $d $old | Test-Path) { Write-UserMessage -Message "Migrating $old to $new" -Info Join-Path $d $old | Rename-Item -NewName $new } + $manifestPath = Join-Path $d $new + #endregion Migration from non-generic file name + + # Different extension types + if (!(Test-Path $manifestPath)) { + $installedManifests = Get-ChildItem -LiteralPath $d -Include 'scoop-manifest.*' + if ($installedManifests.Count -gt 0) { + $manifestPath = $installedManifests[0].FullName + } + } - return parse_json (Join-Path $d $new) + return ConvertFrom-Manifest -Path $manifestPath } function save_install_info($info, $dir) { diff --git a/libexec/scoop-list.ps1 b/libexec/scoop-list.ps1 index 498e1131ff..0e4c95a3c0 100644 --- a/libexec/scoop-list.ps1 +++ b/libexec/scoop-list.ps1 @@ -41,9 +41,10 @@ if ($apps) { $sortSplat.Property = { $_.gci.CreationTime } } elseif ($orderUpdated) { $sortSplat.Property = { + # TODO: Keep only scoop-install $old = Join-Path $_.gci.Fullname '*\install.json' | Get-ChildItem $new = Join-Path $_.gci.Fullname '*\scoop-install.json' | Get-ChildItem - @($old, $new) | Get-ChildItem | Sort-Object -Property LastWriteTimeUtc | Select-Object -ExpandProperty LastWriteTimeUtc -Last 1 + @($old, $new) | Get-ChildItem | Sort-Object -Property 'LastWriteTimeUtc' | Select-Object -ExpandProperty 'LastWriteTimeUtc' -Last 1 } }