class PoiseRuby::Resources::BundleInstall::Provider

The default provider for the `bundle_install` resource.

@see Resource

Public Instance Methods

action_install() click to toggle source

Install bundler and the gems in the Gemfile.

# File lib/poise_ruby/resources/bundle_install.rb, line 118
def action_install
  run_bundler('install')
end
action_update() click to toggle source

Install bundler and update the gems in the Gemfile.

# File lib/poise_ruby/resources/bundle_install.rb, line 123
def action_update
  run_bundler('update')
end
bundler_binary() click to toggle source

Return the absolute path to the correct bundle binary to run.

@return [String]

# File lib/poise_ruby/resources/bundle_install.rb, line 130
def bundler_binary
  @bundler_binary ||= ::File.join(poise_gem_bindir, 'bundle')
end
gemfile_path() click to toggle source

Find the absolute path to the Gemfile. This mirrors bundler's internal search logic by scanning up to parent folder as needed.

@return [String]

# File lib/poise_ruby/resources/bundle_install.rb, line 138
def gemfile_path
  @gemfile_path ||= begin
    path = ::File.expand_path(new_resource.path)
    if ::File.file?(path)
      # We got a path to a real file, use that.
      path
    else
      # Walk back until path==dirname(path) meaning we are at the root
      while path != (next_path = ::File.dirname(path))
        possible_path = ::File.join(path, 'Gemfile')
        return possible_path if ::File.file?(possible_path)
        path = next_path
      end
    end
  end
end

Private Instance Methods

bundler_command(command) click to toggle source

Command array to run when installing the Gemfile.

@return [Array<String>]

# File lib/poise_ruby/resources/bundle_install.rb, line 218
def bundler_command(command)
  [bundler_binary, command] + bundler_options
end
bundler_options() click to toggle source

Command line options for the bundle install.

@return [Array<String>]

# File lib/poise_ruby/resources/bundle_install.rb, line 191
def bundler_options
  [].tap do |opts|
    if new_resource.binstubs
      opts << "--binstubs" + (new_resource.binstubs.is_a?(String) ? "=#{new_resource.binstubs}" : '')
    end
    if new_resource.vendor
      opts << "--path=" + (new_resource.vendor.is_a?(String) ? new_resource.vendor : 'vendor/bundle')
    end
    if new_resource.deployment
      opts << '--deployment'
    end
    if new_resource.jobs
      opts << "--jobs=#{new_resource.jobs}"
    end
    if new_resource.retry
      opts << "--retry=#{new_resource.retry}"
    end
    if new_resource.without
      opts << '--without'
      opts.insert(-1, *new_resource.without)
    end
  end
end
poise_gem_bindir() click to toggle source

Parse out the value for Gem.bindir. This is so complicated to minimize the required configuration on the resource combined with gem having terrible output formats.

Renamed from gem_bindir in 2.3.0 because of a conflict with a method of the same name in Chef::Mixin::PathSanity (which is pulled in via ShellOut) added in 13.0.

@return [String]

# File lib/poise_ruby/resources/bundle_install.rb, line 176
def poise_gem_bindir
  cmd = ruby_shell_out!(new_resource.gem_binary, 'environment')
  # Parse a line like:
  # - EXECUTABLE DIRECTORY: /usr/local/bin
  matches = cmd.stdout.scan(/EXECUTABLE DIRECTORY: (.*)$/).first
  if matches
    matches.first
  else
    raise PoiseRuby::Error.new("Cannot find EXECUTABLE DIRECTORY: #{cmd.stdout}")
  end
end
run_bundler(command) click to toggle source

Install the gems in the Gemfile.

# File lib/poise_ruby/resources/bundle_install.rb, line 158
def run_bundler(command)
  return converge_by "Run bundle #{command}" if whyrun_mode?
  cmd = ruby_shell_out!(bundler_command(command), environment: {'BUNDLE_GEMFILE' => gemfile_path}, user: new_resource.user)
  # Look for a line like 'Installing $gemname $version' to know if we did anything.
  if cmd.stdout.include?('Installing')
    new_resource.updated_by_last_action(true)
  end
end