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