|
1 |
| -#!/bin/bash |
| 1 | +#!/bin/sh |
2 | 2 |
|
3 |
| -if [ "$#" -ne 2 ] |
| 3 | +set -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 |
| 10 | +files=" |
| 11 | +README.md |
| 12 | +kotlinx-coroutines-core/README.md |
| 13 | +kotlinx-coroutines-debug/README.md |
| 14 | +kotlinx-coroutines-test/README.md |
| 15 | +ui/coroutines-guide-ui.md |
| 16 | +gradle.properties |
| 17 | +integration-testing/gradle.properties |
| 18 | +" |
| 19 | + |
| 20 | +# read gradle.properties to get the old version |
| 21 | +set +e |
| 22 | +old_version="$(git grep -hoP '(?<=^version=).*(?=-SNAPSHOT$)' gradle.properties)" |
| 23 | +set -e |
| 24 | +if [ "$?" -ne 0 ] |
4 | 25 | then
|
5 |
| - echo "Use: ./bump-version old_version new_version" |
6 |
| - exit |
| 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 |
7 | 32 | fi
|
8 | 33 |
|
9 |
| -old_version=$1 |
10 |
| -new_version=$2 |
| 34 | +# check the command-line arguments for mentions of the version |
| 35 | +if [ "$#" -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 |
| 51 | +fi |
| 52 | + |
| 53 | + |
| 54 | +# Escape dots, e.g. 1.0.0 -> 1\.0\.0 |
| 55 | +escaped_old_version="$(printf "%s\n" "$old_version" | sed 's/[.]/\\./g')" |
11 | 56 |
|
12 | 57 | update_version() {
|
13 |
| - echo "Updating version from '$old_version' to '$new_version' in $1" |
14 |
| - sed -i.bak s/$old_version/$new_version/g $1 |
15 |
| - rm $1.bak |
| 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" |
16 | 69 | }
|
17 | 70 |
|
18 |
| -update_version "README.md" |
19 |
| -update_version "kotlinx-coroutines-core/README.md" |
20 |
| -update_version "kotlinx-coroutines-debug/README.md" |
21 |
| -update_version "kotlinx-coroutines-test/README.md" |
22 |
| -update_version "ui/coroutines-guide-ui.md" |
23 |
| -update_version "gradle.properties" |
24 |
| -update_version "integration-test/gradle.properties" |
| 71 | +to_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 |
| 78 | +done) |
25 | 79 |
|
26 |
| -# Escape dots, e.g. 1.0.0 -> 1\.0\.0 |
27 |
| -escaped_old_version=$(echo $old_version | sed s/[.]/\\\\./g) |
28 |
| -result=$(find ./ -type f \( -iname \*.properties -o -iname \*.md \) | grep -v "\.gradle" | grep -v "build" | xargs -I{} grep -H "$escaped_old_version" {} | grep -v CHANGES.md | grep -v COMPATIBILITY.md) |
29 |
| -if [ -z "$result" ]; |
| 80 | +set +e |
| 81 | +version_mentions=$( |
| 82 | + find . -type f \( -iname '*.properties' -o -iname '*.md' \) \ |
| 83 | + -not -iname CHANGES.md \ |
| 84 | + -exec git grep --fixed-strings --word "$old_version" {} + |
| 85 | + ) |
| 86 | +set -e |
| 87 | +if [ -z "$version_mentions" ] |
30 | 88 | then
|
31 |
| - echo "Done" |
| 89 | + echo "Done. To undo, run this command:" >&2 |
| 90 | + printf "git checkout%s\n" "$to_undo" >&2 |
32 | 91 | else
|
33 |
| - echo "ERROR: Previous version is present in the project: $result" |
34 |
| - exit -1 |
| 92 | + echo "ERROR: Previous version is present in the project: $version_mentions" |
| 93 | + [ -z "$to_undo" ] || eval "git checkout$to_undo" |
| 94 | + exit 1 |
35 | 95 | fi
|
0 commit comments