Skip to content

Commit 16ff304

Browse files
committed
Add gem tasks
1 parent 231143e commit 16ff304

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
require "rake"
12
require "rspec/core/rake_task"
3+
require_relative "rake/gem"
24

35
namespace :octofacts do
46
task :default => [:"octofacts:spec:octofacts", :"octofacts:spec:octofacts_updater", :"octofacts:spec:octofacts_integration"] do

rake/gem.rb

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
require "fileutils"
2+
require "open3"
3+
require "shellwords"
4+
5+
module Octofacts
6+
# A class to contain methods and constants for cleaner code
7+
class Gem
8+
BASEDIR = File.expand_path("..", File.dirname(__FILE__)).freeze
9+
GEMS = ["octofacts", "octofacts-updater"].freeze
10+
PKGDIR = File.join(BASEDIR, "pkg").freeze
11+
12+
# Verify that Gemfile.lock matches .version and that it's committed, since `bundle exec ...` will
13+
# update the file for us.
14+
def self.verify_gemfile_version!
15+
bundler = Bundler::LockfileParser.new(Bundler.read_file(File.expand_path("../Gemfile.lock", File.dirname(__FILE__))))
16+
gems = bundler.specs.select { |specs| GEMS.include?(specs.name) }
17+
GEMS.each do |gem|
18+
this_gem = gems.detect { |g| g.name == gem }
19+
unless this_gem
20+
raise "Did not find #{gem} in Gemfile.lock"
21+
end
22+
unless this_gem.version.to_s == version
23+
raise "Gem #{gem} is version #{this_gem.version}, not #{version}"
24+
end
25+
end
26+
27+
puts "Ensuring that all changes are committed."
28+
exec_command("git diff-index --quiet HEAD --")
29+
puts "OK: All gems on #{version} and no uncommitted changes here."
30+
end
31+
32+
# Read the version number from the .version file in the root of the project.
33+
def self.version
34+
@version ||= File.read(File.expand_path("../.version", File.dirname(__FILE__))).strip
35+
end
36+
37+
# Determine what branch we are on
38+
def self.branch
39+
exec_command("git rev-parse --abbrev-ref HEAD").strip
40+
end
41+
42+
# Build the gem and put it into the 'pkg' directory
43+
def self.build
44+
Dir.mkdir PKGDIR unless File.directory?(PKGDIR)
45+
GEMS.each do |gem|
46+
begin
47+
output_file = File.join(BASEDIR, "#{gem}-#{version}.gem")
48+
target_file = File.join(PKGDIR, "#{gem}-#{version}.gem")
49+
exec_command("gem build #{gem}.gemspec")
50+
unless File.file?(output_file)
51+
raise "gem #{gem} failed to create expected output file"
52+
end
53+
FileUtils.mv output_file, target_file
54+
puts "Generated #{target_file}"
55+
ensure
56+
# Clean up the *.gem generated in the main directory if it's still there
57+
FileUtils.rm(output_file) if File.file?(output_file)
58+
end
59+
end
60+
end
61+
62+
# Push the gem to rubygems
63+
def self.push
64+
GEMS.each do |gem|
65+
target_file = File.join(PKGDIR, "#{gem}-#{version}.gem")
66+
unless File.file?(target_file)
67+
raise "Cannot push: #{target_file} does not exist"
68+
end
69+
end
70+
GEMS.each do |gem|
71+
target_file = File.join(PKGDIR, "#{gem}-#{version}.gem")
72+
exec_command("gem push #{Shellwords.escape(target_file)}")
73+
end
74+
end
75+
76+
# Tag the release on GitHub
77+
def self.tag
78+
# Make sure we have not released this version before
79+
exec_command("git fetch -t origin")
80+
tags = exec_command("git tag -l").split(/\n/)
81+
raise "There is already a #{version} tag" if tags.include?(version)
82+
83+
# Tag it
84+
exec_command("git tag #{Shellwords.escape(version)}")
85+
exec_command("git push origin master")
86+
exec_command("git push origin #{Shellwords.escape(version)}")
87+
end
88+
89+
# Yank gem from rubygems
90+
def self.yank
91+
GEMS.each do |gem|
92+
exec_command("gem yank #{gem} -v #{Shellwords.escape(version)}")
93+
end
94+
end
95+
96+
# Utility method: Execute command
97+
def self.exec_command(command)
98+
STDERR.puts "Command: #{command}"
99+
output, code = Open3.capture2e(command, chdir: BASEDIR)
100+
return output if code.exitstatus.zero?
101+
STDERR.puts "Output:\n#{output}"
102+
STDERR.puts "Exit code: #{code.exitstatus}"
103+
exit code.exitstatus
104+
end
105+
end
106+
end
107+
108+
namespace :gem do
109+
task "build" do
110+
branch = Octofacts::Gem.branch
111+
unless branch == "master"
112+
raise "On a non-master branch #{branch}; use gem:force-build if you really want to do this"
113+
end
114+
Octofacts::Gem.build
115+
end
116+
117+
task "check" do
118+
Octofacts::Gem.verify_gemfile_version!
119+
end
120+
121+
task "force-build" do
122+
branch = Octofacts::Gem.branch
123+
unless branch == "master"
124+
warn "WARNING: Force-building from non-master branch #{branch}"
125+
end
126+
Octofacts::Gem.build
127+
end
128+
129+
task "push" do
130+
Octofacts::Gem.push
131+
end
132+
133+
task "release" do
134+
branch = Octofacts::Gem.branch
135+
unless branch == "master"
136+
raise "On a non-master branch #{branch}; refusing to release"
137+
end
138+
[:check, :build, :tag, :push].each { |t| Rake::Task["gem:#{t}"].invoke }
139+
end
140+
141+
task "tag" do
142+
branch = Octofacts::Gem.branch
143+
raise "On a non-master branch #{branch}; refusing to tag" unless branch == "master"
144+
Octofacts::Gem.tag
145+
end
146+
147+
task "yank" do
148+
Octofacts::Gem.yank
149+
end
150+
end

0 commit comments

Comments
 (0)