release.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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-solidity 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-solidity next
  42. npm dist-tag rm --otp "$otp" @openzeppelin/contracts next
  43. fi
  44. }
  45. push_and_publish() {
  46. dist_tag="$1"
  47. log "Pushing release branch and tags to upstream"
  48. push_release_branch_and_tag
  49. publish "$dist_tag"
  50. }
  51. prompt_otp() {
  52. log -n "Enter npm 2FA token: "
  53. read -r otp
  54. echo "$otp"
  55. }
  56. environment_check() {
  57. if ! git remote get-url upstream &> /dev/null; then
  58. log "No 'upstream' remote found"
  59. exit 1
  60. fi
  61. if npm whoami &> /dev/null; then
  62. log "Will publish as '$(npm whoami)'"
  63. else
  64. log "Not logged in into npm, run 'npm login' first"
  65. exit 1
  66. fi
  67. }
  68. environment_check
  69. if [[ "$*" == "push" ]]; then
  70. push_and_publish next
  71. elif [[ "$*" == "start minor" ]]; then
  72. log "Creating new minor pre-release"
  73. assert_current_branch master
  74. # Create temporary release branch
  75. git checkout -b release-temp
  76. # This bumps minor and adds prerelease suffix, commits the changes, and tags the commit
  77. npm version preminor --preid="$PRERELEASE_SUFFIX"
  78. # Rename the release branch
  79. git branch --move "$(current_release_branch)"
  80. push_and_publish next
  81. elif [[ "$*" == "start major" ]]; then
  82. log "Creating new major pre-release"
  83. assert_current_branch master
  84. # Create temporary release branch
  85. git checkout -b release-temp
  86. # This bumps major and adds prerelease suffix, commits the changes, and tags the commit
  87. npm version premajor --preid="$PRERELEASE_SUFFIX"
  88. # Rename the release branch
  89. git branch --move "$(current_release_branch)"
  90. push_and_publish next
  91. elif [[ "$*" == "rc" ]]; then
  92. log "Bumping pre-release"
  93. assert_current_branch "$(current_release_branch)"
  94. # Bumps prerelease number, commits and tags
  95. npm version prerelease
  96. push_and_publish next
  97. elif [[ "$*" == "final" ]]; then
  98. # Update changelog release date, remove prerelease suffix, tag, push to git, publish in npm, remove next dist-tag
  99. log "Creating final release"
  100. assert_current_branch "$(current_release_branch)"
  101. # This will remove the prerelease suffix from the version
  102. npm version patch
  103. push_release_branch_and_tag
  104. push_and_publish latest
  105. npm deprecate 'openzeppelin-solidity@>=4.0.0' "This package is now published as @openzeppelin/contracts. Please change your dependency."
  106. log "Remember to merge the release branch into master and push upstream"
  107. else
  108. log "Unknown command: '$*'"
  109. exit 1
  110. fi