Class: Kamaze::Project::Tools::Gemspec::Builder

Inherits:
Packager show all
Defined in:
lib/kamaze/project/tools/gemspec/builder.rb

Overview

Package a gem from its own gemspec file

The build command allows you to create a gem from a ruby gemspec.

The best way to build a gem is to use a Rakefile and the Gem::PackageTask which ships with RubyGems. But, you can also use this class ;).

Sample of use:

builder = Kamaze.project.tools.fetch(:gemspec_builder)
builder.build if builder.ready?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Kamaze::Project::Tools::Packager

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Kamaze::Project::Tools::Packager

Instance Attribute Details

#observer_peersHash|nil (readonly, protected) Originally defined in module Concern::Observable

Returns:

  • (Hash|nil)

Instance Method Details

#buildself

Build .gem file

Returns:

  • (self)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kamaze/project/tools/gemspec/builder.rb', line 31

def build
  self.tap do
    prepare

    buildable_dir = self.buildable.dirname.realpath

    # dive into ``src`` file
    Dir.chdir(gemspec_srcfile.dirname) do
      self.gem_runner.run(build_args).yield_self do |file|
        FileUtils.mv(file, buildable_dir)

        buildable_dir.join(file)
      end
    end
  end
end

#build_argsArray<String> (protected)

Get args used by gem command

Returns:

  • (Array<String>)


106
107
108
109
110
# File 'lib/kamaze/project/tools/gemspec/builder.rb', line 106

def build_args
  Dir.chdir(pwd) do
    [:build, gemspec_srcfile, '--norc'].map(&:to_s)
  end
end

#buildablePathname

Get buildable (relative path)

Returns:

  • (Pathname)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kamaze/project/tools/gemspec/builder.rb', line 51

def buildable
  return nil unless ready? # @todo raise an explicit exception

  gemspec_reader.read(Hash).fetch(:full_name).tap do |full_name|
    # @formatter:off
    return fs.package_dirs
             .fetch(:gem)
             .join("#{full_name}.gem")
             .to_s
             .gsub(%r{^\./}, '')
             .yield_self { |file_path| Pathname.new(file_path) }
    # @formatter:on
  end
end

#gem_runnerGem::GemRunner (protected)

Returns:

  • (Gem::GemRunner)


96
97
98
99
100
101
# File 'lib/kamaze/project/tools/gemspec/builder.rb', line 96

def gem_runner
  require 'rubygems'
  require 'rubygems/gem_runner'

  Gem::GemRunner.new
end

#gemspec_srcfilePathname

Get path do gemspec present in src dir

Returns:

  • (Pathname)


75
76
77
78
79
80
81
82
83
# File 'lib/kamaze/project/tools/gemspec/builder.rb', line 75

def gemspec_srcfile
  # @formatter:off
  (package_dirs.fetch(:src)
       .realpath
       .join(buildable.basename('.*')).to_s
       .gsub(/-([0-9 \.])+$/, '') + '.gemspec')
    .yield_self { |s| Pathname.new(s) }
  # @formatter:on
end

#ready?Boolean Also known as: buildable?

Returns:

  • (Boolean)


66
67
68
# File 'lib/kamaze/project/tools/gemspec/builder.rb', line 66

def ready?
  gemspec_reader.read(Hash)[:full_name].nil? ? false : super
end

#setupObject (protected)



87
88
89
90
91
92
93
# File 'lib/kamaze/project/tools/gemspec/builder.rb', line 87

def setup
  super

  self.package_labels = [:src, :gem]
  self.purgeables = [:gem]
  self.package_name = "ruby/gem-#{Gem::VERSION}"
end