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

Inherits:
Packager show all
Defined in:
lib/kamaze/project/tools/gemspec/builder.rb,
lib/kamaze/project/tools/gemspec.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)


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

def build
  prepare

  buildable_dir = self.buildable.dirname.realpath

  # dive into ``src`` file
  Dir.chdir(gemspec_srcfile.dirname) do
    Gem::GemRunner.new.run(build_args).yield_self do |file|
      FileUtils.mv(file, buildable_dir)

      buildable_dir.join(file)
    end
  end

  self
end

#build_argsArray<String> (protected)

Get args used by gem command

Returns:

  • (Array<String>)


94
95
96
97
98
# File 'lib/kamaze/project/tools/gemspec/builder.rb', line 94

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

#buildablePathname

Get buildable (relative path)

Returns:

  • (Pathname)


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

def buildable
  full_name = gemspec_reader.read(Hash).fetch(:full_name)
  file_path = fs.package_dirs
                .fetch(:gem)
                .join("#{full_name}.gem")
                .to_s
                .gsub(%r{^\./}, '')

  ::Pathname.new(file_path)
end

#gemspec_srcfilePathname

Get path do gemspec present in src dir

Returns:

  • (Pathname)


72
73
74
75
76
77
78
79
# File 'lib/kamaze/project/tools/gemspec/builder.rb', line 72

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

#ready?Boolean Also known as: buildable?

Returns:

  • (Boolean)


63
64
65
# File 'lib/kamaze/project/tools/gemspec/builder.rb', line 63

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

#setupObject (protected)



83
84
85
86
87
88
89
# File 'lib/kamaze/project/tools/gemspec/builder.rb', line 83

def setup
  super

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