Skip to content

Commit 2cff749

Browse files
committed
melting-pot: generalize xpath logic
We will want to extract other information from POMs safely, so let's provide a more general-purpose xpath function using xmllint.
1 parent 553b33e commit 2cff749

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

melting-pot.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,24 @@ pom() {
278278
echo "$pomPath"
279279
}
280280

281+
# For the given XML file on disk ($1), gets the value of the
282+
# specified XPath expression of the form "//$2/$3/$4/...".
283+
xpath() {
284+
local xmlFile="$1"
285+
shift
286+
local xpath="/"
287+
while [ $# -gt 0 ]
288+
do
289+
# NB: Ignore namespace issues; see: http://stackoverflow.com/a/8266075
290+
xpath="$xpath/*[local-name()='$1']"
291+
shift
292+
done
293+
xmllint --xpath "$xpath" "$xmlFile" | sed -E 's/^[^>]*>(.*)<[^<]*$/\1/'
294+
}
295+
281296
# Gets the SCM URL for the given GAV.
282297
scmURL() {
283-
local scmXPath="//*[local-name()='project']/*[local-name()='scm']/*[local-name()='connection']"
284-
xmllint --xpath "$scmXPath" "$(pom "$1")" | sed -E 's/.*>scm:git:(.*)<.*/\1/'
298+
xpath "$(pom "$1")" project scm connection | sed -E 's/.*>scm:git:(.*)<.*/\1/'
285299
}
286300

287301
# Gets the SCM tag for the given GAV.

0 commit comments

Comments
 (0)