Module: Buildr::Util::Gems
Overview
Utility methods for running gem commands
Instance Method Summary collapse
-
#command(cmd, *args) ⇒ Object
Execute a GemRunner command.
-
#install(*dependencies) ⇒ Object
Install gems specified by each Gem::Dependency if they are missing.
Instance Method Details
#command(cmd, *args) ⇒ Object
Execute a GemRunner command
174 175 176 177 178 179 180 181 182 |
# File 'lib/buildr/core/util.rb', line 174 def command(cmd, *args) = Hash === args.last ? args.pop : {} gem_home = ENV['GEM_HOME'] || Gem.path.find { |f| File.writable?(f) } [:sudo] = :root unless Util.win_os? || gem_home [:command] = 'gem' args << 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)
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/buildr/core/util.rb', line 143 def install(*dependencies) raise ArgumentError, "Expected at least one argument" if dependencies.empty? not_found_deps = [] to_install = [] remote = dependencies.each do |dep| if spec = Gem.source_index.search(dep).last # Found in local repo to_install << spec elsif (spec = Gem::SpecFetcher.fetcher.fetch(dep, true).map { |spec, source| spec }.last) # Found in remote repo to_install << spec else # Not found anywhere not_found_deps << "#{dep.name} #{dep.requirement}" end end 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 |