Class: Hanami::CLI::Bundler

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/cli/bundler.rb

Overview

Conveniences for running bundler from CLI commands.

Since:

  • 2.0.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fs: Hanami::CLI::Files.new, system_call: SystemCall.new) ⇒ Bundler

Returns a new bundler.

Parameters:

  • fs (Hanami::CLI::Files) (defaults to: Hanami::CLI::Files.new)

    the filesystem interaction object

  • system_call (SystemCall) (defaults to: SystemCall.new)

    convenience object for making system calls

Since:

  • 2.0.0



53
54
55
56
# File 'lib/hanami/cli/bundler.rb', line 53

def initialize(fs: Hanami::CLI::Files.new, system_call: SystemCall.new)
  @fs = fs
  @system_call = system_call
end

Class Method Details

.require(*groups) ⇒ void

This method returns an undefined value.

If a Gemfile exists, sets up the Bundler environment and loads all the gems from the given groups.

This can be called multiple times with different groups.

This is a convenience wrapper for Bundler.require.



40
41
42
43
44
# File 'lib/hanami/cli/bundler.rb', line 40

def self.require(*groups)
  return unless File.exist?(ENV.fetch(BUNDLE_GEMFILE) { DEFAULT_GEMFILE_PATH })

  ::Bundler.require(*groups)
end

Instance Method Details

#bundle(cmd, env: nil) ⇒ Object #bundle(cmd, env: nil, &blk) ⇒ Object

Executes the given command prefixed by bundle.

This is how you should execute all bundle subcommands.

Overloads:

  • #bundle(cmd, env: nil, &blk) ⇒ Object

    Executes the command and passes the given block to the Open3.popen3 method called internally.

    Examples:

    bundle("info") do |stdin, stdout, stderr, wait_thread|
      # ...
    end

Parameters:

  • cmd (String)

    the commands to prefix with bundle

  • env (Hash<String, String>) (defaults to: nil)

    an optional hash of environment variables to set before executing the command

See Also:

Since:

  • 2.0.0



131
132
133
134
135
136
137
138
139
140
# File 'lib/hanami/cli/bundler.rb', line 131

def bundle(cmd, env: nil, &block)
  bundle_bin = which("bundle")
  hanami_env = "HANAMI_ENV=#{env} " unless env.nil?

  system_call.call(
    "#{hanami_env}#{bundle_bin} #{cmd}",
    env: {BUNDLE_GEMFILE => fs.expand_path(DEFAULT_GEMFILE_PATH)},
    &block
  )
end

#exec(cmd, env: nil, &blk) ⇒ SystemCall::Result

Executes the given command prefixed by bundle exec.

Returns:

Since:

  • 2.0.0



104
105
106
# File 'lib/hanami/cli/bundler.rb', line 104

def exec(cmd, env: nil, &blk)
  bundle("exec #{cmd}", env: env, &blk)
end

#hanami_exec(cmd, env: nil, &blk) ⇒ SystemCall::Result

Runs the given Hanami CLI command via bundle exec hanami

Returns:

Raises:

Since:

  • 2.1.0



92
93
94
95
96
# File 'lib/hanami/cli/bundler.rb', line 92

def hanami_exec(cmd, env: nil, &blk)
  exec("hanami #{cmd}", env: env, &blk).tap do |result|
    raise HanamiExecError.new(cmd, result.err) unless result.successful?
  end
end

#installSystemCall::Result

Runs bundle install for the Hanami app.

Returns:

Since:

  • 2.0.0



64
65
66
67
# File 'lib/hanami/cli/bundler.rb', line 64

def install
  parallelism_level = Etc.nprocessors
  bundle "install --jobs=#{parallelism_level} --quiet --no-color"
end

#install!SystemCall::Result

Runs bundle install for the Hanami app and raises an error if the command does not execute successfully.

Returns:

Raises:

Since:

  • 2.0.0



78
79
80
81
82
# File 'lib/hanami/cli/bundler.rb', line 78

def install!
  install.tap do |result|
    raise BundleInstallError.new(result.err) unless result.successful?
  end
end