Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple URL support for autoupdate #2803

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 63 additions & 16 deletions lib/autoupdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,11 @@ function get_hash_for_app([String] $app, $config, [String] $version, [String] $u
return $hash
}

function update_manifest_with_new_version($json, [String] $version, [String] $url, [String] $hash, $architecture = $null) {
function update_manifest_with_new_version($json, [String] $version, $url, $hash, $architecture = $null) {
$json.version = $version

if ($null -eq $architecture) {
if ($json.url -is [System.Array]) {
if ($json.url -is [System.Array] -and $url -isnot [System.Array]) {
$json.url[0] = $url
$json.hash[0] = $hash
} else {
Expand All @@ -263,7 +263,7 @@ function update_manifest_with_new_version($json, [String] $version, [String] $ur
}
} else {
# If there are multiple urls we replace the first one
if ($json.architecture.$architecture.url -is [System.Array]) {
if ($json.architecture.$architecture.url -is [System.Array] -and $url -isnot [System.Array]) {
$json.architecture.$architecture.url[0] = $url
$json.architecture.$architecture.hash[0] = $hash
} else {
Expand Down Expand Up @@ -318,34 +318,81 @@ function get_version_substitutions([String] $version, [Hashtable] $customMatches
return $versionVariables
}

function autoupdate([String] $app, $dir, $json, [String] $version, [Hashtable] $matches) {
Write-Host -f DarkCyan "Autoupdating $app"
function lets_do_autoupdates([String] $app, $json, [String] $version, $substitutions) {
$urls = @()
$hashes = @()
$has_changes = $false
$has_errors = $false
[Bool]$valid = $true
$substitutions = get_version_substitutions $version $matches

if ($json.url) {
for ($i=0; $i -lt $json.autoupdate.url.Length; $i++) {
# create new url
$url = substitute $json.autoupdate.url $substitutions
$valid = $true
$url = substitute $json.autoupdate.url[$i] $substitutions
$urls += $url

if($valid) {
# create hash
$hash = get_hash_for_app $app $json.autoupdate.hash $version $url $substitutions
if ($json.autoupdate.hash.length -gt $i) {
$h = $json.autoupdate.hash[$i]
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else {
} elseif ($json.autoupdate.hash.length -ne 0) {
$h = $json.autoupdate.hash[-1]
} else {

If hash's length is less than url, the last hash expression is reused for remaining urls.

So we could use one hash expression for all these urls if hash extraction url and regex are the same, e.g., $sha256.*?$basename, different hashes for different $basenames.

$h = $null
}
$hash = get_hash_for_app $app $h $version $url $substitutions
$hashes += $hash
if ($null -eq $hash) {
$valid = $false
Write-Host -f DarkRed "Could not find hash!"
}
}
}

# write changes to the json object
if ($valid) {
$has_changes = $true
update_manifest_with_new_version $json $version $urls $hashes
} else {
$has_errors = $true
throw "Could not update $app"
}

return $has_changes, $has_errors
}


function autoupdate([String] $app, $dir, $json, [String] $version, [Hashtable] $matches) {
Write-Host -f DarkCyan "Autoupdating $app"
$has_changes = $false
$has_errors = $false
[Bool]$valid = $true
$substitutions = get_version_substitutions $version $matches

# write changes to the json object
if ($valid) {
$has_changes = $true
update_manifest_with_new_version $json $version $url $hash
if ($json.url) {
if ($json.autoupdate.url -is [System.Array]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if...else... should be applied to $json.architecture's ForEach-Object script too.

$has_changes, $has_errors = lets_do_autoupdates $app $json $version $substitutions
$has_changes = $return[0]
$has_errors = $return[1]
} else {
$has_errors = $true
throw "Could not update $app"
# create new url
$url = substitute $json.autoupdate.url $substitutions
$valid = $true

if($valid) {
# create hash
$hash = get_hash_for_app $app $json.autoupdate.hash $version $url $substitutions
if ($null -eq $hash) {
$valid = $false
Write-Host -f DarkRed "Could not find hash!"
}
}

# write changes to the json object
if ($valid) {
$has_changes = $true
update_manifest_with_new_version $json $version $url $hash
} else {
$has_errors = $true
throw "Could not update $app"
}
}
} else {
$json.architecture | Get-Member -MemberType NoteProperty | ForEach-Object {
Expand Down
47 changes: 38 additions & 9 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@
},
"type": "object"
},
"hashExtractionOrArrayOfHashExtractions": {
"anyOf": [
{
"$ref": "#/definitions/hashExtraction"
},
{
"items": {
"$ref": "#/definitions/hashExtraction"
},
"minItems": 1,
"type": "array",
"uniqueItems": false
}
]
},
"architecture": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -158,11 +173,10 @@
"type": "string"
},
"url": {
"format": "uri",
"type": "string"
"$ref": "#/definitions/autoupdateUriOrArrayOfAutoupdateUris"
},
"hash": {
"$ref": "#/definitions/hashExtraction"
"$ref": "#/definitions/hashExtractionOrArrayOfHashExtractions"
}
},
"type": "object"
Expand All @@ -174,11 +188,10 @@
"type": "string"
},
"url": {
"format": "uri",
"type": "string"
"$ref": "#/definitions/autoupdateUriOrArrayOfAutoupdateUris"
},
"hash": {
"$ref": "#/definitions/hashExtraction"
"$ref": "#/definitions/hashExtractionOrArrayOfHashExtractions"
}
},
"type": "object"
Expand All @@ -190,14 +203,13 @@
"type": "string"
},
"hash": {
"$ref": "#/definitions/hashExtraction"
"$ref": "#/definitions/hashExtractionOrArrayOfHashExtractions"
},
"note": {
"$ref": "#/definitions/stringOrArrayOfStrings"
},
"url": {
"format": "uri",
"type": "string"
"$ref": "#/definitions/autoupdateUriOrArrayOfAutoupdateUris"
}
},
"type": "object"
Expand Down Expand Up @@ -352,6 +364,23 @@
}
]
},
"autoupdateUriOrArrayOfAutoupdateUris": {
"anyOf": [
{
"format": "uri",
"type": "string"
},
{
"items": {
"format": "uri",
"type": "string"
},
"minItems": 1,
"type": "array",
"uniqueItems": true
}
]
},
"licenseIdentifiers": {
"type": "string",
"description": "License identifier based on SPDX License List https://spdx.org/licenses/",
Expand Down