-
Notifications
You must be signed in to change notification settings - Fork 0
/
pom.sh
executable file
·137 lines (116 loc) · 3.18 KB
/
pom.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash -e
# depends on: xmlstarlet
MYSELF="$(readlink -f "$0")"
MYDIR="${MYSELF%/*}"
ME=$(basename $MYSELF)
source $MYDIR/env
[[ -f $LOCAL_ENV ]] && source $LOCAL_ENV
source $MYDIR/log.sh
source $MYDIR/require.sh
NAMESPACE='http://maven.apache.org/POM/4.0.0'
function do_select() {
local file="$1"
local query="$2"
require -f file "arg 1 should be the pom file location"
require query "arg 2 should be the xpath query, maven pom namespace prefix is 'x:'"
xmlstarlet sel -N x=$NAMESPACE -t -v "$query" $file
}
function do_edit() {
local file="${1}"
local query="$2"
local value="$3"
currval=$(do_select "$file" "$query")
require value "arg 3 should be the replacement value"
debug "editing $file $query from $currval to $value..."
xmlstarlet ed -L -N x=$NAMESPACE \
-u "$query" -v "$value" \
$file
debug "value changed."
}
# selects project version, or parent version, if undefined.
function select_version() {
local v=$(do_select "$1" 'x:project/x:version' || true)
if [[ ! -n "$v" ]]; then
debug "falling back to parent/version..."
v=$(do_select "$1" 'x:project/x:parent/x:version')
fi
echo "$v"
}
function set_version() {
local file="$1"
local new_v="$2"
local v=$(do_select "$file" 'x:project/x:version' || true)
if [[ ! -n "$v" ]]; then
debug "falling back to parent/version..."
do_edit "$file" "x:project/x:parent/x:version" "$new_v"
else
do_edit "$file" "x:project/x:version" "$new_v"
fi
}
##
# increments build version.
# if SNAPSHOT, just closes it without incrementing.
function bump_build() {
local file="$1"
local v=$(select_version "$file")
if [[ "$v" == *SNAPSHOT* ]]; then
debug "only closing snapshot..."
new_v=${v/-SNAPSHOT/}
else
new_v=$(echo $v | awk -F. '{$NF+=1; OFS="."; print $0}' | sed 's/ /./g')
debug "new version calculated as: $new_v"
fi
do_edit "$file" "x:project/x:version" "$new_v"
echo $new_v
}
function reopen_version() {
local file="$1"
local v=$(select_version "$file")
if [[ "$v" == *SNAPSHOT* ]]; then
debug "project is already in snapshot"
else
new_v="$(bump_build "$file")-SNAPSHOT"
fi
do_edit "$file" "x:project/x:version" "$new_v"
echo "$new_v"
}
while test $# -gt 0
do
case "$1" in
--verbose|-v)
debugging on
;;
--select|-s)
shift; f="$1"
shift; q="$1"
do_select "$f" "$q"
;;
--edit|-e)
shift; file="$1"
shift; query="$1"
shift; value="$1"
do_edit "$file" "$query" "$value"
;;
--project-version|--version)
shift; file="$1"
select_version "$file"
;;
--set)
shift; file="$1"
shift; version="$1"
set_version "$file" "$version"
;;
--bump-build|-b)
shift; f="$1"
bump_build "$f"
;;
--snap|--open|-o)
shift; f="$1"
reopen_version "$f"
;;
-*)
echo "bad option '$1'"
;;
esac
shift
done