diff --git a/scripts/bump-and-tag-version.sh b/scripts/bump-and-tag-version.sh index 6406d48..1cadbbd 100644 --- a/scripts/bump-and-tag-version.sh +++ b/scripts/bump-and-tag-version.sh @@ -22,12 +22,12 @@ source "$SCRIPTS_DIRECTORY/shared/utilities.sh" tag_and_push() { local -r tag="$1" - echo "Creating tag: $tag" + echo "Creating tag: \"$tag\"" git tag "$tag" \ - || { echo "Could not tag: $tag" ; exit 1; } + || { echo "Could not tag: \"$tag\"" ; exit 1; } git push -u origin master "$tag" \ - || { echo "Could not push the tag: $tag"; exit 1; } - echo "Tag created and pushed: $tag" + || { echo "Could not push the tag: \"$tag\""; exit 1; } + echo "Tag created and pushed: \"$tag\"" } increase_patch_version() { @@ -52,7 +52,14 @@ is_latest_commit_tagged() { echo "Could not check the tags of the commit $latest_commit" exit 1 fi - if has_value "$tag_of_latest_commit"; then return 0; else return 1; fi + if ! has_value "$tag_of_latest_commit"; then + return 1; + fi + if ! is_valid_semantic_version_string "$tag_of_latest_commit"; then + echo "Latest commit tag \"$tag_of_latest_commit\" in commit \"$latest_commit\" is not a version string" + exit 1 + fi + return 0 } main() { @@ -66,18 +73,17 @@ main() { exit 0 fi local last_version - if ! last_version=$(print_latest_version) \ - || ! has_value "$last_version"; then - echo "Could not retrieve latest version." + if ! last_version=$(print_latest_version); then + echo "Could not retrieve latest version. $last_version" exit 1 fi local new_version if ! new_version=$(increase_patch_version "$last_version") \ - || ! has_value "$new_version"; then + || is_empty_or_null "$new_version"; then echo "Could not increase the version" exit 1 fi - echo "Updating $last_version to $new_version" + echo "Updating \"$last_version\" to \"$new_version\"" tag_and_push "$new_version" } diff --git a/scripts/bump-everywhere.sh b/scripts/bump-everywhere.sh index 42a87c0..40ada26 100644 --- a/scripts/bump-everywhere.sh +++ b/scripts/bump-everywhere.sh @@ -121,13 +121,12 @@ create_changelog() { } update_npm() { - local -r latest_version_tag="$1" local -r file_name="package.json" if ! file_exists "$file_name"; then return 0 fi echo "Updating npm version" - bash "$SCRIPTS_DIRECTORY/bump-npm-version.sh" --version "$latest_version_tag" \ + bash "$SCRIPTS_DIRECTORY/bump-npm-version.sh" \ || { echo "Could not bump npm version"; exit 1; } git add "$file_name" \ || { echo "git add failed for $file_name"; exit 1; } @@ -172,12 +171,11 @@ main() { update_readme create_changelog local version_tag - if ! version_tag="$(print_latest_version)" \ - || is_empty_or_null "$version_tag"; then - echo "Could not retrieve latest version" + if ! version_tag="$(print_latest_version)"; then + echo "Could not retrieve latest version. $version_tag" exit 1 fi - update_npm "$version_tag" + update_npm commit_and_push "$version_tag" create_release } diff --git a/scripts/bump-npm-version.sh b/scripts/bump-npm-version.sh index b9ba612..c2ba397 100644 --- a/scripts/bump-npm-version.sh +++ b/scripts/bump-npm-version.sh @@ -10,21 +10,12 @@ # - Local: ./shared/utilities.sh # Globals -readonly PACKAGES_FILE_NAME="package.json" readonly SCRIPTS_DIRECTORY=$(dirname "$0") # Import dependencies # shellcheck source=scripts/shared/utilities.sh source "$SCRIPTS_DIRECTORY/shared/utilities.sh" -# Parse parameters -while [[ "$#" -gt 0 ]]; do case $1 in - --version) NEW_VERSION="$2"; shift;; - *) echo "Unknown parameter passed: $1"; exit 1;; -esac; shift; done - -# Validate parameters -if is_empty_or_null "$NEW_VERSION"; then echo "New version is not set."; exit 1; fi; match_and_replace_version() { local -r content="$1" new_version="$2" @@ -36,8 +27,8 @@ match_and_replace_version() { bump_npm_package_version() { local -r new_version="$1" file_name="$2" local original - if ! original="$(cat $PACKAGES_FILE_NAME)"; then - echo "Could read \"$PACKAGES_FILE_NAME\"" + if ! original=$(cat "$file_name"); then + echo "Could read \"$file_name\"" exit 1 fi local updated @@ -50,8 +41,18 @@ bump_npm_package_version() { } main() { - bump_npm_package_version "${NEW_VERSION}" "$PACKAGES_FILE_NAME" - echo "Updated npm version to ${NEW_VERSION}" + local -r file_name="package.json" + if ! file_exists "$file_name"; then + echo "Skipping.. No $file_name file exists." + exit 0; + fi + local new_version + if ! new_version=$(print_latest_version); then + echo "Could not retrieve the new version. $new_version" + exit 1; + fi + bump_npm_package_version "$new_version" "$file_name" + echo "Updated npm version to $new_version" } main \ No newline at end of file diff --git a/scripts/bump-readme-versions.sh b/scripts/bump-readme-versions.sh index df48b26..066c9ce 100644 --- a/scripts/bump-readme-versions.sh +++ b/scripts/bump-readme-versions.sh @@ -38,11 +38,14 @@ file_content_contains() { } main() { - local version_before - if ! version_before=$(print_previous_version) \ - || is_empty_or_null "$version_before"; then + if has_single_version; then echo "Skipping.. There were no versions before." - exit 0; + return 0 + fi + local version_before + if ! version_before=$(print_previous_version); then + echo "Could not get the version before. $version_before" + exit 1; fi local -r file_name="README.md" if ! file_exists "$file_name"; then @@ -54,9 +57,9 @@ main() { exit 0; fi local new_version - if ! new_version=$(print_latest_version) \ - || is_empty_or_null "$new_version"; then - echo "Could not retrieve the new version" + if ! new_version=$(print_latest_version); then + echo "Could not retrieve the new version. $new_version" + exit 1; fi search_and_replace "$file_name" "$version_before" "$new_version" git add "$file_name" \ diff --git a/scripts/create-github-release.sh b/scripts/create-github-release.sh index 3c95a28..14e9f1b 100644 --- a/scripts/create-github-release.sh +++ b/scripts/create-github-release.sh @@ -56,18 +56,6 @@ release_exists() { fi } -has_single_version() { - local -i total_tags - if ! total_tags=$(count_tags); then - echo "Could not count tags" - exit 1 - fi - if [ "$total_tags" -eq "1" ]; then - return 0 # There is only a a single tag - fi - return 1 # There are none or multiple tags -} - print_release_notes() { local -r version="$1" if has_single_version; then @@ -75,9 +63,8 @@ print_release_notes() { return 0 fi local version_before - if ! version_before=$(print_previous_version) \ - || is_empty_or_null "$version_before"; then - echo "Could not get the previous version" + if ! version_before=$(print_previous_version); then + echo "Could not get the previous version. $version_before" exit 1 fi local changes @@ -120,9 +107,8 @@ create_release() { main() { local latest_version - if ! latest_version=$(print_latest_version) \ - || is_empty_or_null "$latest_version"; then - echo "Could not get the latest version" + if ! latest_version=$(print_latest_version); then + echo "Could not get the latest version. $latest_version" exit 1; fi if ! release_exists "$latest_version"; then diff --git a/scripts/shared/utilities.sh b/scripts/shared/utilities.sh index e87c8cc..43c1f35 100644 --- a/scripts/shared/utilities.sh +++ b/scripts/shared/utilities.sh @@ -23,19 +23,60 @@ repository_has_any_tags() { [[ "$total_tags" -ne 0 ]] } -print_previous_version() { +is_valid_semantic_version_string() { + local version="$1" + local -i -r MAX_LENGTH=256 # for package.json compatibility: https://github.com/npm/node-semver/blob/master/internal/constants.js + if (( ${#version} > MAX_LENGTH )); then + echo "Version \"$version\" is too long (max: $MAX_LENGTH, but was: ${#version})" + return 1 + fi + if [[ ! $version =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then + echo "Version \"$version\" is invalid (not in \`major.minor.patch\` format)" + return 1 + fi + return 0 +} + +# Prints latest version, exists with positive code if it cannot +print_latest_version() { + if ! repository_has_any_tags; then + exit 1; + fi + local -r latest_tag=$(git tag | sort -V | tail -1) + if ! is_valid_semantic_version_string "$latest_tag"; then + exit 1; + fi + echo "$latest_tag" +} + +has_single_version() { local -i total_tags if ! total_tags=$(count_tags); then echo "Could not count tags" exit 1 fi - if [ "$total_tags" -le 1 ]; then exit 1; fi - git tag | sort -V | tail -2 | head -1 + if [ "$total_tags" -eq "1" ]; then + return 0 # There is only a a single tag + fi + return 1 # There are none or multiple tags } -print_latest_version() { - if ! repository_has_any_tags; then exit 1; fi - git tag | sort -V | tail -1 +# Prints latest version, exists with positive code if it cannot +print_previous_version() { + local -i total_tags + if ! total_tags=$(count_tags); then + echo "Could not count tags" + exit 1 + fi + if [ "$total_tags" -le 1 ]; then + echo "There's only a single version" + exit 1; + fi + local -r previous_tag=$(git tag | sort -V | tail -2 | head -1) + if ! is_valid_semantic_version_string "$previous_tag"; then + exit 1; + fi + echo "$previous_tag" } file_exists() {