From 100af79796ce617729ca2ad2198780aead1344b8 Mon Sep 17 00:00:00 2001 From: Utkarsh Gupta <32920299+utkarshg6@users.noreply.github.com> Date: Fri, 26 May 2023 13:54:37 +0530 Subject: [PATCH] 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 --- CONTRIBUTING.md | 33 ++++++++++++++ ci/bump_version.sh | 107 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 120 insertions(+), 20 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 403f805f6..bf3f6ec5b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -261,3 +261,36 @@ discrepancies they find. Once your PR is approved both by the reviewer and by the QA lead, the reviewer will merge it into the public repo's `master` branch for you, and you can start on another issue! + +#### Versioning + +After your code is merged into the `master` branch and approved for a version bump, you can utilize the +`Node/ci/bump_version.sh` script. + +The `bump_version.sh` script is designed to modify the version inside `Cargo.toml` files and update the corresponding +`Cargo.lock` files for Rust projects. The following documentation explains how the script works and how to use it. + +##### Usage + +To use the script, navigate to the `Node/ci` directory and execute the following command: + +```bash +./bump_version.sh +``` + +Where `` is the new version number you want to set. The version number should be in the form `x.y.z`, +where `x`, `y`, and `z` are positive integers. The script validates that the argument is a valid version number and +exits with an error message if the argument is not valid. + +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. +You can use the following command to run the script: + +```bash +./bump_version.sh 6.9.1 +``` + +Note: The script only modifies the version numbers inside the cargo files, and does not connect to the internet or +modify the project's dependencies in any way. + +The script is easy to use and validates the command-line argument to ensure that it is a valid version number. It also +reports any errors that occur during the modification process. diff --git a/ci/bump_version.sh b/ci/bump_version.sh index de32eea3c..7790787ea 100755 --- a/ci/bump_version.sh +++ b/ci/bump_version.sh @@ -1,5 +1,16 @@ #!/bin/bash +RED='\033[0;31m' +GREEN='\033[0;32m' +CYAN='\033[0;36m' +#YELLOW='\033[0;33m' +#BLUE='\033[0;34m' +#MAGENTA='\033[0;35m' +#GRAY='\033[0;37m' + +# Reset output color to the default +NC='\033[0m' + if [ $# != 1 ] then echo "Please provide version as the first argument" @@ -7,6 +18,18 @@ then fi version="$1" +regex="^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$" + +if [[ $version =~ $regex ]]; then + echo -e "${CYAN}Changing to the version number $version${NC}" +else + echo -e "${RED}Error: Invalid version number provided.${NC}" + echo "The version number should follow the semantic versioning format." + echo "Please ensure the version number consists of MAJOR.MINOR.PATCH" + echo "Example: 0.6.9 or 2.3.1" + exit 1 +fi + CI_DIR="$( cd "$( dirname "$0" )" && pwd )" pushd "$CI_DIR/../" @@ -15,50 +38,94 @@ echo "Searching for crates..." crates=($(find . -type d -exec bash -c '[ -f "$0"/Cargo.toml ]' '{}' \; -print)) if [[ "${#crates[@]}" == "0" ]]; then - echo "No crates found." + echo -e "${RED}Error: No crates found." exit 1 else - echo "Found ${#crates[@]} crate(s): ${crates[*]}" + echo -e "${CYAN}Found ${#crates[@]} crate(s): ${crates[*]}${NC}" fi -file=Cargo.toml final_exit_code=0 declare -a grep_failures declare -a lockfile_failures -bump_version() { - # Catches every `version` that begins a line and doesn't end with a comma. - find_pattern='^version\s*=.*[^,]\s*$' - replace_pattern='s/'$find_pattern'/version = "'"$version"'"/' +# Catches every `version` that begins a line and stores version inside double quotes +find_pattern="^version = \".*\"$" +replace_pattern="s/${find_pattern}/version = \"${version}\"/" + +get_current_version() { + local current_version="" + if [ "$(uname)" == "Darwin" ]; then + # macOS + current_version=$(grep -Eo "$find_pattern" "$file" | sed 's/^version = "//;s/"$//') + else + # Linux + current_version="$(grep -oP '(?<=^version = ")[^"]+' "$file" | head -n 1)" + fi + + echo "$current_version" +} + +replace_version() { + if [ "$(uname)" == "Darwin" ]; then + # macOS + sed -i '' "$replace_pattern" "$file" + else + # Linux + sed -i "$replace_pattern" "$file" + fi +} + +find_and_replace() { + local crate=$1 + local file="Cargo.toml" - grep -q "$find_pattern" "$file" && sed -i "" "$replace_pattern" "$file" - exit_code="$?" - if [[ "$exit_code" != "0" ]]; then + if grep -q "$find_pattern" "$file"; then + local prev_version=$(get_current_version) + replace_version + local new_version=$(get_current_version) + echo -e "${CYAN} Successfully changed the version inside $file for ${crate#./} (v$prev_version -> v$new_version)${NC}" + else + echo -e "${RED} Error: Failed to change the version inside $file for ${crate#./}${NC}" final_exit_code=1 - grep_failures+=($1) - return + grep_failures+=("$crate") + return 1 fi +} + +update_lockfile() { + local crate=$1 + local file="Cargo.lock" - cargo generate-lockfile - exit_code="$?" - if [[ "$exit_code" != "0" ]]; then + if cargo update --workspace; then + echo -e "${CYAN} Successfully updated $file for ${crate#./}${NC}" + else + echo -e "${RED} Error: Failed to update $file for ${crate#./}${NC}" final_exit_code=1 - lockfile_failures+=($1) + lockfile_failures+=("$crate") + return 1 fi } for crate in "${crates[@]}" do pushd "$crate" - bump_version "$crate" + find_and_replace "$crate" + popd +done + + +for crate in "${crates[@]}" +do + pushd "$crate" + update_lockfile "$crate" popd done if [[ $final_exit_code != 0 ]]; then - [[ "${#grep_failures[@]}" != "0" ]] && echo "Failed to find 'version' for ${#grep_failures[@]} crate(s): ${grep_failures[*]}" - [[ "${#lockfile_failures[@]}" != "0" ]] && echo "Failed to generate lockfile for ${#lockfile_failures[@]} crate(s): ${lockfile_failures[*]}" + [[ "${#grep_failures[@]}" != "0" ]] && echo -e "${RED} Error: Failed to find 'version' for ${#grep_failures[@]} crate(s): ${grep_failures[*]}" + [[ "${#lockfile_failures[@]}" != "0" ]] && echo -e "${RED} Error: Failed to generate lockfile for ${#lockfile_failures[@]} crate(s): ${lockfile_failures[*]}" else - echo "The version number has been changed to $version." + echo -e "${GREEN} The version number has been updated to $version." fi exit $final_exit_code