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