Module: Buildr::Util::Gems

Extended by:
Gems
Included in:
Gems
Defined in:
lib/buildr/core/util.rb

Overview

Utility methods for running gem commands

Instance Method Summary collapse

Instance Method Details

#command(cmd, *args) ⇒ Object

Execute a GemRunner command



146
147
148
149
150
151
152
153
154
# File 'lib/buildr/core/util.rb', line 146

def command(cmd, *args)
  options = Hash === args.last ? args.pop : {}
  gem_home = ENV['GEM_HOME'] || Gem.path.find { |f| File.writable?(f) }
  options[:sudo] = :root unless Util.win_os? || gem_home
  options[:command] = 'gem'
  args << options
  args.unshift '-i', gem_home if cmd == 'install' && gem_home && !args.any?{ |a| a[/-i|--install-dir/] }
  Util.ruby cmd, *args
end

#install(*dependencies) ⇒ Object

Install gems specified by each Gem::Dependency if they are missing. This method prompts the user for permission before installing anything.

Returns the installed Gem::Dependency objects or fails if permission not granted or when buildr is not running interactively (on a tty)

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/buildr/core/util.rb', line 127

def install(*dependencies)
  raise ArgumentError, "Expected at least one argument" if dependencies.empty?
  remote = dependencies.map { |dep| Gem::SourceInfoCache.search(dep).last || dep }
  not_found_deps, to_install = remote.partition { |gem| gem.is_a?(Gem::Dependency) }
  fail Gem::LoadError, "Build requires the gems #{not_found_deps.join(', ')}, which cannot be found in local or remote repository." unless not_found_deps.empty?
  uses = "This build requires the gems #{to_install.map(&:full_name).join(', ')}:"
  fail Gem::LoadError, "#{uses} to install, run Buildr interactively." unless $stdout.isatty
  unless agree("#{uses} do you want me to install them? [Y/n]", true)
    fail Gem::LoadError, 'Cannot build without these gems.'
  end
  to_install.each do |spec|
    say "Installing #{spec.full_name} ... " if verbose
    command 'install', spec.name, '-v', spec.version.to_s, :verbose => false
    Gem.source_index.load_gems_in Gem::SourceIndex.installed_spec_directories
  end
  to_install
end