Skip to content

Commit

Permalink
refactor: Support YAML typed manifests in some executables (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash258 authored Jan 16, 2021
1 parent 075d3be commit 1f3901e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/Search.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' = @()
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/buckets.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -860,6 +861,8 @@ function applist($apps, $global, $bucket = $null) {
}

function parse_app([string] $app) {
# TODO: YAML
# if ($app -match "(?:(?<bucket>[a-zA-Z0-9-]+)\/)?(?<app>.*\.$ALLOWED_MANIFESTS_EXTENSIONS_REGEX$|[a-zA-Z0-9-_.]+)(?:@(?<version>.*))?") {
if ($app -match '(?:(?<bucket>[a-zA-Z0-9-]+)\/)?(?<app>.*.json$|[a-zA-Z0-9-_.]+)(?:@(?<version>.*))?') {
return $matches['app'], $matches['bucket'], $matches['version']
}
Expand Down
3 changes: 2 additions & 1 deletion lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
43 changes: 34 additions & 9 deletions lib/manifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
<#
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion libexec/scoop-list.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit 1f3901e

Please sign in to comment.