release.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env bash
  2. # Exit script as soon as a command fails.
  3. set -o errexit
  4. log() {
  5. # Print to stderr to prevent this from being 'returned'
  6. echo "$@" > /dev/stderr
  7. }
  8. current_version() {
  9. echo "v$(node --print --eval "require('./package.json').version")"
  10. }
  11. current_release_branch() {
  12. v="$(current_version)"
  13. echo "release-${v%%-rc.*}"
  14. }
  15. assert_current_branch() {
  16. current_branch="$(git symbolic-ref --short HEAD)"
  17. expected_branch="$1"
  18. if [[ "$current_branch" != "$expected_branch" ]]; then
  19. log "Current branch '$current_branch' is not '$expected_branch'"
  20. exit 1
  21. fi
  22. }
  23. push_release_branch_and_tag() {
  24. git push upstream "$(current_release_branch)" "$(current_version)"
  25. }
  26. push_and_publish() {
  27. dist_tag="$1"
  28. log "Pushing release branch and tags to upstream"
  29. push_release_branch_and_tag
  30. log "Publishing package on npm"
  31. npm publish --tag "$dist_tag" --otp "$(prompt_otp)"
  32. if [[ "$dist_tag" == "latest" ]]; then
  33. npm dist-tag rm --otp "$(prompt_otp)" openzeppelin-solidity next
  34. fi
  35. }
  36. prompt_otp() {
  37. log -n "Enter npm 2FA token: "
  38. read -r otp
  39. echo "$otp"
  40. }
  41. environment_check() {
  42. if ! git remote get-url upstream &> /dev/null; then
  43. log "No 'upstream' remote found"
  44. exit 1
  45. fi
  46. if npm whoami &> /dev/null; then
  47. log "Will publish as '$(npm whoami)'"
  48. else
  49. log "Not logged in into npm, run 'npm login' first"
  50. exit 1
  51. fi
  52. }
  53. environment_check
  54. if [[ "$*" == "start minor" ]]; then
  55. log "Creating new minor pre-release"
  56. assert_current_branch master
  57. # Create temporary release branch
  58. git checkout -b release-temp
  59. # This bumps minor and adds rc suffix, commits the changes, and tags the commit
  60. npm version preminor --preid=rc
  61. # Rename the release branch
  62. git branch --move "$(current_release_branch)"
  63. push_and_publish next
  64. elif [[ "$*" == "rc" ]]; then
  65. log "Bumping pre-release"
  66. assert_current_branch "$(current_release_branch)"
  67. # Bumps rc number, commits and tags
  68. npm version prelease
  69. push_and_publish next
  70. elif [[ "$*" == "final" ]]; then
  71. # Update changelog release date, remove rc suffix, tag, push to git, publish in npm, remove next dist-tag
  72. log "Creating final release"
  73. assert_current_branch "$(current_release_branch)"
  74. # This will remove the -rc suffix from the version
  75. npm version patch
  76. push_release_branch_and_tag
  77. push_and_publish latest
  78. log "Remember to merge the release branch into master and push upstream"
  79. else
  80. log "Unknown command: '$*'"
  81. exit 1
  82. fi