|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "English" |
| 4 | +require "gem-release" |
| 5 | + |
| 6 | +desc("Releases the gem package using the given version. |
| 7 | +
|
| 8 | + IMPORTANT: the gem version must be in valid rubygem format (no dashes). |
| 9 | + This task depends on the gem-release ruby gem. |
| 10 | +
|
| 11 | + 1st argument: The new version in rubygem format (no dashes). Pass no argument to |
| 12 | + automatically perform a patch version bump. |
| 13 | + 2nd argument: Perform a dry run by passing 'true' as a second argument. |
| 14 | +
|
| 15 | + Example: `rake create_release[2.1.0,false]`") |
| 16 | + |
| 17 | +task :create_release, %i[gem_version dry_run] do |_t, args| |
| 18 | + args_hash = args.to_hash |
| 19 | + |
| 20 | + is_dry_run = Release.object_to_boolean(args_hash[:dry_run]) |
| 21 | + gem_version = args_hash.fetch(:gem_version, "").strip |
| 22 | + gem_root = Release.gem_root |
| 23 | + |
| 24 | + Release.update_the_local_project |
| 25 | + Release.ensure_there_is_nothing_to_commit |
| 26 | + Release.sh_in_dir(gem_root, |
| 27 | + "gem bump --no-commit #{gem_version == "" ? "" : "--version #{gem_version}"}") |
| 28 | + Release.sh_in_dir(gem_root, "bundle install") |
| 29 | + Release.sh_in_dir(gem_root, "git commit -am 'Bump version to #{gem_version}'") |
| 30 | + Release.sh_in_dir(gem_root, "git push") |
| 31 | + |
| 32 | + # See https://github.com/svenfuchs/gem-release |
| 33 | + Release.release_the_new_gem_version unless is_dry_run |
| 34 | +end |
| 35 | + |
| 36 | +module Release |
| 37 | + extend FileUtils |
| 38 | + class << self |
| 39 | + def gem_root |
| 40 | + File.expand_path("..", __dir__) |
| 41 | + end |
| 42 | + |
| 43 | + # Executes a string or an array of strings in a shell in the given directory in an unbundled environment |
| 44 | + def sh_in_dir(dir, *shell_commands) |
| 45 | + shell_commands.flatten.each { |shell_command| sh "cd #{dir} && #{shell_command.strip}" } |
| 46 | + end |
| 47 | + |
| 48 | + def ensure_there_is_nothing_to_commit |
| 49 | + status = `git status --porcelain` |
| 50 | + |
| 51 | + return if $CHILD_STATUS.success? && status == "" |
| 52 | + |
| 53 | + error = if $CHILD_STATUS.success? |
| 54 | + "You have uncommitted code. Please commit or stash your changes before continuing" |
| 55 | + else |
| 56 | + "You do not have Git installed. Please install Git, and commit your changes before continuing" |
| 57 | + end |
| 58 | + raise(error) |
| 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 update_the_local_project |
| 66 | + puts "Pulling latest commits from remote repository" |
| 67 | + |
| 68 | + sh_in_dir(gem_root, "git pull --rebase") |
| 69 | + raise "Failed in pulling latest changes from default remote repository." unless $CHILD_STATUS.success? |
| 70 | + rescue Errno::ENOENT |
| 71 | + raise "Ensure you have Git and Bundler installed before continuing." |
| 72 | + end |
| 73 | + |
| 74 | + def release_the_new_gem_version |
| 75 | + puts "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" |
| 76 | + puts "Use the OTP for RubyGems!" |
| 77 | + puts "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" |
| 78 | + |
| 79 | + sh_in_dir(gem_root, "gem release --push --tag") |
| 80 | + end |
| 81 | + end |
| 82 | +end |
0 commit comments