release.sh (4903B)
1 #!/usr/bin/env bash 2 3 set -e -o pipefail 4 shopt -s extglob 5 6 VERSION= 7 NEXT_VERSION= 8 BRANCH=$(git rev-parse --abbrev-ref HEAD) 9 NARGS=0 10 DRY_RUN= 11 INSANE=0 12 13 # usage [ERROR-MESSAGES...] EXIT-STATUS 14 usage() { 15 while [[ $# -gt 1 ]]; do 16 echo "$1" >&2 17 shift 18 done 19 echo "Usage:" 20 echo "./release.sh [OPTIONS] <VERSION_TO_RELEASE> [NEXT_VERSION]" 21 echo "" 22 echo "Options:" 23 echo " --dry-run|-n: only print commands, do not execute them." 24 echo "" 25 echo "Examples:" 26 echo " When releasing a new point release:" 27 echo " ./release.sh 0.6.3 0.6.4" 28 echo " When releasing a new major version:" 29 echo " ./release.sh 0.7.0 0.8.0" 30 echo "" 31 echo "You may omit NEXT_VERSION in order to avoid creating a commit on" 32 echo "the branch from which the release was created. Not recommended." 33 exit $1 34 } 35 36 while [ $# -gt 0 ]; do 37 case "$1" in 38 --dry-run|-n|--just-print) 39 DRY_RUN=true 40 git() { echo "git $*"; } 41 npm() { echo "npm $*"; } 42 ;; 43 -h|-\?|--help) 44 usage 0 45 ;; 46 -*) 47 usage "Unknown option: $1" "" 1 48 ;; 49 *) 50 case "$NARGS" in 51 0) 52 VERSION="$1" 53 NARGS=1 54 ;; 55 1) 56 NEXT_VERSION="$1" 57 NARGS=2 58 ;; 59 *) 60 usage "Too many arguments: $1" "" 1 61 ;; 62 esac 63 ;; 64 esac 65 shift 66 done 67 68 if [[ $NARGS = 0 ]]; then 69 usage "Missing argument: version number" "" 1 70 fi 71 72 # Some sanity checks up front 73 if ! command git diff --stat --exit-code HEAD; then 74 echo "Please make sure you have no uncommitted changes" >&2 75 : $((++INSANE)) 76 fi 77 if ! command npm owner ls katex | grep -q "^$(command npm whoami) <"; then 78 echo "You don't seem do be logged into npm, use \`npm login\`" >&2 79 : $((++INSANE)) 80 fi 81 if [[ $BRANCH != @(v*|master) ]]; then 82 echo "'$BRANCH' does not like a release branch to me" >&2 83 : $((++INSANE)) 84 fi 85 86 if [[ -z "$NEXT_VERSION" ]]; then 87 echo "About to release $VERSION from $BRANCH. " 88 else 89 echo "About to release $VERSION from $BRANCH and bump to $NEXT_VERSION-pre." 90 fi 91 if [[ $INSANE != 0 ]]; then 92 read -r -p "$INSANE sanity check(s) failed, really proceed? [y/n] " CONFIRM 93 else 94 read -r -p "Look good? [y/n] " CONFIRM 95 fi 96 if [[ "$CONFIRM" != "y" ]]; then 97 exit 1 98 fi 99 100 # Make a new detached HEAD 101 git checkout "$BRANCH" 102 git pull 103 git checkout --detach 104 105 # Build generated files and add them to the repository (for bower) 106 git clean -fdx build dist 107 make setup dist 108 sed -i.bak -E '/^\/dist\/$/d' .gitignore 109 rm -f .gitignore.bak 110 git add .gitignore dist/ 111 112 # Edit package.json and bower.json to the right version (see 113 # http://stackoverflow.com/a/22084103 for why we need the .bak file to make 114 # this mac & linux compatible) 115 sed -i.bak -E 's|"version": "[^"]+",|"version": "'$VERSION'",|' package.json 116 rm -f package.json.bak 117 118 # Update the version number in CDN URLs included in the README files, 119 # and regenerate the Subresource Integrity hash for these files. 120 node update-sri.js "${VERSION}" README.md contrib/*/README.md dist/README.md 121 122 # Make the commit and tag, and push them. 123 git add package.json bower.json README.md contrib/*/README.md dist/README.md 124 git commit -n -m "v$VERSION" 125 git diff --stat --exit-status # check for uncommitted changes 126 git tag -a "v$VERSION" -m "v$VERSION" 127 git push origin "v$VERSION" 128 129 # Update npm (bower and cdnjs update automatically) 130 npm publish 131 132 if [ ! -z "$NEXT_VERSION" ]; then 133 # Go back to original branch to bump 134 git checkout "$BRANCH" 135 136 # Edit package.json and bower.json to the right version 137 sed -i.bak -E 's|"version": "[^"]+",|"version": "'$NEXT_VERSION'-pre",|' package.json 138 rm -f package.json.bak 139 140 # Refer to the just-released version in the documentation of the 141 # development branch, too. Most people will read docs on master. 142 git checkout "v${VERSION}" -- README.md contrib/*/README.md 143 144 git add package.json bower.json 145 git commit -n -m "Bump $BRANCH to v$NEXT_VERSION-pre" 146 git push origin "$BRANCH" 147 148 # Go back to the tag which has build/katex.tar.gz and build/katex.zip 149 git checkout "v$VERSION" 150 fi 151 152 echo "" 153 echo "The automatic parts are done!" 154 echo "Now all that's left is to create the release on github." 155 echo "Visit https://github.com/Khan/KaTeX/releases/new?tag=v$VERSION to edit the release notes" 156 echo "Don't forget to upload build/katex.tar.gz and build/katex.zip to the release!" 157 158 if [[ ${DRY_RUN} ]]; then 159 echo "" 160 echo "This was a dry run." 161 echo "Operations using git or npm were printed not executed." 162 echo "Some files got modified, though, so you might want to undo " 163 echo "these changes now, e.g. using \`git checkout -- .\` or similar." 164 echo "" 165 fi