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