|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -# Get or set the semver in various files. |
4 | | - |
5 | | -# If called with no args, return the current version. |
6 | | -# If called with one arg, set the version to the new value. |
| 3 | +# Set the semver in various files. |
7 | 4 |
|
8 | 5 | # Always performs sanity checking: |
9 | 6 | # - There must be at least one version file. |
10 | | -# - All version files must agree. (Ignoring the contents but not existence of pre-release version.) |
11 | 7 | # - The version must be a valid semver. |
12 | 8 | # - The version must not be 0.0.0. |
| 9 | +# - After running, altered files must already be under Git control, and they must be only the version files we know how to handle, |
| 10 | +# and modifications must be 0 or 1 line changes. |
13 | 11 |
|
14 | 12 | # If setting the version to $a.$b.$c-$pre, substitute "SNAPSHOT" for $pre in any Java-related files. |
15 | 13 |
|
16 | 14 | set -e |
17 | 15 |
|
18 | 16 | # Parse args |
19 | | -case "$#" in |
20 | | -"0") |
21 | | - NEWVERS="" |
22 | | - ;; |
23 | | -"1") |
24 | | - NEWVERS="$1" |
25 | | - ;; |
26 | | -"*") |
27 | | - echo "Usage: $0 [version]" 1>&2 |
28 | | - exit 1 |
29 | | - ;; |
30 | | -esac |
31 | | - |
32 | | -# Find the version files in this directory or its descendants, but don't recurse too deep. |
33 | | -VERSFILES=$(find . -maxdepth 3 ! -path ./.git/\* | grep -v /node_modules/ | grep -E '.*/(version|Cargo.toml|package.json|pom.xml|version.sbt)$') |
34 | | - |
35 | | -# Do we have at least one? |
36 | | -if [ -z "${VERSFILES}" ] ; then |
37 | | - echo "No version files found; aborting" 1>&2 |
| 17 | +if [ $# -ne 1 ] ; then |
| 18 | + echo "Usage: $0 version" 1>&2 |
38 | 19 | exit 1 |
39 | 20 | fi |
40 | | - |
41 | | -# Read the versions. |
42 | | -CURRENTVERS="" |
43 | | -for FILE in ${VERSFILES} ; do |
44 | | - # Parse each version file according to its type. |
45 | | - case $(basename "${FILE}") in |
46 | | - version) |
47 | | - # It's a file to capture version info for generic things that don't have their own format. |
48 | | - VERS=$(cat "${FILE}") |
49 | | - ;; |
50 | | - Cargo.toml) |
51 | | - VERS=$(cargo metadata --manifest-path "${FILE}" --no-deps --offline --format-version 1 | jq -r '.packages[0].version') |
52 | | - ;; |
53 | | - package.json) |
54 | | - if [ "$(dirname "${FILE}")" = "." ] ; then |
55 | | - # This is the root package.json, so we want .version. |
56 | | - VERS=$(jq -r '.version' < "${FILE}") |
57 | | - else |
58 | | - # This isn't the root package.json, so we assume it depends on the package declared in the root package.json. We need to |
59 | | - # get the root package's name. |
60 | | - ROOTJSNAME=$(jq -r '.name' < package.json) |
61 | | - VERS=$(jq -r ".dependencies[\"${ROOTJSNAME}\"]" < "${FILE}") |
62 | | - # Strip off any leading "^". |
63 | | - VERS=${VERS/^/} |
64 | | - fi |
65 | | - ;; |
66 | | - ./pom.xml) |
67 | | - if [ "$(dirname "${FILE}")" = "." ] ; then |
68 | | - # This is the root pom.xml, so we want /m:project/m:version. |
69 | | - VERS=$(xmlstarlet sel -N m="http://maven.apache.org/POM/4.0.0" -t -v "/m:project/m:version" < "${FILE}") |
70 | | - else |
71 | | - # This isn't the root pom.xml, so we assume it depends on the package declared in the root pom.xml. We need to get the |
72 | | - # root pom's artifactId. |
73 | | - ROOTID=$(xmlstarlet sel -N m="http://maven.apache.org/POM/4.0.0" -t -v "/m:project/m:artifactId" < pom.xml) |
74 | | - # Select /m:project/m:dependencies/m:dependency/m:version where it has a sibling m:artifactId with the correct value. |
75 | | - XPATH="/m:project/m:dependencies/m:dependency[m:artifactId=\"${ROOTID}\"]/m:version" |
76 | | - VERS=$(xmlstarlet sel -N m="http://maven.apache.org/POM/4.0.0" -t -v "${XPATH}" < "${FILE}") |
77 | | - fi |
78 | | - ;; |
79 | | - version.sbt) |
80 | | - VERS=$(sed -e 's/^[^"]*"//' -e 's/"$//' < "${FILE}") |
81 | | - ;; |
82 | | - *) |
83 | | - echo "Can't parse '${FILE}' for version" 1>&2 |
84 | | - exit 1 |
85 | | - ;; |
86 | | - esac |
87 | | - |
88 | | - if [ -z "${VERS}" ] ; then |
89 | | - echo "Empty version from '${FILE}'" 1>&2 |
90 | | - exit 1 |
91 | | - fi |
92 | | - |
93 | | - # If this is the first parsed version file, then set current version. |
94 | | - if [ -z "${CURRENTVERS}" ] ; then |
95 | | - CURRENTVERS="${VERS}" |
96 | | - fi |
97 | | - |
98 | | - # Compare this file's version to other files' version. Ignore anything after the "-" in a pre-release version, but keep the "-" |
99 | | - # so a release version is unequal to a pre-release. |
100 | | - if ! [ "${CURRENTVERS/-*/-}" = "${VERS/-*/-}" ] ; then |
101 | | - echo "Version '${VERS}' in '${FILE}' doesn't match '${CURRENTVERS}' from others in '${VERSFILES}'" 1>&2 |
102 | | - exit 1 |
103 | | - fi |
104 | | -done |
| 21 | +NEWVERS="$1" |
105 | 22 |
|
106 | 23 | # Sanity check: Ignoring any pre-release info, version must not be 0.0.0. |
107 | | -if [ "${CURRENTVERS/-*/}" = "0.0.0" ] ; then |
108 | | - echo "Illegal zero version '${CURRENTVERS}'" 1>&2 |
| 24 | +if [ "${NEWVERS/-*/}" = "0.0.0" ] ; then |
| 25 | + echo "Illegal zero version '${NEWVERS}'" 1>&2 |
109 | 26 | exit 1 |
110 | 27 | fi |
111 | 28 | # Sanity check: Must be valid semver. |
112 | | -if ! [[ ${CURRENTVERS} =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$ ]] ; then |
113 | | - echo "Invalid version '${CURRENTVERS}'" 1>&2 |
| 29 | +if ! [[ ${NEWVERS} =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$ ]] ; then |
| 30 | + echo "Invalid version '${NEWVERS}'" 1>&2 |
114 | 31 | exit 1 |
115 | 32 | fi |
116 | 33 |
|
117 | | -# If we're just getting the current version, print it and exit successfully. |
118 | | -if [ -z "${NEWVERS}" ] ; then |
119 | | - echo "${CURRENTVERS}" |
120 | | - exit 0 |
121 | | -fi |
122 | | - |
123 | | -# If we reach this point, it means we're setting the version. |
| 34 | +# Find the version files in this directory or its descendants, but don't recurse too deep. |
| 35 | +# This line must be kept in sync with "bump-version.get.sh". |
| 36 | +VERSFILES=$(find . -maxdepth 3 ! -path ./.git/\* | grep -v /node_modules/ | grep -E '.*/(version|Cargo.toml|package.json|pom.xml|version.sbt)$') |
124 | 37 |
|
125 | 38 | # Edit the version files. |
126 | 39 | for FILE in ${VERSFILES} ; do |
|
0 commit comments