Class | Gem::DependencyInstaller |
In: |
lib/rubygems/dependency_installer.rb
|
Parent: | Object |
DEFAULT_OPTIONS | = | { :env_shebang => false, :domain => :both, # HACK dup :force => false, :format_executable => false, # HACK dup :ignore_dependencies => false, :security_policy => nil, # HACK NoSecurity requires OpenSSL. AlmostNo? Low? :wrappers => true |
gems_to_install | [R] | |
installed_gems | [R] |
Creates a new installer instance.
Options are:
:env_shebang: | See Gem::Installer::new. |
:domain: | :local, :remote, or :both. :local only searches gems in the current directory. :remote searches only gems in Gem::sources. :both searches both. |
:force: | See Gem::Installer#install. |
:format_executable: | See Gem::Installer#initialize. |
:ignore_dependencies: Don‘t install any dependencies. :install_dir: See Gem::Installer#install. :security_policy: See Gem::Installer::new and Gem::Security. :wrappers: See Gem::Installer::new
# File lib/rubygems/dependency_installer.rb, line 38 38: def initialize(options = {}) 39: options = DEFAULT_OPTIONS.merge options 40: @env_shebang = options[:env_shebang] 41: @domain = options[:domain] 42: @force = options[:force] 43: @format_executable = options[:format_executable] 44: @ignore_dependencies = options[:ignore_dependencies] 45: @install_dir = options[:install_dir] || Gem.dir 46: @security_policy = options[:security_policy] 47: @wrappers = options[:wrappers] 48: @bin_dir = options[:bin_dir] 49: 50: @installed_gems = [] 51: end
Returns a list of pairs of gemspecs and source_uris that match Gem::Dependency dep from both local (Dir.pwd) and remote (Gem.sources) sources. Gems are sorted with newer gems prefered over older gems, and local gems prefered over remote gems.
# File lib/rubygems/dependency_installer.rb, line 58 58: def find_gems_with_sources(dep) 59: gems_and_sources = [] 60: 61: if @domain == :both or @domain == :local then 62: Dir[File.join(Dir.pwd, "#{dep.name}-[0-9]*.gem")].each do |gem_file| 63: spec = Gem::Format.from_file_by_path(gem_file).spec 64: gems_and_sources << [spec, gem_file] if spec.name == dep.name 65: end 66: end 67: 68: if @domain == :both or @domain == :remote then 69: begin 70: requirements = dep.version_requirements.requirements.map do |req, ver| 71: req 72: end 73: 74: all = requirements.length > 1 || 75: (requirements.first != ">=" and requirements.first != ">") 76: 77: found = Gem::SourceInfoCache.search_with_source dep, true, all 78: 79: gems_and_sources.push(*found) 80: 81: rescue Gem::RemoteFetcher::FetchError => e 82: if Gem.configuration.really_verbose then 83: say "Error fetching remote data:\t\t#{e.message}" 84: say "Falling back to local-only install" 85: end 86: @domain = :local 87: end 88: end 89: 90: gems_and_sources.sort_by do |gem, source| 91: [gem, source =~ /^http:\/\// ? 0 : 1] # local gems win 92: end 93: end
# File lib/rubygems/dependency_installer.rb, line 129 129: def find_spec_by_name_and_version gem_name, version = Gem::Requirement.default 130: spec_and_source = nil 131: 132: glob = if File::ALT_SEPARATOR then 133: gem_name.gsub File::ALT_SEPARATOR, File::SEPARATOR 134: else 135: gem_name 136: end 137: 138: local_gems = Dir["#{glob}*"].sort.reverse 139: 140: unless local_gems.empty? then 141: local_gems.each do |gem_file| 142: next unless gem_file =~ /gem$/ 143: begin 144: spec = Gem::Format.from_file_by_path(gem_file).spec 145: spec_and_source = [spec, gem_file] 146: break 147: rescue SystemCallError, Gem::Package::FormatError 148: end 149: end 150: end 151: 152: if spec_and_source.nil? then 153: dep = Gem::Dependency.new gem_name, version 154: spec_and_sources = find_gems_with_sources(dep).reverse 155: 156: spec_and_source = spec_and_sources.find { |spec, source| 157: Gem::Platform.match spec.platform 158: } 159: end 160: 161: if spec_and_source.nil? then 162: raise Gem::GemNotFoundException, 163: "could not find #{gem_name} locally or in a repository" 164: end 165: 166: @specs_and_sources = [spec_and_source] 167: end
Gathers all dependencies necessary for the installation from local and remote sources unless the ignore_dependencies was given.
# File lib/rubygems/dependency_installer.rb, line 98 98: def gather_dependencies 99: specs = @specs_and_sources.map { |spec,_| spec } 100: 101: dependency_list = Gem::DependencyList.new 102: dependency_list.add(*specs) 103: 104: unless @ignore_dependencies then 105: to_do = specs.dup 106: seen = {} 107: 108: until to_do.empty? do 109: spec = to_do.shift 110: next if spec.nil? or seen[spec.name] 111: seen[spec.name] = true 112: 113: spec.dependencies.each do |dep| 114: results = find_gems_with_sources(dep).reverse # local gems first 115: 116: results.each do |dep_spec, source_uri| 117: next if seen[dep_spec.name] 118: @specs_and_sources << [dep_spec, source_uri] 119: dependency_list.add dep_spec 120: to_do.push dep_spec 121: end 122: end 123: end 124: end 125: 126: @gems_to_install = dependency_list.dependency_order.reverse 127: end
Installs the gem and all its dependencies.
# File lib/rubygems/dependency_installer.rb, line 171 171: def install dep_or_name, version = Gem::Requirement.default 172: if String === dep_or_name then 173: find_spec_by_name_and_version dep_or_name, version 174: else 175: @specs_and_sources = [find_gems_with_sources(dep_or_name).last] 176: end 177: 178: gather_dependencies 179: 180: spec_dir = File.join @install_dir, 'specifications' 181: source_index = Gem::SourceIndex.from_gems_in spec_dir 182: 183: @gems_to_install.each do |spec| 184: last = spec == @gems_to_install.last 185: # HACK is this test for full_name acceptable? 186: next if source_index.any? { |n,_| n == spec.full_name } and not last 187: 188: # TODO: make this sorta_verbose so other users can benefit from it 189: say "Installing gem #{spec.full_name}" if Gem.configuration.really_verbose 190: 191: _, source_uri = @specs_and_sources.assoc spec 192: begin 193: local_gem_path = Gem::RemoteFetcher.fetcher.download spec, source_uri, 194: @install_dir 195: rescue Gem::RemoteFetcher::FetchError 196: next if @force 197: raise 198: end 199: 200: inst = Gem::Installer.new local_gem_path, 201: :env_shebang => @env_shebang, 202: :force => @force, 203: :format_executable => @format_executable, 204: :ignore_dependencies => @ignore_dependencies, 205: :install_dir => @install_dir, 206: :security_policy => @security_policy, 207: :wrappers => @wrappers, 208: :bin_dir => @bin_dir 209: 210: spec = inst.install 211: 212: @installed_gems << spec 213: end 214: end