Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98a2370

Browse files
authored
chore: add release task (#4)
1 parent 2c0a2c2 commit 98a2370

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ source "https://rubygems.org"
55
# Specify your gem's dependencies in package_json.gemspec
66
gemspec
77

8+
gem "gem-release"
89
gem "rake", "~> 13.0"
910
gem "rspec", "~> 3.0"
1011
gem "rubocop", "< 1.51" # TODO: this version dropped support for Ruby 2.6

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ GEM
99
ast (2.4.2)
1010
diff-lcs (1.5.0)
1111
docile (1.4.0)
12+
gem-release (2.2.2)
1213
json (2.6.3)
1314
parallel (1.23.0)
1415
parser (3.2.2.3)
@@ -70,6 +71,7 @@ PLATFORMS
7071
x86_64-linux
7172

7273
DEPENDENCIES
74+
gem-release
7375
package_json!
7476
rake (~> 13.0)
7577
rspec (~> 3.0)

rakelib/create_release.rake

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)