increment-cargo-version.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env bash
  2. set -e
  3. usage() {
  4. cat <<EOF
  5. usage: $0 [major|minor|patch|-preXYZ]
  6. Increments the Cargo.toml version.
  7. Default:
  8. * Removes the prerelease tag if present, otherwise the minor version is incremented.
  9. EOF
  10. exit 0
  11. }
  12. here="$(dirname "$0")"
  13. cd "$here"/..
  14. source ci/semver_bash/semver.sh
  15. source scripts/read-cargo-variable.sh
  16. ignores=(
  17. .cache
  18. .cargo
  19. target
  20. node_modules
  21. )
  22. not_paths=()
  23. for ignore in "${ignores[@]}"; do
  24. not_paths+=(-not -path "*/$ignore/*")
  25. done
  26. # shellcheck disable=2207
  27. Cargo_tomls=($(find . -name Cargo.toml "${not_paths[@]}"))
  28. # Collect the name of all the internal crates
  29. crates=()
  30. for Cargo_toml in "${Cargo_tomls[@]}"; do
  31. crates+=("$(readCargoVariable name "$Cargo_toml")")
  32. done
  33. # Read the current version
  34. MAJOR=0
  35. MINOR=0
  36. PATCH=0
  37. SPECIAL=""
  38. semverParseInto "$(readCargoVariable version Cargo.toml)" MAJOR MINOR PATCH SPECIAL
  39. [[ -n $MAJOR ]] || usage
  40. currentVersion="$MAJOR\.$MINOR\.$PATCH$SPECIAL"
  41. bump=$1
  42. if [[ -z $bump ]]; then
  43. if [[ -n $SPECIAL ]]; then
  44. bump=dropspecial # Remove prerelease tag
  45. else
  46. bump=minor
  47. fi
  48. fi
  49. SPECIAL=""
  50. # Figure out what to increment
  51. case $bump in
  52. patch)
  53. PATCH=$((PATCH + 1))
  54. ;;
  55. major)
  56. MAJOR=$((MAJOR+ 1))
  57. MINOR=0
  58. PATCH=0
  59. ;;
  60. minor)
  61. MINOR=$((MINOR+ 1))
  62. PATCH=0
  63. ;;
  64. dropspecial)
  65. ;;
  66. check)
  67. badTomls=()
  68. for Cargo_toml in "${Cargo_tomls[@]}"; do
  69. if grep "^version = { workspace = true }" "$Cargo_toml" &>/dev/null; then
  70. continue
  71. fi
  72. if ! grep "^version *= *\"$currentVersion\"$" "$Cargo_toml" &>/dev/null; then
  73. badTomls+=("$Cargo_toml")
  74. fi
  75. done
  76. if [[ ${#badTomls[@]} -ne 0 ]]; then
  77. echo "Error: Incorrect crate version specified in: ${badTomls[*]}"
  78. exit 1
  79. fi
  80. exit 0
  81. ;;
  82. -*)
  83. if [[ $1 =~ ^-[A-Za-z0-9]*$ ]]; then
  84. SPECIAL="$1"
  85. else
  86. echo "Error: Unsupported characters found in $1"
  87. exit 1
  88. fi
  89. ;;
  90. *)
  91. echo "Error: unknown argument: $1"
  92. usage
  93. ;;
  94. esac
  95. # Version bumps should occur in their own commit. Disallow bumping version
  96. # in dirty working trees. Gate after arg parsing to prevent breaking the
  97. # `check` subcommand.
  98. (
  99. set +e
  100. if ! git diff --exit-code; then
  101. echo -e "\nError: Working tree is dirty. Commit or discard changes before bumping version." 1>&2
  102. exit 1
  103. fi
  104. )
  105. newVersion="$MAJOR.$MINOR.$PATCH$SPECIAL"
  106. # Update all the Cargo.toml files
  107. for Cargo_toml in "${Cargo_tomls[@]}"; do
  108. if ! grep "$currentVersion" "$Cargo_toml"; then
  109. echo "$Cargo_toml (skipped)"
  110. continue
  111. fi
  112. # Set new crate version
  113. (
  114. set -x
  115. sed -i "$Cargo_toml" -e "s/^version = \"$currentVersion\"$/version = \"$newVersion\"/"
  116. )
  117. # Fix up the version references to other internal crates
  118. for crate in "${crates[@]}"; do
  119. (
  120. set -x
  121. sed -i "$Cargo_toml" -e "
  122. s/^$crate = { *path *= *\"\([^\"]*\)\" *, *version *= *\"[^\"]*\"\(.*\)} *\$/$crate = \{ path = \"\1\", version = \"=$newVersion\"\2\}/
  123. "
  124. )
  125. done
  126. done
  127. # Update cargo lock files
  128. scripts/cargo-for-all-lock-files.sh tree >/dev/null
  129. echo "$currentVersion -> $newVersion"
  130. exit 0