From 16bdee060b131e4eaeb9b095cf351564ee557ffd Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Tue, 14 May 2019 03:01:45 +0800 Subject: [PATCH 1/3] fix(update): support changing scoop tracking repository (#3459) Allows tracking of custom scoop fork: ``` $ scoop config SCOOP_REPO "https://github.com//scoop-fork" $ scoop config SCOOP_BRANCH "patch-17" $ scoop update ``` --- libexec/scoop-update.ps1 | 43 +++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/libexec/scoop-update.ps1 b/libexec/scoop-update.ps1 index 10455657d..4a5d9d739 100644 --- a/libexec/scoop-update.ps1 +++ b/libexec/scoop-update.ps1 @@ -37,17 +37,17 @@ $quiet = $opt.q -or $opt.quiet $independent = $opt.i -or $opt.independent # load config -$repo = $(get_config SCOOP_REPO) -if (!$repo) { - $repo = "https://github.com/lukesampson/scoop" - set_config SCOOP_REPO "$repo" | Out-Null +$configRepo = get_config SCOOP_REPO +if (!$configRepo) { + $configRepo = "https://github.com/lukesampson/scoop" + set_config SCOOP_REPO $configRepo | Out-Null } # Find current update channel from config -$branch = $(get_config SCOOP_BRANCH) -if (!$branch) { - $branch = "master" - set_config SCOOP_BRANCH "$branch" | Out-Null +$configBranch = get_config SCOOP_BRANCH +if (!$configBranch) { + $configBranch = "master" + set_config SCOOP_BRANCH $configBranch | Out-Null } if(($PSVersionTable.PSVersion.Major) -lt 5) { @@ -78,7 +78,7 @@ function update_scoop() { $newdir = fullpath $(versiondir 'scoop' 'new') # get git scoop - git_clone -q $repo --branch $branch --single-branch "`"$newdir`"" + git_clone -q $configRepo --branch $configBranch --single-branch "`"$newdir`"" # check if scoop was successful downloaded if (!(test-path "$newdir")) { @@ -91,16 +91,27 @@ function update_scoop() { } else { Push-Location $currentdir - # Change branch if user configured other branch - if (!((git_branch) -match "\*\s+$branch")) { - # reset git fetch refs (GH-3368) + $currentRepo = git_config remote.origin.url + $currentBranch = git_branch + + $isRepoChanged = !($currentRepo -match $configRepo) + $isBranchChanged = !($currentBranch -match "\*\s+$configBranch") + + # Change remote url if the repo is changed + if ($isRepoChanged) { + git_config remote.origin.url "$configRepo" + } + + # Fetch and reset local repo if the repo or the branch is changed + if ($isRepoChanged -or $isBranchChanged) { + # Reset git fetch refs, so that it can fetch all branches (GH-3368) git_config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" - # fetch remote branches - git_fetch --force origin -q + # fetch remote branch + git_fetch --force origin "refs/heads/`"$configBranch`":refs/remotes/origin/$configBranch" -q # checkout and track the branch - git_checkout -B $branch -t origin/$branch -q + git_checkout -B $configBranch -t origin/$configBranch -q # reset branch HEAD - git_reset --hard origin/$branch -q + git_reset --hard origin/$configBranch -q } else { git_pull -q } From f6062855bad8d4829ebe02cd4664fbebe0586f90 Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Tue, 14 May 2019 03:06:26 +0800 Subject: [PATCH 2/3] feat(manifest): xpath support in checkver and autoupdate (#3458) --- bin/checkver.ps1 | 22 +++++++++++++++++++++- lib/autoupdate.ps1 | 32 ++++++++++++++++++++++++++++++++ schema.json | 7 +++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/bin/checkver.ps1 b/bin/checkver.ps1 index 831195c0b..47f4bd349 100644 --- a/bin/checkver.ps1 +++ b/bin/checkver.ps1 @@ -112,6 +112,7 @@ $Queue | ForEach-Object { } $regex = '' $jsonpath = '' + $xpath = '' $replace = '' if ($json.checkver -eq 'github') { @@ -140,12 +141,15 @@ $Queue | ForEach-Object { if ($json.checkver.jsonpath) { $jsonpath = $json.checkver.jsonpath } + if ($json.checkver.xpath) { + $xpath = $json.checkver.xpath + } if ($json.checkver.replace -and $json.checkver.replace.GetType() -eq [System.String]) { $replace = $json.checkver.replace } - if (!$jsonpath -and !$regex) { + if (!$jsonpath -and !$regex -and !$xpath) { $regex = $json.checkver } @@ -159,6 +163,7 @@ $Queue | ForEach-Object { regex = $regex; json = $json; jsonpath = $jsonpath; + xpath = $xpath; reverse = $reverse; replace = $replace; } @@ -185,6 +190,7 @@ while ($in_progress -gt 0) { $url = $state.url $regexp = $state.regex $jsonpath = $state.jsonpath + $xpath = $state.xpath $reverse = $state.reverse $replace = $state.replace $expected_ver = $json.version @@ -214,11 +220,25 @@ while ($in_progress -gt 0) { } } + if ($xpath) { + # Getting version from XML, using XPath + $ver = ([xml]$page).SelectSingleNode($xpath).'#text' + if (!$ver) { + next "couldn't find '$xpath' in $url" + continue + } + } + if ($jsonpath -and $regexp) { $page = $ver $ver = '' } + if ($xpath -and $regexp) { + $page = $ver + $ver = '' + } + if ($regexp) { $regex = New-Object System.Text.RegularExpressions.Regex($regexp) if ($reverse) { diff --git a/lib/autoupdate.ps1 b/lib/autoupdate.ps1 index 64842d549..39aeaa722 100644 --- a/lib/autoupdate.ps1 +++ b/lib/autoupdate.ps1 @@ -109,6 +109,29 @@ function find_hash_in_json([String] $url, [Hashtable] $substitutions, [String] $ return format_hash $hash } +function find_hash_in_xml([String] $url, [Hashtable] $substitutions, [String] $xpath) { + $xml = $null + + try { + $wc = New-Object Net.Webclient + $wc.Headers.Add('Referer', (strip_filename $url)) + $wc.Headers.Add('User-Agent', (Get-UserAgent)) + $xml = [xml]$wc.downloadstring($url) + } catch [system.net.webexception] { + write-host -f darkred $_ + write-host -f darkred "URL $url is not valid" + return + } + + # Replace placeholders + if ($substitutions) { + $xpath = substitute $xpath $substitutions + } + + $hash = $xml.SelectSingleNode($xpath).'#text' + return format_hash $hash +} + function find_hash_in_headers([String] $url) { $hash = $null @@ -178,6 +201,12 @@ function get_hash_for_app([String] $app, $config, [String] $version, [String] $u $regex = $config.regex } + $xpath = '' + if ($config.xpath) { + $xpath = $config.xpath + $hashmode = 'xpath' + } + if (!$hashfile_url -and $url -match "^(?:.*fosshub.com\/).*(?:\/|\?dwl=)(?.*)$") { $hashmode = 'fosshub' } @@ -193,6 +222,9 @@ function get_hash_for_app([String] $app, $config, [String] $version, [String] $u 'json' { $hash = find_hash_in_json $hashfile_url $substitutions $jsonpath } + 'xpath' { + $hash = find_hash_in_xml $hashfile_url $substitutions $xpath + } 'rdf' { $hash = find_hash_in_rdf $hashfile_url $basename } diff --git a/schema.json b/schema.json index 11a9a4422..c245b5f8a 100644 --- a/schema.json +++ b/schema.json @@ -43,11 +43,15 @@ "pattern": "^\\$[.[].*$", "type": "string" }, + "xpath": { + "type": "string" + }, "mode": { "enum": [ "download", "extract", "json", + "xpath", "rdf", "metalink", "fosshub", @@ -237,6 +241,9 @@ "pattern": "^\\$[.[].*$", "type": "string" }, + "xpath": { + "type": "string" + }, "reverse": { "description": "Reverse the order of regex matches", "type": "boolean" From 765706ce38f8a68531b5e36acfad13bd21c2cfb6 Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Wed, 15 May 2019 22:54:59 +0800 Subject: [PATCH 3/3] fix(autoupdate): Handle xml namespace in xpath mode (#3465) --- bin/checkver.ps1 | 10 +++++++++- lib/autoupdate.ps1 | 11 ++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/bin/checkver.ps1 b/bin/checkver.ps1 index 47f4bd349..70d356079 100644 --- a/bin/checkver.ps1 +++ b/bin/checkver.ps1 @@ -221,8 +221,16 @@ while ($in_progress -gt 0) { } if ($xpath) { + $xml = [xml]$page + # Find all `significant namespace declarations` from the XML file + $nsList = $xml.SelectNodes("//namespace::*[not(. = ../../namespace::*)]") + # Then add them into the NamespaceManager + $nsmgr = New-Object System.Xml.XmlNamespaceManager($xml.NameTable) + $nsList | ForEach-Object { + $nsmgr.AddNamespace($_.LocalName, $_.Value) + } # Getting version from XML, using XPath - $ver = ([xml]$page).SelectSingleNode($xpath).'#text' + $ver = $xml.SelectSingleNode($xpath, $nsmgr).'#text' if (!$ver) { next "couldn't find '$xpath' in $url" continue diff --git a/lib/autoupdate.ps1 b/lib/autoupdate.ps1 index 39aeaa722..57910d084 100644 --- a/lib/autoupdate.ps1 +++ b/lib/autoupdate.ps1 @@ -128,7 +128,16 @@ function find_hash_in_xml([String] $url, [Hashtable] $substitutions, [String] $x $xpath = substitute $xpath $substitutions } - $hash = $xml.SelectSingleNode($xpath).'#text' + # Find all `significant namespace declarations` from the XML file + $nsList = $xml.SelectNodes("//namespace::*[not(. = ../../namespace::*)]") + # Then add them into the NamespaceManager + $nsmgr = New-Object System.Xml.XmlNamespaceManager($xml.NameTable) + $nsList | ForEach-Object { + $nsmgr.AddNamespace($_.LocalName, $_.Value) + } + + # Getting hash from XML, using XPath + $hash = $xml.SelectSingleNode($xpath, $nsmgr).'#text' return format_hash $hash }