|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Update version number and prepare for publishing the new version |
| 4 | + |
| 5 | +set -e |
| 6 | + |
| 7 | +# Empty to run the rest of the line and "echo" for a dry run |
| 8 | +CMD_PREFIX= |
| 9 | + |
| 10 | +# Add a quote if this is a dry run |
| 11 | +QUOTE= |
| 12 | + |
| 13 | +NEW_VERSION= |
| 14 | + |
| 15 | +UPDATE_ONLY=false |
| 16 | + |
| 17 | +function echo_err |
| 18 | +{ |
| 19 | + echo "$@" 1>&2; |
| 20 | +} |
| 21 | + |
| 22 | +function usage |
| 23 | +{ |
| 24 | + echo "Usage: $0 [parameters]" |
| 25 | + echo " -v | --version <version>" |
| 26 | + echo " -d | --dry-run print the commands without executing them" |
| 27 | + echo " -u | --update-only only update the version" |
| 28 | + echo " -h | --help print this information and exit" |
| 29 | + echo |
| 30 | + echo "For example: $0 -v 1.2.3" |
| 31 | +} |
| 32 | + |
| 33 | +function process_arguments |
| 34 | +{ |
| 35 | + while [[ "$1" != "" ]]; do |
| 36 | + case $1 in |
| 37 | + -v | --version ) |
| 38 | + shift |
| 39 | + NEW_VERSION=${1:-} |
| 40 | + if ! [[ "${NEW_VERSION}" =~ [0-9]+\.[0-9]+\.[0-9]+(\-.+)? ]]; then |
| 41 | + echo_err "You must supply a new version after -v or --version" |
| 42 | + echo_err "For example:" |
| 43 | + echo_err " 1.2.3" |
| 44 | + echo_err " 1.2.3-rc1" |
| 45 | + echo_err "" |
| 46 | + usage; return 1 |
| 47 | + fi |
| 48 | + ;; |
| 49 | + -d | --dry-run ) |
| 50 | + CMD_PREFIX=echo |
| 51 | + echo "Dry Run" |
| 52 | + echo "" |
| 53 | + ;; |
| 54 | + -u | --update-only ) |
| 55 | + UPDATE_ONLY=true |
| 56 | + echo "Only update version" |
| 57 | + echo "" |
| 58 | + ;; |
| 59 | + -h | --help ) |
| 60 | + usage; return 0 |
| 61 | + ;; |
| 62 | + * ) |
| 63 | + usage; return 1 |
| 64 | + esac |
| 65 | + shift || true |
| 66 | + done |
| 67 | +} |
| 68 | + |
| 69 | +# Intentionally make pushd silent |
| 70 | +function pushd |
| 71 | +{ |
| 72 | + command pushd "$@" > /dev/null |
| 73 | +} |
| 74 | + |
| 75 | +# Intentionally make popd silent |
| 76 | +function popd |
| 77 | +{ |
| 78 | + command popd > /dev/null |
| 79 | +} |
| 80 | + |
| 81 | +# Check if one version is less than or equal than other |
| 82 | +# Example: |
| 83 | +# ver_lte 1.2.3 1.2.3 && echo "yes" || echo "no" # yes |
| 84 | +# ver_lte 1.2.3 1.2.4 && echo "yes" || echo "no" # yes |
| 85 | +# ver_lte 1.2.4 1.2.3 && echo "yes" || echo "no" # no |
| 86 | +function ver_lte |
| 87 | +{ |
| 88 | + [[ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]] |
| 89 | +} |
| 90 | + |
| 91 | +# Extract the last entry or entry for a given version |
| 92 | +# The function is not currently used in this file. |
| 93 | +# Examples: |
| 94 | +# changelog_last_entry |
| 95 | +# changelog_last_entry 1.10.0 |
| 96 | +# |
| 97 | +function changelog_last_entry |
| 98 | +{ |
| 99 | + sed -e "1,/^${1}/d" -e '/^=/d' -e '/^$/d' -e '/^[0-9]/,$d' CHANGELOG.md |
| 100 | +} |
| 101 | + |
| 102 | +function verify_dependencies |
| 103 | +{ |
| 104 | + # Test if the gnu grep is installed |
| 105 | + if ! grep --version | grep -q GNU |
| 106 | + then |
| 107 | + echo_err "GNU grep is required for this script" |
| 108 | + echo_err "You can install it using the following command:" |
| 109 | + echo_err "" |
| 110 | + echo_err "brew install grep --with-default-names" |
| 111 | + return 1 |
| 112 | + fi |
| 113 | + |
| 114 | + if [[ "${UPDATE_ONLY}" = true ]]; then |
| 115 | + return 0; |
| 116 | + fi |
| 117 | + |
| 118 | + if [[ -z "$(type -t git-changelog)" ]] |
| 119 | + then |
| 120 | + echo_err "git-extras packages is not installed." |
| 121 | + echo_err "You can install it using the following command:" |
| 122 | + echo_err "" |
| 123 | + echo_err "brew install git-extras" |
| 124 | + return 1 |
| 125 | + fi |
| 126 | +} |
| 127 | + |
| 128 | +# Replace old string only if it is present in the file, otherwise return 1 |
| 129 | +function safe_replace |
| 130 | +{ |
| 131 | + local old=$1 |
| 132 | + local new=$2 |
| 133 | + local file=$3 |
| 134 | + |
| 135 | + grep -q "${old}" "${file}" || { echo_err "${old} was not found in ${file}"; return 1; } |
| 136 | + |
| 137 | + ${CMD_PREFIX} sed -i.bak -e "${QUOTE}s/${old}/${new}/${QUOTE}" -- "${file}" && rm -- "${file}.bak" |
| 138 | +} |
| 139 | + |
| 140 | +function update_version |
| 141 | +{ |
| 142 | + if [[ -z "${NEW_VERSION}" ]]; then |
| 143 | + usage; return 1 |
| 144 | + fi |
| 145 | + |
| 146 | + # Enter git root |
| 147 | + pushd "$(git rev-parse --show-toplevel)" |
| 148 | + |
| 149 | + local current_version |
| 150 | + current_version=$(grep -oiP '(?<=__version__ \= \")([a-zA-Z0-9\-.]+)(?=")' cloudinary_cli/version.py) |
| 151 | + |
| 152 | + if [[ -z "${current_version}" ]]; then |
| 153 | + echo_err "Failed getting current version, please check directory structure and/or contact developer" |
| 154 | + return 1 |
| 155 | + fi |
| 156 | + |
| 157 | + # Use literal dot character in regular expression |
| 158 | + local current_version_re=${current_version//./\\.} |
| 159 | + |
| 160 | + echo "# Current version is: ${current_version}" |
| 161 | + echo "# New version is: ${NEW_VERSION}" |
| 162 | + |
| 163 | + ver_lte "${NEW_VERSION}" "${current_version}" && { echo_err "New version is not greater than current version"; return 1; } |
| 164 | + |
| 165 | + # Add a quote if this is a dry run |
| 166 | + QUOTE=${CMD_PREFIX:+"'"} |
| 167 | + |
| 168 | + safe_replace "__version__ = \"${current_version_re}\""\ |
| 169 | + "__version__ = \"${NEW_VERSION}\""\ |
| 170 | + cloudinary_cli/version.py\ |
| 171 | + || return 1 |
| 172 | + |
| 173 | + |
| 174 | + if [[ "${UPDATE_ONLY}" = true ]]; then |
| 175 | + popd; |
| 176 | + return 0; |
| 177 | + fi |
| 178 | + |
| 179 | + ${CMD_PREFIX} git changelog -t ${NEW_VERSION} || true |
| 180 | + |
| 181 | + echo "" |
| 182 | + echo "# After editing CHANGELOG.md, optionally review changes and issue these commands:" |
| 183 | + echo git add cloudinary_cli/version.py CHANGELOG.md |
| 184 | + echo git commit -m "\"Version ${NEW_VERSION}\"" |
| 185 | + echo sed -e "'1,/^${NEW_VERSION//./\\.}/d'" \ |
| 186 | + -e "'/^=/d'" \ |
| 187 | + -e "'/^$/d'" \ |
| 188 | + -e "'/^[0-9]/,\$d'" \ |
| 189 | + CHANGELOG.md \ |
| 190 | + \| git tag -a "'${NEW_VERSION}'" --file=- |
| 191 | + |
| 192 | + # Don't run those commands on dry run |
| 193 | + [[ -n "${CMD_PREFIX}" ]] && { popd; return 0; } |
| 194 | + |
| 195 | + echo "" |
| 196 | + read -p "Run the above commands automatically? (y/N): " confirm && [[ ${confirm} == [yY] || ${confirm} == [yY][eE][sS] ]] || { popd; return 0; } |
| 197 | + |
| 198 | + git add cloudinary_cli/version.py CHANGELOG.md |
| 199 | + git commit -m "Version ${NEW_VERSION}" |
| 200 | + sed -e "1,/^${NEW_VERSION//./\\.}/d" \ |
| 201 | + -e "/^=/d" \ |
| 202 | + -e "/^$/d" \ |
| 203 | + -e "/^[0-9]/,\$d" \ |
| 204 | + CHANGELOG.md \ |
| 205 | + | git tag -a "${NEW_VERSION}" --file=- |
| 206 | + |
| 207 | + popd |
| 208 | +} |
| 209 | + |
| 210 | +process_arguments "$@" |
| 211 | +verify_dependencies |
| 212 | +update_version |
0 commit comments