-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgem_tasks.rb
69 lines (57 loc) · 1.62 KB
/
gem_tasks.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'rubygems/specification'
module Sunspot
class GemTasks
PROJECT_ROOT = File.dirname(Rake.application.rakefile_location)
def initialize(dependencies = {}, &block)
@dependencies = dependencies
@gemspec_block = block
task(:gemspec, "Write gemspec")
task(:build, "Build gem")
task(:tag, "Tag version in Git and push to origin")
task(:release, "Release gem to Gemcutter")
end
def build
run_dependencies(:build)
filename = Gem::Builder.new(spec).build
FileUtils.mv(filename, File.join(PROJECT_ROOT, 'pkg'))
File.join('pkg', filename)
end
def tag
tag_name = "v#{spec.version}"
puts `git tag -m "Released gem version #{spec.version}" #{tag_name}`
puts `git push origin #{tag_name}:#{tag_name}`
end
def release
validate_or_abort
tag
puts `gem push #{build}`
end
def gemspec
File.open(File.join(PROJECT_ROOT, "#{spec.name}.gemspec"), 'w') do |file|
file << spec.to_ruby
end
end
def spec
@spec ||= Gem::Specification.new(&@gemspec_block)
end
def validate_or_abort
begin
spec.validate
rescue Gem::InvalidSpecificationException => e
abort("Gemspec is invalid: #{e.message}")
end
end
private
def task(name, description)
Rake.application.last_description = description
Rake::Task.define_task(name) { send(name) }
end
def run_dependencies(task)
if @dependencies[task]
Array(@dependencies[task]).each do |dependency|
Rake::Task[dependency].invoke
end
end
end
end
end