2021-08-31 00:10:48 +02:00
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
|
|
myself="$(basename "$0")"
|
|
|
|
|
version_file="$(pwd)/mobilizon_reshare/VERSION"
|
|
|
|
|
pyproject_toml="$(pwd)/pyproject.toml"
|
2021-10-31 01:55:25 +02:00
|
|
|
|
docker_compose_yml="$(pwd)/docker-compose.yml"
|
2022-04-11 08:36:10 +02:00
|
|
|
|
completion_dir="$(pwd)/etc"
|
2021-09-28 11:38:13 +02:00
|
|
|
|
current_branch="$(git rev-parse --abbrev-ref HEAD)"
|
|
|
|
|
current_commit="$(git log -1 --format='%H')"
|
2021-08-31 00:10:48 +02:00
|
|
|
|
dryrun=0
|
|
|
|
|
verbose=0
|
|
|
|
|
publish=0
|
|
|
|
|
bump="INVALID"
|
|
|
|
|
|
|
|
|
|
required_commands=("getopt" "pysemver" "git")
|
|
|
|
|
valid_types=('major' 'minor' 'patch' 'prerelease' 'build')
|
|
|
|
|
|
|
|
|
|
error() {
|
|
|
|
|
echo -e "$@" >/dev/fd/2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
crash() {
|
|
|
|
|
[ "$#" -gt 0 ] && error "$1"
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
check-dependencies() {
|
|
|
|
|
for com in "${required_commands[@]}"; do
|
|
|
|
|
if ! command -v "$com" >/dev/null; then
|
|
|
|
|
error "$com not found but it's required! Please install it with your favourite package manager."
|
|
|
|
|
crash "\n\t\t\t\tOr just use Guix ;)\n"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
|
cat <<EOF
|
|
|
|
|
Usage: ${myself} -b <bump-type> [-hpbvd]
|
|
|
|
|
Release a new version of Mobilizon Reshare according to https://semver.org
|
|
|
|
|
|
|
|
|
|
-h, --help Show this help message.
|
|
|
|
|
|
|
|
|
|
-b, --bump Select a version bump type
|
|
|
|
|
|
|
|
|
|
-p, --publish Publish current release to PyPI.
|
|
|
|
|
|
|
|
|
|
-d, --dryrun Show operations, instead of carrying them out.
|
|
|
|
|
|
|
|
|
|
--list-types List all valid version bump types.
|
|
|
|
|
|
|
|
|
|
-v, --verbose Run script in verbose mode. Will print out each step of execution.
|
|
|
|
|
EOF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validate-bump-type() {
|
|
|
|
|
if [[ ${valid_types[*]} =~ (^|[[:space:]])"$1"($|[[:space:]]) ]]; then
|
|
|
|
|
true
|
|
|
|
|
else
|
|
|
|
|
crash "Invalid bump type: $1"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validate-pypi-token() {
|
|
|
|
|
if [ -z ${PYPI_TOKEN+x} ]; then
|
|
|
|
|
crash "The PYPI_TOKEN environment variable is required but unset. Please set it to upload the new release to PyPI."
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current-version() {
|
|
|
|
|
cat "${version_file}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
release-new-version() {
|
|
|
|
|
current="$(current-version)"
|
|
|
|
|
next="$(pysemver bump "$1" "$current")"
|
2022-04-11 08:36:10 +02:00
|
|
|
|
echo "RELEASING VERSION: ${next}"
|
2021-08-31 00:10:48 +02:00
|
|
|
|
|
2022-04-11 08:36:10 +02:00
|
|
|
|
[ "$verbose" = "1" ] && echo "Updating ${version_file}..."
|
2021-08-31 00:10:48 +02:00
|
|
|
|
[ "$dryrun" = "0" ] && printf "%s" "$next" >"$version_file"
|
|
|
|
|
|
2022-04-11 08:36:10 +02:00
|
|
|
|
[ "$verbose" = "1" ] && echo "Updating ${pyproject_toml}..."
|
2021-08-31 00:10:48 +02:00
|
|
|
|
[ "$dryrun" = "0" ] && sed -i -E "s/version.*=.*\"${current}\"$/version = \"${next}\"/" "$pyproject_toml"
|
|
|
|
|
|
2022-04-11 08:36:10 +02:00
|
|
|
|
[ "$verbose" = "1" ] && echo "Updating ${docker_compose_yml}..."
|
2021-10-31 01:55:25 +02:00
|
|
|
|
[ "$dryrun" = "0" ] && sed -i "s/${current}/${next}/" "$docker_compose_yml"
|
|
|
|
|
|
2022-04-11 08:36:10 +02:00
|
|
|
|
[ "$verbose" = "1" ] && echo "Generating completion scripts in ${completion_dir}..."
|
|
|
|
|
[ "$dryrun" = "0" ] && scripts/generate_completion_scripts.sh
|
|
|
|
|
|
|
|
|
|
[ "$verbose" = "1" ] && echo "Committing ${pyproject_toml}, ${completion_dir}, ${docker_compose_yml} and ${version_file}"
|
|
|
|
|
[ "$dryrun" = "0" ] && git add "$docker_compose_yml" \
|
|
|
|
|
"${pyproject_toml}" \
|
2022-04-19 21:52:45 +02:00
|
|
|
|
"${completion_dir}" \
|
2022-04-11 08:36:10 +02:00
|
|
|
|
"${version_file}" && \
|
|
|
|
|
git commit -m "Release v${next}."
|
2021-08-31 00:10:48 +02:00
|
|
|
|
|
|
|
|
|
[ "$verbose" = "1" ] && echo "Tagging Git HEAD with v${next}"
|
|
|
|
|
[ "$dryrun" = "0" ] && git tag "v${next}"
|
|
|
|
|
|
|
|
|
|
[ "$verbose" = "1" ] && echo "Building package..."
|
|
|
|
|
[ "$dryrun" = "0" ] && poetry build
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-26 20:12:06 +02:00
|
|
|
|
restore-git-state () {
|
|
|
|
|
# This way we can go back to a
|
|
|
|
|
# detached HEAD state as well.
|
2021-09-28 11:38:13 +02:00
|
|
|
|
if [ "${current_branch}" = "HEAD" ]; then
|
|
|
|
|
git checkout "${current_commit}"
|
2021-09-26 20:12:06 +02:00
|
|
|
|
else
|
2021-09-28 11:38:13 +02:00
|
|
|
|
git checkout "${current_branch}"
|
2021-09-26 20:12:06 +02:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 00:10:48 +02:00
|
|
|
|
parse-args() {
|
|
|
|
|
[ "$#" -eq 0 ] && crash "$(usage)"
|
|
|
|
|
|
|
|
|
|
options=$(getopt -l "help,publish,bump:,list-types,verbose,dryrun" -o "hpb:vd" -- "$@")
|
|
|
|
|
|
|
|
|
|
# set --:
|
|
|
|
|
# If no arguments follow this option, then the positional parameters are unset. Otherwise, the positional parameters
|
|
|
|
|
# are set to the arguments, even if some of them begin with a ‘-’.
|
|
|
|
|
eval set -- "$options"
|
|
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
|
case $1 in
|
|
|
|
|
-h | --help)
|
|
|
|
|
usage
|
|
|
|
|
exit 0
|
|
|
|
|
;;
|
|
|
|
|
--list-types)
|
|
|
|
|
echo "${valid_types[@]}"
|
|
|
|
|
exit 0
|
|
|
|
|
;;
|
|
|
|
|
-v | --verbose)
|
|
|
|
|
verbose=1
|
|
|
|
|
set -vx
|
|
|
|
|
;;
|
|
|
|
|
-d | --dryrun)
|
|
|
|
|
dryrun=1
|
|
|
|
|
verbose=1
|
|
|
|
|
;;
|
|
|
|
|
-p | --publish)
|
|
|
|
|
publish=1
|
|
|
|
|
;;
|
|
|
|
|
-b | --bump)
|
|
|
|
|
shift
|
|
|
|
|
bump="$1"
|
|
|
|
|
;;
|
|
|
|
|
--)
|
|
|
|
|
shift
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
shift
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
if [ "$bump" = "INVALID" ] && [ "$publish" = "0" ]; then
|
|
|
|
|
error "You can either build a new release or publish the current release to PyPI."
|
|
|
|
|
crash "See ${myself} -h for more information."
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parse-args "$@"
|
|
|
|
|
|
|
|
|
|
if [ "$bump" != "INVALID" ]; then
|
|
|
|
|
check-dependencies
|
|
|
|
|
validate-bump-type "$bump"
|
|
|
|
|
release-new-version "$bump"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$publish" = "1" ]; then
|
|
|
|
|
validate-pypi-token
|
2021-09-26 20:12:06 +02:00
|
|
|
|
|
|
|
|
|
# We make sure to actually publish the tagged version
|
|
|
|
|
git checkout "v$(current-version)"
|
|
|
|
|
set +e
|
|
|
|
|
# If this command fails we still want to go back to the
|
|
|
|
|
# branch we were on.
|
2021-08-31 00:10:48 +02:00
|
|
|
|
poetry publish -u "__token__" -p "$PYPI_TOKEN"
|
2021-09-26 20:12:06 +02:00
|
|
|
|
set -e
|
|
|
|
|
restore-git-state
|
2021-08-31 00:10:48 +02:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
exit 0
|