forked from google/bazel-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_version
executable file
·117 lines (99 loc) · 3.18 KB
/
update_version
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
#!/bin/bash
#
# USAGE
#
# update_version group_id artifact_id version_number
# update_version VERSION_VARIABLE version_number
#
# Example: update_version com.google.guava guava 31.1-jre
# Example: update_version ERROR_PRONE_VERSION 2.3.2
set -eu
readonly WORKSPACE="$(dirname $0)/workspace_defs.bzl"
# Prints one line for each import that matches the Perl regular expressions in
# the arguments. Each line has, space separated:
# * the start line
# * the end line
# * the group ID
# * the artifact ID
# * the version or version variable name
# * the SHA
#
# The first argument is a Perl regular expression for the group ID, the second
# for the artifact ID, etc. There can be up to four arguments.
read_imports() {
perl -ne '
%import = (start => $.) if /maven_import\(/;
$import{$1} = $2 if /(group_id|artifact_id|version|sha256) = "?([^"]+)"?,$/;
if (/^ +\)$/) {
print "$import{start} $. ";
print join(" ", @import{"group_id", "artifact_id", "version", "sha256"});
print "\n";
}
' "${WORKSPACE}" | grep -P "^\d+ \d+ $*( |\$)"
}
# Prints the SHA 256 for the JAR for the given group ID, artifact ID, and
# version.
sha() {
local group=$1
local artifact=$2
local version=$3
local url='https://repo1.maven.org/maven2/'
url+="${group//.//}/${artifact}/${version}/${artifact}-${version}.jar"
set -o pipefail
curl -f --no-progress-meter "${url}" | sha256sum | cut -d' ' -f1
}
# Updates the maven_import in workspace_defs.bzl for the given group ID and
# artifact ID by updating the value for the parameter to the given value.
update_artifact() {
local group=$1
local artifact=$2
local parameter=$3
local value=$4
local start end group artifact version sha
echo "updating ${parameter} for ${group}:${artifact} to ${value}" >&2
read start end group artifact version sha \
< <(read_imports "\Q${group}\E" "\Q${artifact}\E")
perl -i -lpe '
if ('"${start}..${end}"') {
s(^(\s*\Q'"${parameter}"'\E) =.*,$)($1 = "'"${value}"'",);
}
' "${WORKSPACE}"
}
# Updates the definition in workspace_defs.bzl for the given variable to the
# given value.
update_variable() {
local variable=$1
local value=$2
echo "updating ${variable} to ${value}" >&2
perl -i -lpe 's(^(\s*\Q'"${variable}"'\E) = "[^"]+"$)($1 = "'"${value}"'")' \
"${WORKSPACE}"
}
update_artifact_version() {
local group=$1
local artifact=$2
local new_version=$3
update_artifact "${group}" "${artifact}" version "${new_version}"
update_artifact "${group}" "${artifact}" sha256 \
"$(sha "${group}" "${artifact}" "${new_version}")"
}
update_artifacts_for_variable() {
local variable=$1
local new_version=$2
local start end group artifact version sha
update_variable "${variable}" "${new_version}"
while read start end group artifact version sha; do
update_artifact "${group}" "${artifact}" \
sha256 "$(sha "${group}" "${artifact}" "${new_version}")"
done < <(read_imports "\S+" "\S+" "\Q${variable}\E")
}
case "$#" in
3) update_artifact_version "$@"
;;
2) update_artifacts_for_variable "$@"
;;
*) echo -e >&2 "Usage:\n" \
" $0 group_id artifact_id new_version\n" \
" $0 variable new_version"
exit 2
;;
esac