Class: Gem::DependencyInstaller
- Inherits:
-
Object
- Object
- Gem::DependencyInstaller
- Includes:
- UserInteraction
- Defined in:
- lib/rubygems/dependency_installer.rb
Overview
Installs a gem along with all its dependencies from local and remote gems.
Constant Summary
- 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, }
Instance Attribute Summary (collapse)
-
- (Object) gems_to_install
readonly
Returns the value of attribute gems_to_install.
-
- (Object) installed_gems
readonly
Returns the value of attribute installed_gems.
Instance Method Summary (collapse)
- - (Object) add_found_dependencies(to_do, dependency_list)
-
- (Object) find_gems_with_sources(dep)
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.
-
- (Object) find_spec_by_name_and_version(gem_name, version = Gem::Requirement.default, prerelease = false)
Finds a spec and the source_uri it came from for gem gem_name and version.
-
- (Object) gather_dependencies
Gathers all dependencies necessary for the installation from local and remote sources unless the ignore_dependencies was given.
-
- (DependencyInstaller) initialize(options = {})
constructor
Creates a new installer instance.
-
- (Object) install(dep_or_name, version = Gem::Requirement.default)
Installs the gem dep_or_name and all its dependencies.
Methods included from UserInteraction
Methods included from DefaultUserInteraction
ui, #ui, #ui=, ui=, use_ui, #use_ui
Constructor Details
- (DependencyInstaller) initialize(options = {})
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 |
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rubygems/dependency_installer.rb', line 46 def initialize( = {}) if [:install_dir] then spec_dir = [:install_dir], 'specifications' @source_index = Gem::SourceIndex.from_gems_in spec_dir else @source_index = Gem.source_index end = DEFAULT_OPTIONS.merge @bin_dir = [:bin_dir] @development = [:development] @domain = [:domain] @env_shebang = [:env_shebang] @force = [:force] @format_executable = [:format_executable] @ignore_dependencies = [:ignore_dependencies] @prerelease = [:prerelease] @security_policy = [:security_policy] @user_install = [:user_install] @wrappers = [:wrappers] @installed_gems = [] @install_dir = [:install_dir] || Gem.dir @cache_dir = [:cache_dir] || @install_dir # Set with any errors that SpecFetcher finds while search through # gemspecs for a dep @errors = nil end |
Instance Attribute Details
- (Object) gems_to_install (readonly)
Returns the value of attribute gems_to_install
14 15 16 |
# File 'lib/rubygems/dependency_installer.rb', line 14 def gems_to_install @gems_to_install end |
- (Object) installed_gems (readonly)
Returns the value of attribute installed_gems
15 16 17 |
# File 'lib/rubygems/dependency_installer.rb', line 15 def installed_gems @installed_gems end |
Instance Method Details
- (Object) add_found_dependencies(to_do, dependency_list)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rubygems/dependency_installer.rb', line 142 def add_found_dependencies to_do, dependency_list seen = {} until to_do.empty? do spec = to_do.shift next if spec.nil? or seen[spec.name] seen[spec.name] = true deps = spec.runtime_dependencies deps |= spec.development_dependencies if @development deps.each do |dep| results = find_gems_with_sources(dep).reverse # FIX: throw in everything that satisfies, and let # FIX: dependencylist reduce to the chosen few results.reject! do |dep_spec,| to_do.push dep_spec # already locally installed @source_index.any? do |_, installed_spec| dep.name == installed_spec.name and dep.requirement.satisfied_by? installed_spec.version end end results.each do |dep_spec, source_uri| next if seen[dep_spec.name] @specs_and_sources << [dep_spec, source_uri] # FIX: this is the bug dependency_list.add dep_spec end end end end |
- (Object) find_gems_with_sources(dep)
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.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rubygems/dependency_installer.rb', line 84 def find_gems_with_sources(dep) # Reset the errors @errors = nil gems_and_sources = [] if @domain == :both or @domain == :local then Dir[File.join(Dir.pwd, "#{dep.name}-[0-9]*.gem")].each do |gem_file| spec = Gem::Format.from_file_by_path(gem_file).spec gems_and_sources << [spec, gem_file] if spec.name == dep.name end end if @domain == :both or @domain == :remote then begin requirements = dep.requirement.requirements.map do |req, ver| req end all = !dep.prerelease? && # we only need latest if there's one requirement and it is # guaranteed to match the newest specs (requirements.length > 1 or (requirements.first != ">=" and requirements.first != ">")) found, @errors = Gem::SpecFetcher.fetcher.fetch_with_errors dep, all, true, dep.prerelease? gems_and_sources.push(*found) rescue Gem::RemoteFetcher::FetchError => e if Gem.configuration.really_verbose then say "Error fetching remote data:\t\t#{e.}" say "Falling back to local-only install" end @domain = :local end end gems_and_sources.sort_by do |gem, source| [gem, source =~ /^http:\/\// ? 0 : 1] # local gems win end end |
- (Object) find_spec_by_name_and_version(gem_name, version = Gem::Requirement.default, prerelease = false)
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.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/rubygems/dependency_installer.rb', line 184 def find_spec_by_name_and_version(gem_name, version = Gem::Requirement.default, prerelease = false) spec_and_source = nil glob = if File::ALT_SEPARATOR then gem_name.gsub File::ALT_SEPARATOR, File::SEPARATOR else gem_name end local_gems = Dir["#{glob}*"].sort.reverse unless local_gems.empty? then local_gems.each do |gem_file| next unless gem_file =~ /gem$/ begin spec = Gem::Format.from_file_by_path(gem_file).spec spec_and_source = [spec, gem_file] break rescue SystemCallError, Gem::Package::FormatError end end end if spec_and_source.nil? then dep = Gem::Dependency.new gem_name, version dep.prerelease = true if prerelease spec_and_sources = find_gems_with_sources(dep).reverse spec_and_source = spec_and_sources.find { |spec, source| Gem::Platform.match spec.platform } end if spec_and_source.nil? then raise Gem::GemNotFoundException.new( "Could not find a valid gem '#{gem_name}' (#{version}) locally or in a repository", gem_name, version, @errors) end @specs_and_sources = [spec_and_source] end |
- (Object) gather_dependencies
Gathers all dependencies necessary for the installation from local and remote sources unless the ignore_dependencies was given.
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/rubygems/dependency_installer.rb', line 130 def gather_dependencies specs = @specs_and_sources.map { |spec,_| spec } dependency_list = Gem::DependencyList.new @development dependency_list.add(*specs) to_do = specs.dup add_found_dependencies to_do, dependency_list unless @ignore_dependencies @gems_to_install = dependency_list.dependency_order.reverse end |
- (Object) install(dep_or_name, version = Gem::Requirement.default)
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.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/rubygems/dependency_installer.rb', line 242 def install dep_or_name, version = Gem::Requirement.default if String === dep_or_name then find_spec_by_name_and_version dep_or_name, version, @prerelease else dep_or_name.prerelease = @prerelease @specs_and_sources = [find_gems_with_sources(dep_or_name).last] end @installed_gems = [] gather_dependencies @gems_to_install.each do |spec| last = spec == @gems_to_install.last # HACK is this test for full_name acceptable? next if @source_index.any? { |n,_| n == spec.full_name } and not last # TODO: make this sorta_verbose so other users can benefit from it say "Installing gem #{spec.full_name}" if Gem.configuration.really_verbose _, source_uri = @specs_and_sources.assoc spec begin local_gem_path = Gem::RemoteFetcher.fetcher.download spec, source_uri, @cache_dir rescue Gem::RemoteFetcher::FetchError next if @force raise end inst = Gem::Installer.new local_gem_path, :bin_dir => @bin_dir, :development => @development, :env_shebang => @env_shebang, :force => @force, :format_executable => @format_executable, :ignore_dependencies => @ignore_dependencies, :install_dir => @install_dir, :security_policy => @security_policy, :source_index => @source_index, :user_install => @user_install, :wrappers => @wrappers spec = inst.install @installed_gems << spec end @installed_gems end |