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, :prerelease => 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. |
:prerelease: | Allow prerelease versions. See 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 46 46: def initialize(options = {}) 47: if options[:install_dir] then 48: spec_dir = options[:install_dir], 'specifications' 49: @source_index = Gem::SourceIndex.from_gems_in spec_dir 50: else 51: @source_index = Gem.source_index 52: end 53: 54: options = DEFAULT_OPTIONS.merge options 55: 56: @bin_dir = options[:bin_dir] 57: @development = options[:development] 58: @domain = options[:domain] 59: @env_shebang = options[:env_shebang] 60: @force = options[:force] 61: @format_executable = options[:format_executable] 62: @ignore_dependencies = options[:ignore_dependencies] 63: @prerelease = options[:prerelease] 64: @security_policy = options[:security_policy] 65: @user_install = options[:user_install] 66: @wrappers = options[:wrappers] 67: 68: @installed_gems = [] 69: 70: @install_dir = options[:install_dir] || Gem.dir 71: @cache_dir = options[:cache_dir] || @install_dir 72: 73: # Set with any errors that SpecFetcher finds while search through 74: # gemspecs for a dep 75: @errors = nil 76: 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 84 84: def find_gems_with_sources(dep) 85: # Reset the errors 86: @errors = nil 87: gems_and_sources = [] 88: 89: if @domain == :both or @domain == :local then 90: Dir[File.join(Dir.pwd, "#{dep.name}-[0-9]*.gem")].each do |gem_file| 91: spec = Gem::Format.from_file_by_path(gem_file).spec 92: gems_and_sources << [spec, gem_file] if spec.name == dep.name 93: end 94: end 95: 96: if @domain == :both or @domain == :remote then 97: begin 98: requirements = dep.requirement.requirements.map do |req, ver| 99: req 100: end 101: 102: all = !dep.prerelease? && 103: # we only need latest if there's one requirement and it is 104: # guaranteed to match the newest specs 105: (requirements.length > 1 or 106: (requirements.first != ">=" and requirements.first != ">")) 107: 108: found, @errors = Gem::SpecFetcher.fetcher.fetch_with_errors dep, all, true, dep.prerelease? 109: 110: gems_and_sources.push(*found) 111: 112: rescue Gem::RemoteFetcher::FetchError => e 113: if Gem.configuration.really_verbose then 114: say "Error fetching remote data:\t\t#{e.message}" 115: say "Falling back to local-only install" 116: end 117: @domain = :local 118: end 119: end 120: 121: gems_and_sources.sort_by do |gem, source| 122: [gem, source =~ /^http:\/\// ? 0 : 1] # local gems win 123: end 124: 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 177 177: def find_spec_by_name_and_version(gem_name, 178: version = Gem::Requirement.default, 179: prerelease = false) 180: spec_and_source = nil 181: 182: glob = if File::ALT_SEPARATOR then 183: gem_name.gsub File::ALT_SEPARATOR, File::SEPARATOR 184: else 185: gem_name 186: end 187: 188: local_gems = Dir["#{glob}*"].sort.reverse 189: 190: unless local_gems.empty? then 191: local_gems.each do |gem_file| 192: next unless gem_file =~ /gem$/ 193: begin 194: spec = Gem::Format.from_file_by_path(gem_file).spec 195: spec_and_source = [spec, gem_file] 196: break 197: rescue SystemCallError, Gem::Package::FormatError 198: end 199: end 200: end 201: 202: if spec_and_source.nil? then 203: dep = Gem::Dependency.new gem_name, version 204: dep.prerelease = true if prerelease 205: spec_and_sources = find_gems_with_sources(dep).reverse 206: 207: spec_and_source = spec_and_sources.find { |spec, source| 208: Gem::Platform.match spec.platform 209: } 210: end 211: 212: if spec_and_source.nil? then 213: raise Gem::GemNotFoundException.new( 214: "Could not find a valid gem '#{gem_name}' (#{version}) locally or in a repository", 215: gem_name, version, @errors) 216: end 217: 218: @specs_and_sources = [spec_and_source] 219: 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 130 130: def gather_dependencies 131: specs = @specs_and_sources.map { |spec,_| spec } 132: 133: dependency_list = Gem::DependencyList.new @development 134: dependency_list.add(*specs) 135: 136: unless @ignore_dependencies then 137: to_do = specs.dup 138: seen = {} 139: 140: until to_do.empty? do 141: spec = to_do.shift 142: next if spec.nil? or seen[spec.name] 143: seen[spec.name] = true 144: 145: deps = spec.runtime_dependencies 146: deps |= spec.development_dependencies if @development 147: 148: deps.each do |dep| 149: results = find_gems_with_sources(dep).reverse 150: 151: results.reject! do |dep_spec,| 152: to_do.push dep_spec 153: 154: @source_index.any? do |_, installed_spec| 155: dep.name == installed_spec.name and 156: dep.requirement.satisfied_by? installed_spec.version 157: end 158: end 159: 160: results.each do |dep_spec, source_uri| 161: next if seen[dep_spec.name] 162: @specs_and_sources << [dep_spec, source_uri] 163: dependency_list.add dep_spec 164: end 165: end 166: end 167: end 168: 169: @gems_to_install = dependency_list.dependency_order.reverse 170: end
Installs the gem dep_or_name and all its dependencies. Returns an Array of installed gem specifications.
If the +:prerelease+ option is set and there is a prerelease for dep_or_name the prerelease version will be installed.
Unless explicitly specified as a prerelease dependency, prerelease gems that dep_or_name depend on will not be installed.
If c-1.a depends on b-1 and a-1.a and there is a gem b-1.a available then c-1.a, b-1 and a-1.a will be installed. b-1.a will need to be installed separately.
# File lib/rubygems/dependency_installer.rb, line 235 235: def install dep_or_name, version = Gem::Requirement.default 236: if String === dep_or_name then 237: find_spec_by_name_and_version dep_or_name, version, @prerelease 238: else 239: dep_or_name.prerelease = @prerelease 240: @specs_and_sources = [find_gems_with_sources(dep_or_name).last] 241: end 242: 243: @installed_gems = [] 244: 245: gather_dependencies 246: 247: @gems_to_install.each do |spec| 248: last = spec == @gems_to_install.last 249: # HACK is this test for full_name acceptable? 250: next if @source_index.any? { |n,_| n == spec.full_name } and not last 251: 252: # TODO: make this sorta_verbose so other users can benefit from it 253: say "Installing gem #{spec.full_name}" if Gem.configuration.really_verbose 254: 255: _, source_uri = @specs_and_sources.assoc spec 256: begin 257: local_gem_path = Gem::RemoteFetcher.fetcher.download spec, source_uri, 258: @cache_dir 259: rescue Gem::RemoteFetcher::FetchError 260: next if @force 261: raise 262: end 263: 264: inst = Gem::Installer.new local_gem_path, 265: :bin_dir => @bin_dir, 266: :development => @development, 267: :env_shebang => @env_shebang, 268: :force => @force, 269: :format_executable => @format_executable, 270: :ignore_dependencies => @ignore_dependencies, 271: :install_dir => @install_dir, 272: :security_policy => @security_policy, 273: :source_index => @source_index, 274: :user_install => @user_install, 275: :wrappers => @wrappers 276: 277: spec = inst.install 278: 279: @installed_gems << spec 280: end 281: 282: @installed_gems 283: end