mirror of
https://github.com/ScoopInstaller/Scoop.git
synced 2026-08-25 15:46:47 +00:00
feat(checkver): improve JSONPath extraction support (#4522)
This commit is contained in:
+7
-1
@@ -213,7 +213,13 @@ while ($in_progress -gt 0) {
|
||||
}
|
||||
|
||||
if ($jsonpath) {
|
||||
$ver = json_path $page $jsonpath
|
||||
# Return only a single value if regex is absent
|
||||
$noregex = [String]::IsNullOrEmpty($regex)
|
||||
# If reverse is ON and regex is ON,
|
||||
# Then reverse would have no effect because regex handles reverse
|
||||
# on its own
|
||||
# So in this case we have to disable reverse
|
||||
$ver = json_path $page $jsonpath $null ($reverse -and $noregex) $noregex
|
||||
if (!$ver) {
|
||||
$ver = json_path_legacy $page $jsonpath
|
||||
}
|
||||
|
||||
+20
-18
@@ -92,32 +92,34 @@ function ConvertToPrettyJson {
|
||||
}
|
||||
}
|
||||
|
||||
function json_path([String] $json, [String] $jsonpath, [Hashtable] $substitutions) {
|
||||
function json_path([String] $json, [String] $jsonpath, [Hashtable] $substitutions, [Boolean] $reverse, [Boolean] $single) {
|
||||
Add-Type -Path "$psscriptroot\..\supporting\validator\bin\Newtonsoft.Json.dll"
|
||||
if ($null -ne $substitutions) {
|
||||
$jsonpath = substitute $jsonpath $substitutions ($jsonpath -like "*=~*")
|
||||
}
|
||||
try {
|
||||
$obj = [Newtonsoft.Json.Linq.JObject]::Parse($json)
|
||||
$obj = [Newtonsoft.Json.Linq.JValue]::Parse($json)
|
||||
} catch [Newtonsoft.Json.JsonReaderException] {
|
||||
try {
|
||||
$obj = [Newtonsoft.Json.Linq.JArray]::Parse($json)
|
||||
} catch [Newtonsoft.Json.JsonReaderException] {
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
try {
|
||||
$result = $obj.SelectToken($jsonpath, $true)
|
||||
} catch [Newtonsoft.Json.JsonException] {
|
||||
return $null
|
||||
}
|
||||
return $result.ToString()
|
||||
} catch [System.Management.Automation.MethodInvocationException] {
|
||||
write-host -f DarkRed $_
|
||||
return $null
|
||||
}
|
||||
try {
|
||||
$result = $obj.SelectTokens($jsonpath, $true)
|
||||
if ($reverse) {
|
||||
# Return versions in reverse order
|
||||
$result = [System.Linq.Enumerable]::Reverse($result)
|
||||
}
|
||||
if ($single) {
|
||||
# Extract First value
|
||||
$result = [System.Linq.Enumerable]::First($result)
|
||||
# Convert first value to string
|
||||
$result = $result.ToString()
|
||||
} else {
|
||||
$result = "$([String]::Join('\n', $result))"
|
||||
}
|
||||
return $result
|
||||
} catch [Exception] {
|
||||
Write-Host $_ -ForegroundColor DarkRed
|
||||
}
|
||||
|
||||
return $null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user