Skip to content

Commit

Permalink
validate version (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
undergroundwires committed Jul 16, 2020
1 parent 2bbb364 commit aff2c23
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 60 deletions.
26 changes: 16 additions & 10 deletions scripts/bump-and-tag-version.sh
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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"
}

Expand Down
10 changes: 4 additions & 6 deletions scripts/bump-everywhere.sh
Expand Up @@ -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; }
Expand Down Expand Up @@ -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
}
Expand Down
27 changes: 14 additions & 13 deletions scripts/bump-npm-version.sh
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
17 changes: 10 additions & 7 deletions scripts/bump-readme-versions.sh
Expand Up @@ -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
Expand All @@ -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" \
Expand Down
22 changes: 4 additions & 18 deletions scripts/create-github-release.sh
Expand Up @@ -56,28 +56,15 @@ 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
echo "Initial release"
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
Expand Down Expand Up @@ -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
Expand Down
53 changes: 47 additions & 6 deletions scripts/shared/utilities.sh
Expand Up @@ -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() {
Expand Down

0 comments on commit aff2c23

Please sign in to comment.