bump_version.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/bin/bash -e
  2. prompt_for() {
  3. local prompt_text=$1
  4. local pattern=$2
  5. local default_value=$3
  6. local tmp=
  7. while true; do
  8. read -p "$prompt_text [$default_value]: " tmp
  9. if [ -z "$tmp" ]; then
  10. echo "$default_value"; return
  11. elif echo "$tmp" | grep -qE "^(${pattern})\$"; then
  12. echo "$tmp"; return
  13. fi
  14. done
  15. }
  16. # Reads current versions
  17. # out: VERSION_MAJOR VERSION_MINOR VERSION_PATCH VERSION_IS_DEV CURRENT_VERSION ANDROID_VERSION_CODE
  18. read_versions() {
  19. VERSION_MAJOR=$(grep -oE '^set\(VERSION_MAJOR [0-9]+\)$' CMakeLists.txt | tr -dC 0-9)
  20. VERSION_MINOR=$(grep -oE '^set\(VERSION_MINOR [0-9]+\)$' CMakeLists.txt | tr -dC 0-9)
  21. VERSION_PATCH=$(grep -oE '^set\(VERSION_PATCH [0-9]+\)$' CMakeLists.txt | tr -dC 0-9)
  22. VERSION_IS_DEV=$(grep -oE '^set\(DEVELOPMENT_BUILD [A-Z]+\)$' CMakeLists.txt)
  23. ANDROID_VERSION_CODE=$(grep -oE '\("versionCode", [0-9]+\)' android/build.gradle | tr -dC 0-9)
  24. # Make sure they all exist
  25. [ -n "$VERSION_MAJOR" ]
  26. [ -n "$VERSION_MINOR" ]
  27. [ -n "$VERSION_PATCH" ]
  28. [ -n "$VERSION_IS_DEV" ]
  29. [ -n "$ANDROID_VERSION_CODE" ]
  30. if echo "$VERSION_IS_DEV" | grep -q ' TRUE'; then
  31. VERSION_IS_DEV=1
  32. else
  33. VERSION_IS_DEV=0
  34. fi
  35. CURRENT_VERSION="$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH"
  36. echo "Current Minetest version: $CURRENT_VERSION"
  37. echo "Current Android version code: $ANDROID_VERSION_CODE"
  38. }
  39. # Retrieves protocol version from header
  40. # in: $1
  41. read_proto_ver() {
  42. local ref=$1
  43. git show "$ref":src/network/networkprotocol.h | grep -oE 'LATEST_PROTOCOL_VERSION [0-9]+' | tr -dC 0-9
  44. }
  45. ## Prompts for new android version code
  46. # in: ANDROID_VERSION_CODE
  47. # out: NEW_ANDROID_VERSION_CODE
  48. bump_android_ver() {
  49. # +1 for ARM and +1 for ARM64 APKs
  50. NEW_ANDROID_VERSION_CODE=$(expr $ANDROID_VERSION_CODE + 2)
  51. NEW_ANDROID_VERSION_CODE=$(prompt_for "Set android version code" '[0-9]+' $NEW_ANDROID_VERSION_CODE)
  52. echo
  53. echo "New android version code: $NEW_ANDROID_VERSION_CODE"
  54. }
  55. ## Prompts for new version
  56. # in: VERSION_{MAJOR,MINOR,PATCH} DO_PATCH_REL
  57. # out: NEXT_VERSION NEXT_VERSION_{MAJOR,MINOR,PATCH}
  58. bump_version() {
  59. NEXT_VERSION_MAJOR=$VERSION_MAJOR
  60. if [ "$DO_PATCH_REL" -eq 1 ]; then
  61. NEXT_VERSION_MINOR=$VERSION_MINOR
  62. NEXT_VERSION_PATCH=$(expr $VERSION_PATCH + 1)
  63. else
  64. NEXT_VERSION_MINOR=$(expr $VERSION_MINOR + 1)
  65. NEXT_VERSION_PATCH=0
  66. fi
  67. NEXT_VERSION_MAJOR=$(prompt_for "Set next major" '[0-9]+' $NEXT_VERSION_MAJOR)
  68. if [ "$NEXT_VERSION_MAJOR" != "$VERSION_MAJOR" ]; then
  69. NEXT_VERSION_MINOR=0
  70. NEXT_VERSION_PATCH=0
  71. fi
  72. NEXT_VERSION_MINOR=$(prompt_for "Set next minor" '[0-9]+' $NEXT_VERSION_MINOR)
  73. if [ "$NEXT_VERSION_MINOR" != "$VERSION_MINOR" ]; then
  74. NEXT_VERSION_PATCH=0
  75. fi
  76. NEXT_VERSION_PATCH=$(prompt_for "Set next patch" '[0-9]+' $NEXT_VERSION_PATCH)
  77. NEXT_VERSION="$NEXT_VERSION_MAJOR.$NEXT_VERSION_MINOR.$NEXT_VERSION_PATCH"
  78. echo
  79. echo "New version: $NEXT_VERSION"
  80. }
  81. ## Toggles development build
  82. # in: $1
  83. set_dev_build() {
  84. local is_dev=$1
  85. # Update CMakeList.txt versions
  86. if [ "$is_dev" -eq 1 ]; then
  87. sed -i -re 's/^set\(DEVELOPMENT_BUILD [A-Z]+\)$/set(DEVELOPMENT_BUILD TRUE)/' CMakeLists.txt
  88. else
  89. sed -i -re 's/^set\(DEVELOPMENT_BUILD [A-Z]+\)$/set(DEVELOPMENT_BUILD FALSE)/' CMakeLists.txt
  90. fi
  91. git add -f CMakeLists.txt android/build.gradle
  92. }
  93. ## Writes new android version code
  94. # in: NEW_ANDROID_VERSION_CODE
  95. write_android_version() {
  96. sed -i -re "s/\"versionCode\", [0-9]+/\"versionCode\", $NEW_ANDROID_VERSION_CODE/" android/build.gradle
  97. git add -f android/build.gradle
  98. }
  99. ## Writes new version to the right files
  100. # in: NEXT_VERSION NEXT_VERSION_{MAJOR,MINOR,PATCH}
  101. write_new_version() {
  102. # Update CMakeList.txt versions
  103. sed -i -re "s/^set\(VERSION_MAJOR [0-9]+\)$/set(VERSION_MAJOR $NEXT_VERSION_MAJOR)/" CMakeLists.txt
  104. sed -i -re "s/^set\(VERSION_MINOR [0-9]+\)$/set(VERSION_MINOR $NEXT_VERSION_MINOR)/" CMakeLists.txt
  105. sed -i -re "s/^set\(VERSION_PATCH [0-9]+\)$/set(VERSION_PATCH $NEXT_VERSION_PATCH)/" CMakeLists.txt
  106. # Update Android versions
  107. sed -i -re "s/set\(\"versionMajor\", [0-9]+\)/set(\"versionMajor\", $NEXT_VERSION_MAJOR)/" android/build.gradle
  108. sed -i -re "s/set\(\"versionMinor\", [0-9]+\)/set(\"versionMinor\", $NEXT_VERSION_MINOR)/" android/build.gradle
  109. sed -i -re "s/set\(\"versionPatch\", [0-9]+\)/set(\"versionPatch\", $NEXT_VERSION_PATCH)/" android/build.gradle
  110. # Update doc versions
  111. sed -i -re '1s/[0-9]+\.[0-9]+\.[0-9]+/'"$NEXT_VERSION"'/g' doc/menu_lua_api.md
  112. sed -i -re '1s/[0-9]+\.[0-9]+\.[0-9]+/'"$NEXT_VERSION"'/g' doc/client_lua_api.md
  113. git add -f CMakeLists.txt android/build.gradle doc/menu_lua_api.md doc/client_lua_api.md
  114. }
  115. ## Create release commit and tag
  116. # in: $1
  117. perform_release() {
  118. local release_version=$1
  119. RELEASE_DATE=$(date +%Y-%m-%d)
  120. sed -i '/\<release/s/\(version\)="[^"]*"/\1="'"$release_version"'"/' misc/net.minetest.minetest.appdata.xml
  121. sed -i 's/\(<release date\)="[^"]*"/\1="'"$RELEASE_DATE"'"/' misc/net.minetest.minetest.appdata.xml
  122. git add -f misc/net.minetest.minetest.appdata.xml
  123. git commit -m "Bump version to $release_version"
  124. echo "Tagging $release_version"
  125. git tag -a "$release_version" -m "$release_version"
  126. }
  127. ## Create after-release commit
  128. # in: NEXT_VERSION
  129. back_to_devel() {
  130. echo 'Creating "return back to development" commit'
  131. git commit -m "Continue with $NEXT_VERSION-dev"
  132. }
  133. #######################
  134. # Start of main logic:
  135. #######################
  136. # Switch to top minetest directory
  137. cd ${0%/*}/..
  138. # Determine old versions
  139. read_versions
  140. # Double-check what we're doing
  141. if [ "$VERSION_IS_DEV" -eq 1 ]; then
  142. echo "You are on the development branch and about to make a major or minor release."
  143. DO_PATCH_REL=0
  144. else
  145. echo "You are on the stable/backport branch and about to make a patch release."
  146. DO_PATCH_REL=1
  147. fi
  148. if [[ "$(prompt_for "Is this correct?" '[Yy][Ee][Ss]|[Nn][Oo]|' no)" != [Yy][Ee][Ss] ]]; then
  149. echo "Aborting"
  150. exit 1
  151. fi
  152. if [ "$DO_PATCH_REL" -eq 0 ]; then
  153. # On a regular release the version moves from 5.7.0-dev -> 5.7.0 (new tag) -> 5.8.0-dev
  154. old_proto=$(read_proto_ver origin/stable-5)
  155. new_proto=$(read_proto_ver HEAD)
  156. [ -n "$old_proto" ]
  157. [ -n "$new_proto" ]
  158. echo "Protocol versions: $old_proto (last release) -> $new_proto (now)"
  159. if [ "$new_proto" -le "$old_proto" ]; then
  160. echo "The protocol version has not been increased since last release, refusing to continue."
  161. exit 1
  162. fi
  163. bump_android_ver
  164. write_android_version
  165. set_dev_build 0
  166. perform_release "$CURRENT_VERSION"
  167. bump_version
  168. set_dev_build 1
  169. write_new_version
  170. back_to_devel
  171. else
  172. # On a patch release the version moves from 5.7.0 -> 5.7.1 (new tag)
  173. bump_android_ver
  174. write_android_version
  175. bump_version
  176. write_new_version
  177. perform_release "$NEXT_VERSION"
  178. fi