Class | Gem::DependencyInstaller |
In: |
lib/rubygems/dependency_installer.rb
|
Parent: | Object |
Installs a gem along with all its dependencies from local and remote gems.
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:
:cache_dir: | Alternate repository path to store .gem files in. |
:domain: | :local, :remote, or :both. :local only searches gems in the current directory. :remote searches only gems in Gem::sources. :both searches both. |
:env_shebang: | See Gem::Installer::new. |
: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. |
:user_install: | See Gem::Installer.new |
:wrappers: | See Gem::Installer::new |
# File lib/rubygems/dependency_installer.rb, line 44 44: def initialize(options = {}) 45: if options[:install_dir] then 46: spec_dir = options[:install_dir], 'specifications' 47: @source_index = Gem::SourceIndex.from_gems_in spec_dir 48: else 49: @source_index = Gem.source_index 50: end 51: 52: options = DEFAULT_OPTIONS.merge options 53: 54: @bin_dir = options[:bin_dir] 55: @development = options[:development] 56: @domain = options[:domain] 57: @env_shebang = options[:env_shebang] 58: @force = options[:force] 59: @format_executable = options[:format_executable] 60: @ignore_dependencies = options[:ignore_dependencies] 61: @security_policy = options[:security_policy] 62: @user_install = options[:user_install] 63: @wrappers = options[:wrappers] 64: 65: @installed_gems = [] 66: 67: @install_dir = options[:install_dir] || Gem.dir 68: @cache_dir = options[:cache_dir] || @install_dir 69: 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 preferred over remote gems.
# File lib/rubygems/dependency_installer.rb, line 77 77: def find_gems_with_sources(dep) 78: gems_and_sources = [] 79: 80: if @domain == :both or @domain == :local then 81: Dir[File.join(Dir.pwd, "#{dep.name}-[0-9]*.gem")].each do |gem_file| 82: spec = Gem::Format.from_file_by_path(gem_file).spec 83: gems_and_sources << [spec, gem_file] if spec.name == dep.name 84: end 85: end 86: 87: if @domain == :both or @domain == :remote then 88: begin 89: requirements = dep.version_requirements.requirements.map do |req, ver| 90: req 91: end 92: 93: all = requirements.length > 1 || 94: (requirements.first != ">=" and requirements.first != ">") 95: 96: found = Gem::SpecFetcher.fetcher.fetch dep, all 97: gems_and_sources.push(*found) 98: 99: rescue Gem::RemoteFetcher::FetchError => e 100: if Gem.configuration.really_verbose then 101: say "Error fetching remote data:\t\t#{e.message}" 102: say "Falling back to local-only install" 103: end 104: @domain = :local 105: end 106: end 107: 108: gems_and_sources.sort_by do |gem, source| 109: [gem, source =~ /^http:\/\// ? 0 : 1] # local gems win 110: end 111: end
Finds a spec and the source_uri it came from for gem gem_name and version. Returns an Array of specs and sources required for installation of the gem.
# File lib/rubygems/dependency_installer.rb, line 164 164: def find_spec_by_name_and_version gem_name, version = Gem::Requirement.default 165: spec_and_source = nil 166: 167: glob = if File::ALT_SEPARATOR then 168: gem_name.gsub File::ALT_SEPARATOR, File::SEPARATOR 169: else 170: gem_name 171: end 172: 173: local_gems = Dir["#{glob}*"].sort.reverse 174: 175: unless local_gems.empty? then 176: local_gems.each do |gem_file| 177: next unless gem_file =~ /gem$/ 178: begin 179: spec = Gem::Format.from_file_by_path(gem_file).spec 180: spec_and_source = [spec, gem_file] 181: break 182: rescue SystemCallError, Gem::Package::FormatError 183: end 184: end 185: end 186: 187: if spec_and_source.nil? then 188: dep = Gem::Dependency.new gem_name, version 189: spec_and_sources = find_gems_with_sources(dep).reverse 190: 191: spec_and_source = spec_and_sources.find { |spec, source| 192: Gem::Platform.match spec.platform 193: } 194: end 195: 196: if spec_and_source.nil? then 197: raise Gem::GemNotFoundException, 198: "could not find gem #{gem_name} locally or in a repository" 199: end 200: 201: @specs_and_sources = [spec_and_source] 202: 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 117 117: def gather_dependencies 118: specs = @specs_and_sources.map { |spec,_| spec } 119: 120: dependency_list = Gem::DependencyList.new 121: dependency_list.add(*specs) 122: 123: unless @ignore_dependencies then 124: to_do = specs.dup 125: seen = {} 126: 127: until to_do.empty? do 128: spec = to_do.shift 129: next if spec.nil? or seen[spec.name] 130: seen[spec.name] = true 131: 132: deps = spec.runtime_dependencies 133: deps |= spec.development_dependencies if @development 134: 135: deps.each do |dep| 136: results = find_gems_with_sources(dep).reverse 137: 138: results.reject! do |dep_spec,| 139: to_do.push dep_spec 140: 141: @source_index.any? do |_, installed_spec| 142: dep.name == installed_spec.name and 143: dep.version_requirements.satisfied_by? installed_spec.version 144: end 145: end 146: 147: results.each do |dep_spec, source_uri| 148: next if seen[dep_spec.name] 149: @specs_and_sources << [dep_spec, source_uri] 150: dependency_list.add dep_spec 151: end 152: end 153: end 154: end 155: 156: @gems_to_install = dependency_list.dependency_order.reverse 157: end
Installs the gem and all its dependencies. Returns an Array of installed gems specifications.
# File lib/rubygems/dependency_installer.rb, line 208 208: def install dep_or_name, version = Gem::Requirement.default 209: if String === dep_or_name then 210: find_spec_by_name_and_version dep_or_name, version 211: else 212: @specs_and_sources = [find_gems_with_sources(dep_or_name).last] 213: end 214: 215: @installed_gems = [] 216: 217: gather_dependencies 218: 219: @gems_to_install.each do |spec| 220: last = spec == @gems_to_install.last 221: # HACK is this test for full_name acceptable? 222: next if @source_index.any? { |n,_| n == spec.full_name } and not last 223: 224: # TODO: make this sorta_verbose so other users can benefit from it 225: say "Installing gem #{spec.full_name}" if Gem.configuration.really_verbose 226: 227: _, source_uri = @specs_and_sources.assoc spec 228: begin 229: local_gem_path = Gem::RemoteFetcher.fetcher.download spec, source_uri, 230: @cache_dir 231: rescue Gem::RemoteFetcher::FetchError 232: next if @force 233: raise 234: end 235: 236: inst = Gem::Installer.new local_gem_path, 237: :bin_dir => @bin_dir, 238: :development => @development, 239: :env_shebang => @env_shebang, 240: :force => @force, 241: :format_executable => @format_executable, 242: :ignore_dependencies => @ignore_dependencies, 243: :install_dir => @install_dir, 244: :security_policy => @security_policy, 245: :source_index => @source_index, 246: :user_install => @user_install, 247: :wrappers => @wrappers 248: 249: spec = inst.install 250: 251: @installed_gems << spec 252: end 253: 254: @installed_gems 255: end