-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
increment-cargo-version.sh
executable file
·156 lines (135 loc) · 3.12 KB
/
increment-cargo-version.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env bash
set -e
usage() {
cat <<EOF
usage: $0 [major|minor|patch|-preXYZ]
Increments the Cargo.toml version.
Default:
* Removes the prerelease tag if present, otherwise the minor version is incremented.
EOF
exit 0
}
here="$(dirname "$0")"
cd "$here"/..
source ci/semver_bash/semver.sh
source scripts/read-cargo-variable.sh
ignores=(
.cache
.cargo
target
web3.js/examples
web3.js/test
node_modules
)
not_paths=()
for ignore in "${ignores[@]}"; do
not_paths+=(-not -path "*/$ignore/*")
done
# shellcheck disable=2207
Cargo_tomls=($(find . -mindepth 2 -name Cargo.toml "${not_paths[@]}"))
# shellcheck disable=2207
markdownFiles=($(find . -name "*.md" "${not_paths[@]}"))
# Collect the name of all the internal crates
crates=()
for Cargo_toml in "${Cargo_tomls[@]}"; do
crates+=("$(readCargoVariable name "$Cargo_toml")")
done
# Read the current version
MAJOR=0
MINOR=0
PATCH=0
SPECIAL=""
semverParseInto "$(readCargoVariable version "${Cargo_tomls[0]}")" MAJOR MINOR PATCH SPECIAL
[[ -n $MAJOR ]] || usage
currentVersion="$MAJOR\.$MINOR\.$PATCH$SPECIAL"
bump=$1
if [[ -z $bump ]]; then
if [[ -n $SPECIAL ]]; then
bump=dropspecial # Remove prerelease tag
else
bump=minor
fi
fi
SPECIAL=""
# Figure out what to increment
case $bump in
patch)
PATCH=$((PATCH + 1))
;;
major)
MAJOR=$((MAJOR+ 1))
MINOR=0
PATCH=0
;;
minor)
MINOR=$((MINOR+ 1))
PATCH=0
;;
dropspecial)
;;
check)
badTomls=()
for Cargo_toml in "${Cargo_tomls[@]}"; do
if ! grep "^version *= *\"$currentVersion\"$" "$Cargo_toml" &>/dev/null; then
badTomls+=("$Cargo_toml")
fi
done
if [[ ${#badTomls[@]} -ne 0 ]]; then
echo "Error: Incorrect crate version specified in: ${badTomls[*]}"
exit 1
fi
exit 0
;;
-*)
if [[ $1 =~ ^-[A-Za-z0-9]*$ ]]; then
SPECIAL="$1"
else
echo "Error: Unsupported characters found in $1"
exit 1
fi
;;
*)
echo "Error: unknown argument: $1"
usage
;;
esac
# Version bumps should occur in their own commit. Disallow bumping version
# in dirty working trees. Gate after arg parsing to prevent breaking the
# `check` subcommand.
(
set +e
if ! git diff --exit-code; then
echo -e "\nError: Working tree is dirty. Commit or discard changes before bumping version." 1>&2
exit 1
fi
)
newVersion="$MAJOR.$MINOR.$PATCH$SPECIAL"
# Update all the Cargo.toml files
for Cargo_toml in "${Cargo_tomls[@]}"; do
# Set new crate version
(
set -x
sed -i "$Cargo_toml" -e "0,/^version =/{s/^version = \"[^\"]*\"$/version = \"$newVersion\"/}"
)
# Fix up the version references to other internal crates
for crate in "${crates[@]}"; do
(
set -x
sed -i "$Cargo_toml" -e "
s/^$crate = { *path *= *\"\([^\"]*\)\" *, *version *= *\"[^\"]*\"\(.*\)} *\$/$crate = \{ path = \"\1\", version = \"=$newVersion\"\2\}/
"
)
done
done
# Update all the documentation references
for file in "${markdownFiles[@]}"; do
# Set new crate version
(
set -x
sed -i "$file" -e "s/$currentVersion/$newVersion/g"
)
done
# Update cargo lock files
scripts/cargo-for-all-lock-files.sh tree >/dev/null
echo "$currentVersion -> $newVersion"
exit 0