Skip to content

Create release script #1262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ GEM
execjs (2.7.0)
ffi (1.15.5)
formatador (1.1.0)
gem-release (2.2.2)
globalid (1.0.1)
activesupport (>= 5.0)
guard (2.18.0)
Expand Down Expand Up @@ -248,6 +249,7 @@ DEPENDENCIES
codeclimate-test-reporter
coffee-rails
es5-shim-rails (>= 2.0.5)
gem-release
guard
guard-minitest
jbuilder
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"build": "cd react_ujs && webpack"
},
"devDependencies": {
"webpack": "^5.74.0"
"webpack": "^5.74.0",
"webpack-cli": "^5.0.1"
}
}
125 changes: 125 additions & 0 deletions rakelib/create_release.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# frozen_string_literal: true

require 'English'

desc("Releases both the gem and node package using the given version.
IMPORTANT: the gem version must be in valid rubygem format (no dashes).
It will be automatically converted to a valid yarn semver by the rake task
for the node package version. This only makes a difference for pre-release
versions such as `3.0.0.beta.1` (yarn version would be `3.0.0-beta.1`).
This task depends on the gem-release (ruby gem) and release-it (node package)
which are installed via `bundle install` and `yarn global add release-it`

1st argument: The new version in rubygem format (no dashes). Pass no argument to
automatically perform a patch version bump.
2nd argument: Perform a dry run by passing 'true' as a second argument.

Note, accept defaults for npmjs options. Script will pause to get 2FA tokens.
Example: `rake release[2.1.0,false]`")

task :create_release, %i[gem_version dry_run] do |_t, args|
args_hash = args.to_hash

is_dry_run = Release.object_to_boolean(args_hash[:dry_run])
gem_version = args_hash.fetch(:gem_version, '').strip
npm_version = gem_version.empty? ? '' : Release.convert_rubygem_to_npm_version(gem_version)

Release.update_the_local_project
Release.ensure_there_is_nothing_to_commit

# Preparing for release

# Updating the pre-bundled react
puts 'Updating react'
Rake::Task['react:update'].invoke

# Updating ReactRailsUJS
puts 'Updating ujs:update'
Rake::Task['ujs:update'].invoke

release_the_new_npm_version(npm_version, is_dry_run)
release_the_new_gem_version(gem_version, is_dry_run)

Release.push
end

# A collection of helper functions for gem and npm release
module Release
extend FileUtils
class << self
def gem_root
File.expand_path('..', __dir__)
end

# Executes a string or an array of strings in a shell in the given directory in an unbundled environment
def sh_in_dir(dir, *shell_commands)
shell_commands.flatten.each { |shell_command| sh %(cd #{dir} && #{shell_command.strip}) }
end

def ensure_there_is_nothing_to_commit
status = `git status --porcelain`

return if $CHILD_STATUS.success? && status == ''

error = if $CHILD_STATUS.success?
'You have uncommitted code. Please commit or stash your changes before continuing'
else
'You do not have Git installed. Please install Git, and commit your changes before continuing'
end
raise(error)
end

def object_to_boolean(value)
[true, 'true', 'yes', 1, '1', 't'].include?(value.instance_of?(String) ? value.downcase : value)
end

def convert_rubygem_to_npm_version(gem_version)
regex_match = gem_version.match(/(\d+\.\d+\.\d+)[.-]?(.+)?/)
return "#{regex_match[1]}-#{regex_match[2]}" if regex_match[2]

regex_match[1].to_s
end

def update_the_local_project
puts 'Pulling latest commits from remote repository'

sh_in_dir(gem_root, 'git pull --rebase')
raise 'Failed in pulling latest changes from default remore repository.' unless $CHILD_STATUS.success?
rescue Errno::ENOENT
raise 'Ensure you have Git and Bundler installed before continuing.'
end

def release_the_new_gem_version(gem_version, is_dry_run)
puts 'Bumping gem version'
Release.sh_in_dir(
gem_root,
"gem bump --no-commit #{gem_version == '' ? '' : %(--version #{gem_version})}",
'bundle install',
"git commit -am 'Bump version to #{gem_version}'"
)
puts 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ'
puts 'Use the OTP for RubyGems!'
puts 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ'

sh_in_dir(gem_root, 'gem release --push --tag') unless is_dry_run
end

def release_the_new_npm_version(npm_version, is_dry_run)
puts 'Making npm release'
# Will bump the yarn version, commit, tag the commit, push to repo, and release on yarn
release_it_command = +'release-it'
release_it_command << " #{npm_version}" unless npm_version == ''
release_it_command << ' --npm.publish --no-git.requireCleanWorkingDir'
release_it_command << ' --dry-run --verbose' if is_dry_run
puts 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ'
puts 'Use the OTP for NPM!'
puts 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ'

system(release_it_command)
end

def push
sh_in_dir(gem_root, 'git push')
end
end
end
1 change: 1 addition & 0 deletions react-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'codeclimate-test-reporter'
s.add_development_dependency 'coffee-rails'
s.add_development_dependency 'es5-shim-rails', '>= 2.0.5'
s.add_development_dependency 'gem-release'
s.add_development_dependency 'guard'
s.add_development_dependency 'guard-minitest'
s.add_development_dependency 'jbuilder'
Expand Down
Loading