release.sh 3.6 KB

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