release.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/usr/bin/env bash
  2. set -e -o pipefail
  3. shopt -s extglob
  4. VERSION=
  5. NEXT_VERSION=
  6. BRANCH=$(git rev-parse --abbrev-ref HEAD)
  7. ORIGIN=${ORIGIN:-origin}
  8. NARGS=0
  9. DRY_RUN=
  10. PUBLISH=
  11. INSANE=0
  12. # usage [ERROR-MESSAGES...] EXIT-CODE
  13. usage() {
  14. while [[ $# -gt 1 ]]; do
  15. echo "$1" >&2
  16. shift
  17. done
  18. echo "Usage:"
  19. echo "./release.sh [OPTIONS] <VERSION_TO_RELEASE> [NEXT_VERSION]"
  20. echo ""
  21. echo "Options:"
  22. echo " --publish|-p: publish a release."
  23. echo " --dry-run|-n: only print commands, do not execute them."
  24. echo ""
  25. echo "You may omit NEXT_VERSION in order to avoid updating the version field"
  26. echo "of the package.json."
  27. echo ""
  28. echo "Run this on the master branch. This will create a release branch."
  29. echo "Open a pull request and after it gets merged, run with --publish"
  30. echo "option on the master branch."
  31. echo ""
  32. echo "To update SRI hashes, run this again on the generated release branch"
  33. echo ""
  34. echo "Examples:"
  35. echo " When releasing a v0.6.3:"
  36. echo " ./release.sh 0.6.3 0.6.4"
  37. echo " Open a pull request from v0.6.3-release to master."
  38. echo " After it's merged:"
  39. echo " ./release.sh -p 0.6.3"
  40. exit $1
  41. }
  42. while [ $# -gt 0 ]; do
  43. case "$1" in
  44. --dry-run|-n|--just-print)
  45. DRY_RUN=true
  46. git() { echo "git $*"; }
  47. npm() { echo "npm $*"; }
  48. yarn() { echo "yarn $*"; }
  49. ;;
  50. --publish|-p)
  51. PUBLISH=true
  52. ;;
  53. -h|-\?|--help)
  54. usage 0
  55. ;;
  56. -*)
  57. usage "Unknown option: $1" "" 1
  58. ;;
  59. *)
  60. case "$NARGS" in
  61. 0)
  62. VERSION="$1"
  63. NARGS=1
  64. ;;
  65. 1)
  66. NEXT_VERSION="$1"
  67. NARGS=2
  68. ;;
  69. *)
  70. usage "Too many arguments: $1" "" 1
  71. ;;
  72. esac
  73. ;;
  74. esac
  75. shift
  76. done
  77. if [[ $NARGS = 0 ]]; then
  78. usage "Missing argument: version number" "" 1
  79. fi
  80. # Some sanity checks up front
  81. if ! command git diff --stat --exit-code HEAD; then
  82. echo "Please make sure you have no uncommitted changes" >&2
  83. : $((++INSANE))
  84. fi
  85. if [[ $BRANCH != @(v*-release|master) ]]; then
  86. echo "'$BRANCH' does not look like a release branch to me" >&2
  87. : $((++INSANE))
  88. fi
  89. if [[ $PUBLISH ]]; then
  90. echo "About to publish $VERSION from $BRANCH. "
  91. elif [[ $BRANCH == @(v*-release) ]]; then
  92. echo "About to update SRI hashes for $BRANCH. "
  93. elif [[ ! $NEXT_VERSION ]]; then
  94. echo "About to release $VERSION from $BRANCH. "
  95. else
  96. echo "About to release $VERSION from $BRANCH and bump to $NEXT_VERSION-pre."
  97. fi
  98. if [[ $INSANE != 0 ]]; then
  99. read -r -p "$INSANE sanity check(s) failed, really proceed? [y/n] " CONFIRM
  100. else
  101. read -r -p "Look good? [y/n] " CONFIRM
  102. fi
  103. if [[ $CONFIRM != "y" ]]; then
  104. exit 1
  105. fi
  106. git checkout "$BRANCH"
  107. git pull
  108. if [[ ! $PUBLISH ]]; then
  109. # Make a release branch
  110. git checkout -B "v$VERSION-release"
  111. # Edit package.json to the right version, as it's inlined (see
  112. # http://stackoverflow.com/a/22084103 for why we need the .bak file to make
  113. # this mac & linux compatible)
  114. sed -i.bak -E 's|"version": "[^"]+",|"version": "'$VERSION'",|' package.json
  115. rm -f package.json.bak
  116. # Build generated files
  117. yarn build
  118. if [[ $BRANCH != @(v*-release) ]]; then
  119. if [ ! -z "$NEXT_VERSION" ]; then
  120. # Edit package.json to the next version
  121. sed -i.bak -E 's|"version": "[^"]+",|"version": "'$NEXT_VERSION'-pre",|' package.json
  122. rm -f package.json.bak
  123. fi
  124. # Edit docs to use CSS from CDN
  125. grep -l '/static/' docs/*.md | xargs sed -i.bak \
  126. 's|/static/\([^"]\+\)|https://cdn.jsdelivr.net/npm/katex@./dist/\1" integrity="sha384-\1" crossorigin="anonymous|'
  127. # Update the version number in CDN URLs included in the README and the documentation,
  128. # and regenerate the Subresource Integrity hash for these files.
  129. node update-sri.js "$VERSION" README.md contrib/*/README.md \
  130. docs/*.md docs/*.md.bak website/pages/index.html
  131. # Generate a new version of the docs
  132. pushd website
  133. yarn run version "$VERSION"
  134. popd
  135. # Restore docs to use local built CSS
  136. for file in docs/*.md.bak; do
  137. mv -f "$file" "${file%.bak}"
  138. done
  139. else
  140. # Restore package.json
  141. git checkout package.json
  142. # Regenerate the Subresource Integrity hash in the README and the documentation
  143. node update-sri.js "$VERSION" README.md contrib/*/README.md \
  144. docs/*.md website/pages/index.html website/versioned_docs/version-$VERSION/*.md
  145. fi
  146. # Make the commit and push
  147. git add package.json README.md contrib/*/README.md \
  148. docs website/pages/index.html website/versioned_docs/ \
  149. website/versioned_sidebars/ website/versions.json
  150. if [[ $BRANCH == @(v*-release) ]]; then
  151. git commit -n -m "Update SRI hashes"
  152. elif [[ ! $NEXT_VERSION ]]; then
  153. git commit -n -m "Release v$VERSION"
  154. else
  155. git commit -n -m "Release v$VERSION" -m "Bump $BRANCH to v$NEXT_VERSION-pre"
  156. fi
  157. git push -u "$ORIGIN" "v$VERSION-release"
  158. echo ""
  159. echo "The automatic parts are done!"
  160. echo "Now create a pull request against master from 'v$VERSION-release'"
  161. echo "Visit https://github.com/KaTeX/KaTeX/pulls to open a pull request."
  162. echo "After it gets merged, run './release.sh -p $VERSION'!"
  163. echo "Note that if KaTeX source code is changed after running this script,"
  164. echo "you have to run the release script again."
  165. else
  166. # Make a new detached HEAD
  167. git checkout --detach
  168. # Edit package.json to the right version
  169. sed -i.bak -E 's|"version": "[^"]+",|"version": "'$VERSION'",|' package.json
  170. rm -f package.json.bak
  171. # Build generated files and add them to the repository
  172. git clean -fdx dist
  173. yarn dist
  174. sed -i.bak -E '/^\/dist\/$/d' .gitignore
  175. rm -f .gitignore.bak
  176. # Check Subresource Integrity hashes
  177. node update-sri.js check README.md contrib/*/README.md
  178. # Make the commit and tag, and push them.
  179. git add package.json .gitignore dist/
  180. git commit -n -m "v$VERSION"
  181. git diff --stat --exit-code # check for uncommitted changes
  182. git tag -a "v$VERSION" -m "v$VERSION"
  183. git push "$ORIGIN" "v$VERSION"
  184. # Update npm (cdnjs update automatically)
  185. # Fallback to npm publish, if yarn cannot authenticate, e.g., 2FA
  186. yarn publish --new-version "$VERSION" || npm publish
  187. echo ""
  188. echo "The automatic parts are done!"
  189. echo "Now all that's left is to create the release on GitHub."
  190. echo "Visit https://github.com/KaTeX/KaTeX/releases/new?tag=v$VERSION to edit the release notes."
  191. echo "Don't forget to upload katex.tar.gz and katex.zip to the release!"
  192. fi
  193. git diff --stat --exit-code # check for uncommitted changes
  194. if [[ $DRY_RUN ]]; then
  195. echo ""
  196. echo "This was a dry run."
  197. echo "Operations using git or yarn were printed not executed."
  198. echo "Some files got modified, though, so you might want to undo "
  199. echo "these changes now, e.g. using \`git checkout -- .\` or similar."
  200. echo ""
  201. fi