Class: Yap::GemHelper

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/yap/gem_helper.rb

Constant Summary collapse

Color =
Term::ANSIColor

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base = nil, name = nil) ⇒ GemHelper

Returns a new instance of GemHelper.



22
23
24
25
26
27
28
29
# File 'lib/yap/gem_helper.rb', line 22

def initialize(base = nil, name = nil)
  @base ||= Dir.pwd
  gemspecs = name ? [File.join(@base, "#{name}.gemspec")] : Dir[File.join(@base, "{,*}.gemspec")]
  raise "Unable to determine name from existing gemspec. Use :name => 'gemname' in #install_tasks to manually set it." unless gemspecs.size == 1
  @spec_path = gemspecs.first
  @gemspec = Bundler.load_gemspec(@spec_path)
  @export_as = Yap::Addon.export_as_for_gemspec(@gemspec)
end

Class Attribute Details

.instanceObject

set when install’d.



13
14
15
# File 'lib/yap/gem_helper.rb', line 13

def instance
  @instance
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



20
21
22
# File 'lib/yap/gem_helper.rb', line 20

def base
  @base
end

#gemspecObject (readonly)

Returns the value of attribute gemspec.



20
21
22
# File 'lib/yap/gem_helper.rb', line 20

def gemspec
  @gemspec
end

#spec_pathObject (readonly)

Returns the value of attribute spec_path.



20
21
22
# File 'lib/yap/gem_helper.rb', line 20

def spec_path
  @spec_path
end

Class Method Details

.install_tasks(opts = {}) ⇒ Object



15
16
17
# File 'lib/yap/gem_helper.rb', line 15

def install_tasks(opts = {})
  new(opts[:dir], opts[:name]).install
end

Instance Method Details

#build_gemObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/yap/gem_helper.rb', line 74

def build_gem
  file_name = nil
  sh("gem build -V '#{spec_path}'") do
    file_name = File.basename(built_gem_path)
    FileUtils.mkdir_p(File.join(base, 'pkg'))
    FileUtils.mv(built_gem_path, 'pkg')
    tell_user_success("#{name} #{version} built to pkg/#{file_name}.")
  end
  File.join(base, "pkg", file_name)
end

#installObject



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
70
71
72
# File 'lib/yap/gem_helper.rb', line 31

def install
  built_gem_path = nil

  desc "Build #{Color.yellow(@export_as)} yap addon as gem #{name}-#{version}.gem"
  task "build" do
    built_gem_path = build_gem
    puts "Don't forget to run #{Term::ANSIColor.yellow('rake install && reload!')} to see your changes reflected in yap."
    puts Term::ANSIColor.bright_black("P.S. You can skip this step if you always run 'rake install'")
  end

  desc "Build and install #{Color.yellow(@export_as)} yap addon as gem #{name}-#{version}.gem into system gems."
  task "install" do
    built_gem_path = build_gem
    install_gem(built_gem_path)
    puts "Don't forget to run #{Term::ANSIColor.yellow('reload!')} to see your changes reflected in yap."
  end

  desc "Build and install #{Color.yellow(@export_as)} yap addon as #{name}-#{version}.gem into system gems without network access."
  task "install:local" => "build" do
    install_gem(built_gem_path, :local)
  end

  desc "Create tag #{version_tag} and build and push #{Color.yellow(@export_as)} addon #{name}-#{version}.gem to Rubygems\n" \
       "To prevent publishing in Rubygems use `gem_push=no rake release`"
  task "release", [:remote] => ["build", "release:guard_clean",
                                "release:source_control_push", "release:rubygem_push"] do
  end

  task "release:guard_clean" do
    guard_clean
  end

  task "release:source_control_push", [:remote] do |_, args|
    tag_version { git_push(args[:remote]) } unless already_tagged?
  end

  task "release:rubygem_push" do
    rubygem_push(built_gem_path) if gem_push?
  end

  GemHelper.instance = self
end

#install_gem(built_gem_path = nil, local = false) ⇒ Object



85
86
87
88
89
90
# File 'lib/yap/gem_helper.rb', line 85

def install_gem(built_gem_path = nil, local = false)
  built_gem_path ||= build_gem
  out, _ = sh_with_code("gem install '#{built_gem_path}'#{" --local" if local}")
  raise "Couldn't install gem, run `gem install #{built_gem_path}' for more detailed output" unless out[/Successfully installed/]
  tell_user_success("#{name} (#{version}) installed.")
end