|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'English' |
| 4 | + |
| 5 | +desc("Releases both the gem and node package using the given version. |
| 6 | + IMPORTANT: the gem version must be in valid rubygem format (no dashes). |
| 7 | + It will be automatically converted to a valid yarn semver by the rake task |
| 8 | + for the node package version. This only makes a difference for pre-release |
| 9 | + versions such as `3.0.0.beta.1` (yarn version would be `3.0.0-beta.1`). |
| 10 | + This task depends on the gem-release (ruby gem) and release-it (node package) |
| 11 | + which are installed via `bundle install` and `yarn global add release-it` |
| 12 | +
|
| 13 | + 1st argument: The new version in rubygem format (no dashes). Pass no argument to |
| 14 | + automatically perform a patch version bump. |
| 15 | + 2nd argument: Perform a dry run by passing 'true' as a second argument. |
| 16 | +
|
| 17 | + Note, accept defaults for npmjs options. Script will pause to get 2FA tokens. |
| 18 | + Example: `rake release[2.1.0,false]`") |
| 19 | + |
| 20 | +task :create_release, %i[gem_version dry_run] do |_t, args| |
| 21 | + args_hash = args.to_hash |
| 22 | + is_dry_run = object_to_boolean(args_hash[:dry_run]) |
| 23 | + |
| 24 | + gem_version = args_hash.fetch(:gem_version, '').strip |
| 25 | + npm_version = gem_version.empty? ? '' : convert_rubygem_to_npm_version(gem_version) |
| 26 | + gem_root = File.expand_path('..', __dir__) |
| 27 | + |
| 28 | + # Prepare for release |
| 29 | + Dir.chdir(gem_root) |
| 30 | + ensure_there_is_nothing_to_commit |
| 31 | + |
| 32 | + # Updating the pre-bundled react |
| 33 | + Rake::Task['react:update'].invoke |
| 34 | + |
| 35 | + # Updating ReactRailsUJS |
| 36 | + Rake::Task['ujs:update'].invoke |
| 37 | + |
| 38 | + # release npm version |
| 39 | + # Will bump the yarn version, commit, tag the commit, push to repo, and release on yarn |
| 40 | + release_it_command = +'release-it' |
| 41 | + release_it_command << " #{npm_version}" unless npm_version == '' |
| 42 | + release_it_command << ' --npm.publish --no-git.requireCleanWorkingDir' |
| 43 | + release_it_command << ' --dry-run --verbose' if is_dry_run |
| 44 | + puts 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ' |
| 45 | + puts 'Use the OTP for NPM!' |
| 46 | + puts 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ' |
| 47 | + |
| 48 | + system(release_it_command) |
| 49 | + |
| 50 | + # release gem version |
| 51 | + # Update lib/react/rails/version.rb |
| 52 | + `gem bump --no-commit #{gem_version == '' ? '' : %(--version #{gem_version})}` |
| 53 | + |
| 54 | + puts 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ' |
| 55 | + puts 'Use the OTP for RubyGems!' |
| 56 | + puts 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ' |
| 57 | + |
| 58 | + `gem release` unless is_dry_run |
| 59 | +end |
| 60 | + |
| 61 | +def object_to_boolean(value) |
| 62 | + [true, 'true', 'yes', 1, '1', 't'].include?(value.instance_of?(String) ? value.downcase : value) |
| 63 | +end |
| 64 | + |
| 65 | +def convert_rubygem_to_npm_version(gem_version) |
| 66 | + regex_match = gem_version.match(/(\d+\.\d+\.\d+)[.-]?(.+)?/) |
| 67 | + return "#{regex_match[1]}-#{regex_match[2]}" if regex_match[2] |
| 68 | + |
| 69 | + regex_match[1].to_s |
| 70 | +end |
| 71 | + |
| 72 | +def ensure_there_is_nothing_to_commit |
| 73 | + status = `git status --porcelain` |
| 74 | + |
| 75 | + return if $CHILD_STATUS.success? && status == '' |
| 76 | + |
| 77 | + error = if $CHILD_STATUS.success? |
| 78 | + 'You have uncommitted code. Please commit or stash your changes before continuing' |
| 79 | + else |
| 80 | + 'You do not have Git installed. Please install Git, and commit your changes before continuing' |
| 81 | + end |
| 82 | + raise(error) |
| 83 | +end |
| 84 | + |
| 85 | +def update_the_local_project |
| 86 | + puts 'Pulling latest commits from remote repository' |
| 87 | + |
| 88 | + `git pull --rebase` |
| 89 | + raise 'Failed in pulling latest changes from default remore repository.' unless $CHILD_STATUS.success? |
| 90 | + |
| 91 | + `bundle install` |
| 92 | +rescue Errno::ENOENT |
| 93 | + raise 'Ensure you have Git and Bundler installed before continuing.' |
| 94 | +end |
0 commit comments