Class: Kamaze::Project::Tools::Gemspec::Packer::Command

Inherits:
Object
  • Object
show all
Includes:
Concern::Cli::WithExitOnFailure, Concern::Sh
Defined in:
lib/kamaze/project/tools/gemspec/packer/command.rb

Overview

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.

Defined Under Namespace

Classes: InitializationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Command

Returns a new instance of Command.

Yields:

  • (_self)

Yield Parameters:

Raises:

  • InitializationError



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

Instance Attribute Details

#bin_dirPathname

Get path to "compiled" input file

SHOULD like to bin as seen in gemspec bindir

Returns:

  • (Pathname)


79
80
81
# File 'lib/kamaze/project/tools/gemspec/packer/command.rb', line 79

def bin_dir
  ::Pathname.new(@bin_dir)
end

#executablePathname

Executable used by command

Returns:

  • (Pathname)


70
71
72
# File 'lib/kamaze/project/tools/gemspec/packer/command.rb', line 70

def executable
  ::Pathname.new(Cliver.detect!(@executable)).freeze
end

#packableObject

Filepath to the product issued by command



32
33
34
# File 'lib/kamaze/project/tools/gemspec/packer/command.rb', line 32

def packable
  @packable
end

#pwdPathname (readonly)

Returns:

  • (Pathname)


46
47
48
# File 'lib/kamaze/project/tools/gemspec/packer/command.rb', line 46

def pwd
  @pwd
end

#src_dirObject

The path to source files



43
44
45
# File 'lib/kamaze/project/tools/gemspec/packer/command.rb', line 43

def src_dir
  @src_dir
end

#tmp_dirObject

The directory for temporary files



35
36
37
# File 'lib/kamaze/project/tools/gemspec/packer/command.rb', line 35

def tmp_dir
  @tmp_dir
end

Instance Method Details

#create_shell_runner(cmd) ⇒ Proc (protected) Originally defined in module Concern::Sh

Get shell block

Parameters:

  • cmd (Array)

Returns:

  • (Proc)

#debug_cmd(cmd) ⇒ String (protected) Originally defined in module Concern::Sh

Print a debug message

Parameters:

  • cmd (Array)

Returns:

  • (String)

#executeObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# 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

#failure?Boolean Also known as: failed? Originally defined in module Concern::Cli

Denote execution is a failure.

Returns:

  • (Boolean)

#preserved_env(from = ENV) ⇒ Hash (protected)

TODO:

refactor

Get preserved env (from given env)

Parameters:

  • from (ENV|Hash) (defaults to: ENV)

Returns:

  • (Hash)


131
132
133
134
135
136
137
138
139
140
141
142
# 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_attrsObject (protected)

Process attributes

Raises:

  • InitializationError



113
114
115
116
117
118
119
120
121
122
123
# 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

#retcodeFixnum Originally defined in module Concern::Cli

Status code usable to eventually initiates the termination.

Returns:

  • (Fixnum)

#sh(*cmd, &block) ⇒ Object (protected) Originally defined in module Concern::Sh

Run given (cmd) system command.

If multiple arguments are given the command is run directly (without the shell, same semantics as Kernel::exec and Kernel::system).

#shell_runner_debug?Boolean Originally defined in module Concern::Sh

Denote shell runner is in debug mode.

Returns:

  • (Boolean)

#success?Boolean Also known as: successful? Originally defined in module Concern::Cli

Denote execution is a success.

Returns:

  • (Boolean)

#to_aArray<String>

Returns:

  • (Array<String>)


84
85
86
87
88
89
90
91
92
# 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

#with_exit_on_failure {|Object| ... } ⇒ Object (protected) Originally defined in module Concern::Cli::WithExitOnFailure

Initiates termination by raising SystemExit exception depending on success of given block.

Yields:

Yield Parameters:

  • (self)

Returns:

Raises:

  • (SystemExit)