From d0f3fbf8fe8c579e08112be5a9979d9e691f32f8 Mon Sep 17 00:00:00 2001 From: Nicholas McDonnell <50747025+mcdonnnj@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:07:32 -0400 Subject: [PATCH 1/6] Refactor the `bump_version.sh` script This updates the script to allow you to simultaneously bump the prerelease with the major, minor, or patch versions; de-duplicates logic for modifying and committing files; allows you to set the label used when bumping the build or prerelease versions; and makes performing a `git push` an optional flag. --- bump_version.sh | 156 +++++++++++++++++++++++++++++++++---------- requirements-dev.txt | 2 +- 2 files changed, 122 insertions(+), 36 deletions(-) diff --git a/bump_version.sh b/bump_version.sh index a084b03..a5619f7 100755 --- a/bump_version.sh +++ b/bump_version.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# bump_version.sh (show|major|minor|patch|prerelease|build|finalize) +# bump_version.sh [--push] [--label LABEL] (major | minor | patch | prerelease | build | finalize | show) set -o nounset set -o errexit @@ -9,43 +9,129 @@ set -o pipefail VERSION_FILE=config/version.txt README_FILE=README.md -HELP_INFORMATION="bump_version.sh (show|major|minor|patch|prerelease|build|finalize)" +HELP_INFORMATION="bump_version.sh [--push] [--label LABEL] (major | minor | patch | prerelease | build | finalize | show)" old_version=$(< "$VERSION_FILE") +new_version="$old_version" -if [ $# -ne 1 ]; then +bump_part="" +label="" +commit_prefix="Bump" +with_push=false +commands_with_label=("build" "prerelease") +commands_with_prerelease=("major" "minor" "patch") +with_prerelease=false + +####################################### +# Display an error message, the help information, and exit with a non-zero status. +# Arguments: +# Error message. +####################################### +function invalid_option() { + echo "$1" + echo "$HELP_INFORMATION" + exit 1 +} + +####################################### +# Bump the version using the provided command. +# Arguments: +# The version to bump. +# The command to bump the version. +# Returns: +# The new version. +####################################### +function bump_version() { + local temp_version + temp_version=$(python -c "import semver; print(semver.parse_version_info('$1').${2})") + echo "$temp_version" +} + +if [ $# -eq 0 ]; then echo "$HELP_INFORMATION" else - case $1 in - major | minor | patch | prerelease | build) - new_version=$(python -c "import semver; print(semver.bump_$1('$old_version'))") - echo Changing version from "$old_version" to "$new_version" - tmp_file=/tmp/version.$$ - sed "s/$old_version/$new_version/" $VERSION_FILE > $tmp_file - mv $tmp_file $VERSION_FILE - sed "s/$old_version/$new_version/" $README_FILE > $tmp_file - mv $tmp_file $README_FILE - git add $VERSION_FILE $README_FILE - git commit -m"Bump version from $old_version to $new_version" - git push - ;; - finalize) - new_version=$(python -c "import semver; print(semver.finalize_version('$old_version'))") - echo Changing version from "$old_version" to "$new_version" - tmp_file=/tmp/version.$$ - sed "s/$old_version/$new_version/" $VERSION_FILE > $tmp_file - mv $tmp_file $VERSION_FILE - sed "s/$old_version/$new_version/" $README_FILE > $tmp_file - mv $tmp_file $README_FILE - git add $VERSION_FILE $README_FILE - git commit -m"Bump version from $old_version to $new_version" - git push - ;; - show) - echo "$old_version" - ;; - *) - echo "$HELP_INFORMATION" - ;; - esac + while [ $# -gt 0 ]; do + case $1 in + --push) + if [ "$with_push" = true ]; then + invalid_option "Push has already been set." + fi + + with_push=true + shift + ;; + --label) + if [ -n "$label" ]; then + invalid_option "Label has already been set." + fi + + label="$2" + shift 2 + ;; + build | finalize | major | minor | patch) + if [ -n "$bump_part" ]; then + invalid_option "Only one version part should be bumped at a time." + fi + + bump_part="$1" + shift + ;; + prerelease) + with_prerelease=true + shift + ;; + show) + echo "$old_version" + exit 0 + ;; + *) + invalid_option "Invalid option: $1" + ;; + esac + done +fi + +if [ -n "$label" ] && [ "$with_prerelease" = false ] && [[ ! " ${commands_with_label[*]} " =~ [[:space:]]${bump_part}[[:space:]] ]]; then + invalid_option "Setting the label is only allowed for the following commands: ${commands_with_label[*]}" +fi + +if [ "$with_prerelease" = true ] && [[ ! " ${commands_with_prerelease[*]} " =~ [[:space:]]${bump_part}[[:space:]] ]]; then + invalid_option "Changing the prerelease is only allowed in conjunction with the following commands: ${commands_with_prerelease[*]}" +fi + +label_option="" +if [ -n "$label" ]; then + label_option="token='$label'" +fi + +if [ -n "$bump_part" ]; then + if [ "$bump_part" = "finalize" ]; then + commit_prefix="Finalize" + bump_command="finalize_version()" + elif [ "$bump_part" = "build" ]; then + bump_command="bump_${bump_part}($label_option)" + else + bump_command="bump_${bump_part}()" + fi + new_version=$(bump_version "$old_version" "$bump_command") + echo Changing version from "$old_version" to "$new_version" +fi + +if [ "$with_prerelease" = true ]; then + bump_command="bump_prerelease($label_option)" + temp_version=$(bump_version "$new_version" "$bump_command") + echo Changing version from "$new_version" to "$temp_version" + new_version="$temp_version" +fi + +tmp_file=/tmp/version.$$ +sed "s/$old_version/$new_version/" $VERSION_FILE > $tmp_file +mv $tmp_file $VERSION_FILE +sed "s/$old_version/$new_version/" $README_FILE > $tmp_file +mv $tmp_file $README_FILE +git add $VERSION_FILE $README_FILE +git commit --message "$commit_prefix version from $old_version to $new_version" + +if [ "$with_push" = true ]; then + git push fi diff --git a/requirements-dev.txt b/requirements-dev.txt index cb51627..de5eb3b 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,3 +1,3 @@ --requirement requirements-test.txt ipython -semver +semver>=3 From 9e8a31efcd16fdbde3fc7cf63ce357b593cba4a1 Mon Sep 17 00:00:00 2001 From: Nicholas McDonnell <50747025+mcdonnnj@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:25:29 -0400 Subject: [PATCH 2/6] Adjust usage information for the `bump_version.sh` script Adjust the usage information to document some new options, make the command name in the usage based on the file name for the script, and add explicit help flags. --- bump_version.sh | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/bump_version.sh b/bump_version.sh index a5619f7..40d8098 100755 --- a/bump_version.sh +++ b/bump_version.sh @@ -9,7 +9,20 @@ set -o pipefail VERSION_FILE=config/version.txt README_FILE=README.md -HELP_INFORMATION="bump_version.sh [--push] [--label LABEL] (major | minor | patch | prerelease | build | finalize | show)" +USAGE=$( + cat << END_OF_LINE +Update the version of the project. + +Usage: + ${0##*/} [--push] [--label LABEL] (major | minor | patch | prerelease | build | finalize | show) + ${0##*/} (-h | --help) + +Options: + -h | --help Show this message. + --push Perform a \`git push\` after updating the version. + --label LABEL Specify the label to use when updating the build or prerelease version. +END_OF_LINE +) old_version=$(< "$VERSION_FILE") new_version="$old_version" @@ -29,7 +42,7 @@ with_prerelease=false ####################################### function invalid_option() { echo "$1" - echo "$HELP_INFORMATION" + echo "$USAGE" exit 1 } @@ -48,7 +61,8 @@ function bump_version() { } if [ $# -eq 0 ]; then - echo "$HELP_INFORMATION" + echo "$USAGE" + exit 1 else while [ $# -gt 0 ]; do case $1 in @@ -84,6 +98,10 @@ else echo "$old_version" exit 0 ;; + -h | --help) + echo "$USAGE" + exit 0 + ;; *) invalid_option "Invalid option: $1" ;; From 6d44cd64ba86c368f7e0b27a579dda9515ff263b Mon Sep 17 00:00:00 2001 From: Nicholas McDonnell <50747025+mcdonnnj@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:27:41 -0400 Subject: [PATCH 3/6] Rename version management script This treats this script like the `setup-env` script already present. --- bump_version.sh => bump-version | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bump_version.sh => bump-version (100%) diff --git a/bump_version.sh b/bump-version similarity index 100% rename from bump_version.sh rename to bump-version From 032ae24609fd67aab6c36610d77437fb8b39591e Mon Sep 17 00:00:00 2001 From: Nicholas McDonnell <50747025+mcdonnnj@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:29:31 -0400 Subject: [PATCH 4/6] Bump version from 0.0.2 to 0.1.0-rc.1 --- README.md | 2 +- config/version.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bb7f3b8..b472fee 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ against any directory that houses `.pkr.hcl` files. ```yaml repos: - repo: https://github.com/cisagov/pre-commit-packer - rev: v0.0.2 + rev: v0.1.0-rc.1 hooks: - id: packer_fmt - id: packer_validate diff --git a/config/version.txt b/config/version.txt index 4e379d2..3738566 100644 --- a/config/version.txt +++ b/config/version.txt @@ -1 +1 @@ -0.0.2 +0.1.0-rc.1 From 904ba1e0aab4b69f579fa17500f4fb107e569391 Mon Sep 17 00:00:00 2001 From: Nick <50747025+mcdonnnj@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:21:42 -0400 Subject: [PATCH 5/6] Fix script name reference in the `bump-version` script A reference in the script was not updated when the script had its name changed. Co-authored-by: dav3r --- bump-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bump-version b/bump-version index 40d8098..4fbae43 100755 --- a/bump-version +++ b/bump-version @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# bump_version.sh [--push] [--label LABEL] (major | minor | patch | prerelease | build | finalize | show) +# bump-version [--push] [--label LABEL] (major | minor | patch | prerelease | build | finalize | show) set -o nounset set -o errexit From dd120114f6e39755755357dd5b6f34f7ada0e37e Mon Sep 17 00:00:00 2001 From: Nicholas McDonnell <50747025+mcdonnnj@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:37:37 -0400 Subject: [PATCH 6/6] Finalize version from 0.1.0-rc.1 to 0.1.0 --- README.md | 2 +- config/version.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b472fee..350857a 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ against any directory that houses `.pkr.hcl` files. ```yaml repos: - repo: https://github.com/cisagov/pre-commit-packer - rev: v0.1.0-rc.1 + rev: v0.1.0 hooks: - id: packer_fmt - id: packer_validate diff --git a/config/version.txt b/config/version.txt index 3738566..6e8bf73 100644 --- a/config/version.txt +++ b/config/version.txt @@ -1 +1 @@ -0.1.0-rc.1 +0.1.0