class Kamaze::Project::Tools::Gemspec::Packer::Command

Build/pack a buildable

Command is able to execute itself. Command depends on “FileUtils::sh“ provided by “rake“.

During execution, command “chdir“ to “src_dir“.

Attributes

bin_dir[W]

The directory where “compiled” executable stands

@type [String]

executable[W]

Executable used by command

@type [String|Symbol]

packable[RW]

Filepath to the product issued by command

pwd[R]

@return [Pathname]

src_dir[RW]

The path to source files

tmp_dir[RW]

The directory for temporary files

Public Class Methods

new() { |self| ... } click to toggle source

@raise InitializationError

# File lib/kamaze/project/tools/gemspec/packer/command.rb, line 52
def initialize
  yield self

  @executable ||= :rubyc
  @pwd = ::Pathname.new(Dir.pwd)

  [:executable, :packable, :tmp_dir, :bin_dir].each do |m|
    self.singleton_class.class_eval { protected "#{m}=" }

    next unless self.__send__(m).nil?

    raise InitializationError, "#{m} must be set, got nil"
  end
end

Public Instance Methods

bin_dir() click to toggle source

Get path to “compiled” input file

SHOULD like to “bin“ as seen in gemspec “bindir“

@return [Pathname]

# File lib/kamaze/project/tools/gemspec/packer/command.rb, line 79
def bin_dir
  ::Pathname.new(@bin_dir)
end
executable() click to toggle source

Executable used by command

@return [Pathname]

# File lib/kamaze/project/tools/gemspec/packer/command.rb, line 70
def executable
  ::Pathname.new(Cliver.detect!(@executable)).freeze
end
execute() click to toggle source
# File lib/kamaze/project/tools/gemspec/packer/command.rb, line 94
def execute
  env = preserved_env

  Dir.chdir(pwd.join(src_dir)) do
    with_exit_on_failure do
      Bundler.with_clean_env do
        sh(*[env].concat(self.to_a))

        self.retcode = self.shell_runner_last_status.exitstatus
      end
    end
  end
end
to_a() click to toggle source

@return [Array<String>]

# File lib/kamaze/project/tools/gemspec/packer/command.rb, line 84
def to_a
  packable = ::Pathname.new(self.packable)

  [executable,
   bin_dir.join(packable.basename),
   '-r', '.',
   '-d', pwd.join(tmp_dir),
   '-o', pwd.join(packable)].map(&:to_s)
end

Protected Instance Methods

preserved_env(from = ENV) click to toggle source

Get preserved env (from given env)

@param [ENV|Hash] from @return [Hash]

@todo refactor

# File lib/kamaze/project/tools/gemspec/packer/command.rb, line 131
def preserved_env(from = ENV)
  env = {}
  from = from.to_h

  ['CPPFLAGS'].each do |key|
    next unless from.key?(key)

    env[key] = from.fetch(key)
  end

  env
end
process_attrs() click to toggle source

Process attributes

@raise InitializationError

# File lib/kamaze/project/tools/gemspec/packer/command.rb, line 113
def process_attrs
  [:executable, :packable, :tmp_dir, :bin_dir].each do |m|
    self.singleton_class.class_eval { protected "#{m}=" }

    if self.__send__(m).nil?
      raise InitializationError, "#{m} must be set, got nil"
    end
  end

  self
end