Skip to content

Commit f606285

Browse files
Chawye Hsur15ch13
Chawye Hsu
authored andcommitted
feat(manifest): xpath support in checkver and autoupdate (#3458)
1 parent 16bdee0 commit f606285

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

bin/checkver.ps1

+21-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ $Queue | ForEach-Object {
112112
}
113113
$regex = ''
114114
$jsonpath = ''
115+
$xpath = ''
115116
$replace = ''
116117

117118
if ($json.checkver -eq 'github') {
@@ -140,12 +141,15 @@ $Queue | ForEach-Object {
140141
if ($json.checkver.jsonpath) {
141142
$jsonpath = $json.checkver.jsonpath
142143
}
144+
if ($json.checkver.xpath) {
145+
$xpath = $json.checkver.xpath
146+
}
143147

144148
if ($json.checkver.replace -and $json.checkver.replace.GetType() -eq [System.String]) {
145149
$replace = $json.checkver.replace
146150
}
147151

148-
if (!$jsonpath -and !$regex) {
152+
if (!$jsonpath -and !$regex -and !$xpath) {
149153
$regex = $json.checkver
150154
}
151155

@@ -159,6 +163,7 @@ $Queue | ForEach-Object {
159163
regex = $regex;
160164
json = $json;
161165
jsonpath = $jsonpath;
166+
xpath = $xpath;
162167
reverse = $reverse;
163168
replace = $replace;
164169
}
@@ -185,6 +190,7 @@ while ($in_progress -gt 0) {
185190
$url = $state.url
186191
$regexp = $state.regex
187192
$jsonpath = $state.jsonpath
193+
$xpath = $state.xpath
188194
$reverse = $state.reverse
189195
$replace = $state.replace
190196
$expected_ver = $json.version
@@ -214,11 +220,25 @@ while ($in_progress -gt 0) {
214220
}
215221
}
216222

223+
if ($xpath) {
224+
# Getting version from XML, using XPath
225+
$ver = ([xml]$page).SelectSingleNode($xpath).'#text'
226+
if (!$ver) {
227+
next "couldn't find '$xpath' in $url"
228+
continue
229+
}
230+
}
231+
217232
if ($jsonpath -and $regexp) {
218233
$page = $ver
219234
$ver = ''
220235
}
221236

237+
if ($xpath -and $regexp) {
238+
$page = $ver
239+
$ver = ''
240+
}
241+
222242
if ($regexp) {
223243
$regex = New-Object System.Text.RegularExpressions.Regex($regexp)
224244
if ($reverse) {

lib/autoupdate.ps1

+32
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ function find_hash_in_json([String] $url, [Hashtable] $substitutions, [String] $
109109
return format_hash $hash
110110
}
111111

112+
function find_hash_in_xml([String] $url, [Hashtable] $substitutions, [String] $xpath) {
113+
$xml = $null
114+
115+
try {
116+
$wc = New-Object Net.Webclient
117+
$wc.Headers.Add('Referer', (strip_filename $url))
118+
$wc.Headers.Add('User-Agent', (Get-UserAgent))
119+
$xml = [xml]$wc.downloadstring($url)
120+
} catch [system.net.webexception] {
121+
write-host -f darkred $_
122+
write-host -f darkred "URL $url is not valid"
123+
return
124+
}
125+
126+
# Replace placeholders
127+
if ($substitutions) {
128+
$xpath = substitute $xpath $substitutions
129+
}
130+
131+
$hash = $xml.SelectSingleNode($xpath).'#text'
132+
return format_hash $hash
133+
}
134+
112135
function find_hash_in_headers([String] $url) {
113136
$hash = $null
114137

@@ -178,6 +201,12 @@ function get_hash_for_app([String] $app, $config, [String] $version, [String] $u
178201
$regex = $config.regex
179202
}
180203

204+
$xpath = ''
205+
if ($config.xpath) {
206+
$xpath = $config.xpath
207+
$hashmode = 'xpath'
208+
}
209+
181210
if (!$hashfile_url -and $url -match "^(?:.*fosshub.com\/).*(?:\/|\?dwl=)(?<filename>.*)$") {
182211
$hashmode = 'fosshub'
183212
}
@@ -193,6 +222,9 @@ function get_hash_for_app([String] $app, $config, [String] $version, [String] $u
193222
'json' {
194223
$hash = find_hash_in_json $hashfile_url $substitutions $jsonpath
195224
}
225+
'xpath' {
226+
$hash = find_hash_in_xml $hashfile_url $substitutions $xpath
227+
}
196228
'rdf' {
197229
$hash = find_hash_in_rdf $hashfile_url $basename
198230
}

schema.json

+7
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@
4343
"pattern": "^\\$[.[].*$",
4444
"type": "string"
4545
},
46+
"xpath": {
47+
"type": "string"
48+
},
4649
"mode": {
4750
"enum": [
4851
"download",
4952
"extract",
5053
"json",
54+
"xpath",
5155
"rdf",
5256
"metalink",
5357
"fosshub",
@@ -237,6 +241,9 @@
237241
"pattern": "^\\$[.[].*$",
238242
"type": "string"
239243
},
244+
"xpath": {
245+
"type": "string"
246+
},
240247
"reverse": {
241248
"description": "Reverse the order of regex matches",
242249
"type": "boolean"

0 commit comments

Comments
 (0)