Skip to content

Commit 100af79

Browse files
authored
GH-700: Improve Version Bump Script (#281)
* GH-700: use command cargo update --workspace instead of cargo geneate-lockfile * GH-700: improve the script to update the version in a single run * GH-700: print output in different colors * GH-700: sanitize the input * GH-700: improve regex for input sanitization * GH-700: add colors * GH-700: small improvements * GH-700: add the ability to retrieve previous version * GH-700: refactor the functions and add better logging * GH-700: add better error handling * GH-700: hopefully this works on both the OS * GH-700: add the ability to retreieve the prev_version and new_version values * GH-700: write docs for bump_version.sh * GH-700: fix small mistake inside the documentation * GH-700: Refactor the script * GH-700: review 1 changes (#287) * GH-700: review 2 changes
1 parent 0b9523f commit 100af79

File tree

2 files changed

+120
-20
lines changed

2 files changed

+120
-20
lines changed

CONTRIBUTING.md

+33
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,36 @@ discrepancies they find.
261261

262262
Once your PR is approved both by the reviewer and by the QA lead, the reviewer will merge it into the public repo's
263263
`master` branch for you, and you can start on another issue!
264+
265+
#### Versioning
266+
267+
After your code is merged into the `master` branch and approved for a version bump, you can utilize the
268+
`Node/ci/bump_version.sh` script.
269+
270+
The `bump_version.sh` script is designed to modify the version inside `Cargo.toml` files and update the corresponding
271+
`Cargo.lock` files for Rust projects. The following documentation explains how the script works and how to use it.
272+
273+
##### Usage
274+
275+
To use the script, navigate to the `Node/ci` directory and execute the following command:
276+
277+
```bash
278+
./bump_version.sh <version>
279+
```
280+
281+
Where `<version>` is the new version number you want to set. The version number should be in the form `x.y.z`,
282+
where `x`, `y`, and `z` are positive integers. The script validates that the argument is a valid version number and
283+
exits with an error message if the argument is not valid.
284+
285+
Let's say you want to update the version from `6.9.0` to `6.9.1`. Assuming you're inside the `Node/ci` directory.
286+
You can use the following command to run the script:
287+
288+
```bash
289+
./bump_version.sh 6.9.1
290+
```
291+
292+
Note: The script only modifies the version numbers inside the cargo files, and does not connect to the internet or
293+
modify the project's dependencies in any way.
294+
295+
The script is easy to use and validates the command-line argument to ensure that it is a valid version number. It also
296+
reports any errors that occur during the modification process.

ci/bump_version.sh

+87-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
#!/bin/bash
22

3+
RED='\033[0;31m'
4+
GREEN='\033[0;32m'
5+
CYAN='\033[0;36m'
6+
#YELLOW='\033[0;33m'
7+
#BLUE='\033[0;34m'
8+
#MAGENTA='\033[0;35m'
9+
#GRAY='\033[0;37m'
10+
11+
# Reset output color to the default
12+
NC='\033[0m'
13+
314
if [ $# != 1 ]
415
then
516
echo "Please provide version as the first argument"
617
exit 1
718
fi
819

920
version="$1"
21+
regex="^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$"
22+
23+
if [[ $version =~ $regex ]]; then
24+
echo -e "${CYAN}Changing to the version number $version${NC}"
25+
else
26+
echo -e "${RED}Error: Invalid version number provided.${NC}"
27+
echo "The version number should follow the semantic versioning format."
28+
echo "Please ensure the version number consists of MAJOR.MINOR.PATCH"
29+
echo "Example: 0.6.9 or 2.3.1"
30+
exit 1
31+
fi
32+
1033
CI_DIR="$( cd "$( dirname "$0" )" && pwd )"
1134
pushd "$CI_DIR/../"
1235

@@ -15,50 +38,94 @@ echo "Searching for crates..."
1538
crates=($(find . -type d -exec bash -c '[ -f "$0"/Cargo.toml ]' '{}' \; -print))
1639

1740
if [[ "${#crates[@]}" == "0" ]]; then
18-
echo "No crates found."
41+
echo -e "${RED}Error: No crates found."
1942
exit 1
2043
else
21-
echo "Found ${#crates[@]} crate(s): ${crates[*]}"
44+
echo -e "${CYAN}Found ${#crates[@]} crate(s): ${crates[*]}${NC}"
2245
fi
2346

24-
file=Cargo.toml
2547
final_exit_code=0
2648
declare -a grep_failures
2749
declare -a lockfile_failures
2850

29-
bump_version() {
30-
# Catches every `version` that begins a line and doesn't end with a comma.
31-
find_pattern='^version\s*=.*[^,]\s*$'
32-
replace_pattern='s/'$find_pattern'/version = "'"$version"'"/'
51+
# Catches every `version` that begins a line and stores version inside double quotes
52+
find_pattern="^version = \".*\"$"
53+
replace_pattern="s/${find_pattern}/version = \"${version}\"/"
54+
55+
get_current_version() {
56+
local current_version=""
57+
if [ "$(uname)" == "Darwin" ]; then
58+
# macOS
59+
current_version=$(grep -Eo "$find_pattern" "$file" | sed 's/^version = "//;s/"$//')
60+
else
61+
# Linux
62+
current_version="$(grep -oP '(?<=^version = ")[^"]+' "$file" | head -n 1)"
63+
fi
64+
65+
echo "$current_version"
66+
}
67+
68+
replace_version() {
69+
if [ "$(uname)" == "Darwin" ]; then
70+
# macOS
71+
sed -i '' "$replace_pattern" "$file"
72+
else
73+
# Linux
74+
sed -i "$replace_pattern" "$file"
75+
fi
76+
}
77+
78+
find_and_replace() {
79+
local crate=$1
80+
local file="Cargo.toml"
3381

34-
grep -q "$find_pattern" "$file" && sed -i "" "$replace_pattern" "$file"
35-
exit_code="$?"
36-
if [[ "$exit_code" != "0" ]]; then
82+
if grep -q "$find_pattern" "$file"; then
83+
local prev_version=$(get_current_version)
84+
replace_version
85+
local new_version=$(get_current_version)
86+
echo -e "${CYAN} Successfully changed the version inside $file for ${crate#./} (v$prev_version -> v$new_version)${NC}"
87+
else
88+
echo -e "${RED} Error: Failed to change the version inside $file for ${crate#./}${NC}"
3789
final_exit_code=1
38-
grep_failures+=($1)
39-
return
90+
grep_failures+=("$crate")
91+
return 1
4092
fi
93+
}
94+
95+
update_lockfile() {
96+
local crate=$1
97+
local file="Cargo.lock"
4198

42-
cargo generate-lockfile
43-
exit_code="$?"
44-
if [[ "$exit_code" != "0" ]]; then
99+
if cargo update --workspace; then
100+
echo -e "${CYAN} Successfully updated $file for ${crate#./}${NC}"
101+
else
102+
echo -e "${RED} Error: Failed to update $file for ${crate#./}${NC}"
45103
final_exit_code=1
46-
lockfile_failures+=($1)
104+
lockfile_failures+=("$crate")
105+
return 1
47106
fi
48107
}
49108

50109
for crate in "${crates[@]}"
51110
do
52111
pushd "$crate"
53-
bump_version "$crate"
112+
find_and_replace "$crate"
113+
popd
114+
done
115+
116+
117+
for crate in "${crates[@]}"
118+
do
119+
pushd "$crate"
120+
update_lockfile "$crate"
54121
popd
55122
done
56123

57124
if [[ $final_exit_code != 0 ]]; then
58-
[[ "${#grep_failures[@]}" != "0" ]] && echo "Failed to find 'version' for ${#grep_failures[@]} crate(s): ${grep_failures[*]}"
59-
[[ "${#lockfile_failures[@]}" != "0" ]] && echo "Failed to generate lockfile for ${#lockfile_failures[@]} crate(s): ${lockfile_failures[*]}"
125+
[[ "${#grep_failures[@]}" != "0" ]] && echo -e "${RED} Error: Failed to find 'version' for ${#grep_failures[@]} crate(s): ${grep_failures[*]}"
126+
[[ "${#lockfile_failures[@]}" != "0" ]] && echo -e "${RED} Error: Failed to generate lockfile for ${#lockfile_failures[@]} crate(s): ${lockfile_failures[*]}"
60127
else
61-
echo "The version number has been changed to $version."
128+
echo -e "${GREEN} The version number has been updated to $version."
62129
fi
63130

64131
exit $final_exit_code

0 commit comments

Comments
 (0)