1#!/bin/sh 2 3set -efu 4 5# the list of files that need to have the version updated in them 6# 7# limitations: 8# * no newlines in names 9# * no ' char in names 10files=" 11README.md 12kotlinx-coroutines-core/README.md 13kotlinx-coroutines-debug/README.md 14kotlinx-coroutines-test/README.md 15ui/coroutines-guide-ui.md 16gradle.properties 17integration-testing/gradle.properties 18" 19 20# read gradle.properties to get the old version 21set +e 22old_version="$(git grep -hoP '(?<=^version=).*(?=-SNAPSHOT$)' gradle.properties)" 23set -e 24if [ "$?" -ne 0 ] 25 then 26 echo "Could not read the old version from gradle.properties." >&2 27 if [ "$#" -ne 2 ] 28 then 29 echo "Please use this form instead: ./bump-version.sh old_version new_version" 30 exit 1 31 fi 32fi 33 34# check the command-line arguments for mentions of the version 35if [ "$#" -eq 2 ] 36 then 37 echo "If you want to infer the version automatically, use the form: ./bump-version.sh new_version" >&2 38 if [ -n "$old_version" -a "$1" != "$old_version" ] 39 then 40 echo "The provided old version ($1) is different from the one in gradle.properties ($old_version)." >&2 41 echo "Proceeding anyway with the provided old version." >&2 42 fi 43 old_version=$1 44 new_version=$2 45 elif [ "$#" -eq 1 ] 46 then 47 new_version=$1 48 else 49 echo "Use: ./bump-version.sh new_version" >&2 50 exit 1 51fi 52 53 54# Escape dots, e.g. 1.0.0 -> 1\.0\.0 55escaped_old_version="$(printf "%s\n" "$old_version" | sed 's/[.]/\\./g')" 56 57update_version() { 58 file=$1 59 to_undo=$2 60 echo "Updating version from '$old_version' to '$new_version' in $1" >&2 61 if [ -n "$(git diff --name-status -- "$file")" ] 62 then 63 printf "There are unstaged changes in '$file'. Refusing to proceed.\n" >&2 64 [ -z "$to_undo" ] || eval "git checkout$to_undo" 65 exit 1 66 fi 67 sed -i.bak "s/$escaped_old_version/$new_version/g" "$file" 68 rm -f "$1.bak" 69} 70 71to_undo=$(printf "%s" "$files" | while read -r file; do 72 if [ -n "$file" ] 73 then 74 update_version "$file" "${to_undo:-}" 75 to_undo="${to_undo:-} '$file'" 76 echo -n " '$file'" 77 fi 78done) 79 80set +e 81version_mentions=$( 82 find . -type f \( -iname '*.properties' -o -iname '*.md' \) \ 83 -not -iname CHANGES.md -not -iname CHANGES_UP_TO_1.7.md \ 84 -not -path ./integration/kotlinx-coroutines-jdk8/README.md \ 85 -exec git grep --fixed-strings --word "$old_version" {} + 86 ) 87set -e 88if [ -z "$version_mentions" ] 89then 90 echo "Done. To undo, run this command:" >&2 91 printf "git checkout%s\n" "$to_undo" >&2 92else 93 echo "ERROR: Previous version is present in the project: $version_mentions" 94 [ -z "$to_undo" ] || eval "git checkout$to_undo" 95 exit 1 96fi 97