rkh-bundler 1.2.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. data/.gitignore +22 -0
  2. data/.travis.yml +42 -0
  3. data/CHANGELOG.md +1105 -0
  4. data/ISSUES.md +67 -0
  5. data/LICENSE +23 -0
  6. data/README.md +31 -0
  7. data/Rakefile +208 -0
  8. data/UPGRADING.md +103 -0
  9. data/bin/bundle +31 -0
  10. data/bundler.gemspec +31 -0
  11. data/lib/bundler.rb +353 -0
  12. data/lib/bundler/capistrano.rb +11 -0
  13. data/lib/bundler/cli.rb +693 -0
  14. data/lib/bundler/definition.rb +568 -0
  15. data/lib/bundler/dep_proxy.rb +43 -0
  16. data/lib/bundler/dependency.rb +134 -0
  17. data/lib/bundler/deployment.rb +58 -0
  18. data/lib/bundler/dsl.rb +256 -0
  19. data/lib/bundler/endpoint_specification.rb +78 -0
  20. data/lib/bundler/environment.rb +47 -0
  21. data/lib/bundler/fetcher.rb +225 -0
  22. data/lib/bundler/gem_helper.rb +162 -0
  23. data/lib/bundler/gem_helpers.rb +23 -0
  24. data/lib/bundler/gem_installer.rb +9 -0
  25. data/lib/bundler/gem_tasks.rb +2 -0
  26. data/lib/bundler/graph.rb +148 -0
  27. data/lib/bundler/index.rb +187 -0
  28. data/lib/bundler/installer.rb +190 -0
  29. data/lib/bundler/lazy_specification.rb +79 -0
  30. data/lib/bundler/lockfile_parser.rb +127 -0
  31. data/lib/bundler/match_platform.rb +13 -0
  32. data/lib/bundler/psyched_yaml.rb +15 -0
  33. data/lib/bundler/remote_specification.rb +57 -0
  34. data/lib/bundler/resolver.rb +486 -0
  35. data/lib/bundler/ruby_version.rb +94 -0
  36. data/lib/bundler/rubygems_ext.rb +153 -0
  37. data/lib/bundler/rubygems_integration.rb +394 -0
  38. data/lib/bundler/runtime.rb +233 -0
  39. data/lib/bundler/settings.rb +128 -0
  40. data/lib/bundler/setup.rb +23 -0
  41. data/lib/bundler/shared_helpers.rb +71 -0
  42. data/lib/bundler/source.rb +869 -0
  43. data/lib/bundler/spec_set.rb +137 -0
  44. data/lib/bundler/templates/Executable +16 -0
  45. data/lib/bundler/templates/Executable.standalone +12 -0
  46. data/lib/bundler/templates/Gemfile +4 -0
  47. data/lib/bundler/templates/newgem/Gemfile.tt +4 -0
  48. data/lib/bundler/templates/newgem/LICENSE.tt +22 -0
  49. data/lib/bundler/templates/newgem/README.md.tt +29 -0
  50. data/lib/bundler/templates/newgem/Rakefile.tt +2 -0
  51. data/lib/bundler/templates/newgem/bin/newgem.tt +3 -0
  52. data/lib/bundler/templates/newgem/gitignore.tt +17 -0
  53. data/lib/bundler/templates/newgem/lib/newgem.rb.tt +9 -0
  54. data/lib/bundler/templates/newgem/lib/newgem/version.rb.tt +7 -0
  55. data/lib/bundler/templates/newgem/newgem.gemspec.tt +17 -0
  56. data/lib/bundler/ui.rb +88 -0
  57. data/lib/bundler/vendor/net/http/faster.rb +27 -0
  58. data/lib/bundler/vendor/net/http/persistent.rb +468 -0
  59. data/lib/bundler/vendor/thor.rb +358 -0
  60. data/lib/bundler/vendor/thor/actions.rb +314 -0
  61. data/lib/bundler/vendor/thor/actions/create_file.rb +105 -0
  62. data/lib/bundler/vendor/thor/actions/create_link.rb +57 -0
  63. data/lib/bundler/vendor/thor/actions/directory.rb +93 -0
  64. data/lib/bundler/vendor/thor/actions/empty_directory.rb +134 -0
  65. data/lib/bundler/vendor/thor/actions/file_manipulation.rb +270 -0
  66. data/lib/bundler/vendor/thor/actions/inject_into_file.rb +109 -0
  67. data/lib/bundler/vendor/thor/base.rb +576 -0
  68. data/lib/bundler/vendor/thor/core_ext/file_binary_read.rb +9 -0
  69. data/lib/bundler/vendor/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  70. data/lib/bundler/vendor/thor/core_ext/ordered_hash.rb +100 -0
  71. data/lib/bundler/vendor/thor/error.rb +30 -0
  72. data/lib/bundler/vendor/thor/group.rb +273 -0
  73. data/lib/bundler/vendor/thor/invocation.rb +168 -0
  74. data/lib/bundler/vendor/thor/parser.rb +4 -0
  75. data/lib/bundler/vendor/thor/parser/argument.rb +67 -0
  76. data/lib/bundler/vendor/thor/parser/arguments.rb +161 -0
  77. data/lib/bundler/vendor/thor/parser/option.rb +120 -0
  78. data/lib/bundler/vendor/thor/parser/options.rb +172 -0
  79. data/lib/bundler/vendor/thor/rake_compat.rb +66 -0
  80. data/lib/bundler/vendor/thor/runner.rb +309 -0
  81. data/lib/bundler/vendor/thor/shell.rb +88 -0
  82. data/lib/bundler/vendor/thor/shell/basic.rb +302 -0
  83. data/lib/bundler/vendor/thor/shell/color.rb +108 -0
  84. data/lib/bundler/vendor/thor/shell/html.rb +121 -0
  85. data/lib/bundler/vendor/thor/task.rb +113 -0
  86. data/lib/bundler/vendor/thor/util.rb +229 -0
  87. data/lib/bundler/vendor/thor/version.rb +3 -0
  88. data/lib/bundler/vendored_thor.rb +7 -0
  89. data/lib/bundler/version.rb +6 -0
  90. data/lib/bundler/vlad.rb +11 -0
  91. data/man/bundle-config.ronn +130 -0
  92. data/man/bundle-exec.ronn +111 -0
  93. data/man/bundle-install.ronn +335 -0
  94. data/man/bundle-package.ronn +59 -0
  95. data/man/bundle-update.ronn +176 -0
  96. data/man/bundle.ronn +83 -0
  97. data/man/gemfile.5.ronn +324 -0
  98. data/man/index.txt +6 -0
  99. data/spec/bundler/dsl_spec.rb +48 -0
  100. data/spec/bundler/gem_helper_spec.rb +174 -0
  101. data/spec/bundler/source_spec.rb +25 -0
  102. data/spec/cache/gems_spec.rb +239 -0
  103. data/spec/cache/git_spec.rb +124 -0
  104. data/spec/cache/path_spec.rb +103 -0
  105. data/spec/cache/platform_spec.rb +57 -0
  106. data/spec/install/deploy_spec.rb +211 -0
  107. data/spec/install/gems/c_ext_spec.rb +48 -0
  108. data/spec/install/gems/dependency_api_spec.rb +402 -0
  109. data/spec/install/gems/env_spec.rb +107 -0
  110. data/spec/install/gems/flex_spec.rb +313 -0
  111. data/spec/install/gems/groups_spec.rb +268 -0
  112. data/spec/install/gems/packed_spec.rb +84 -0
  113. data/spec/install/gems/platform_spec.rb +208 -0
  114. data/spec/install/gems/post_install_spec.rb +47 -0
  115. data/spec/install/gems/resolving_spec.rb +72 -0
  116. data/spec/install/gems/simple_case_spec.rb +814 -0
  117. data/spec/install/gems/standalone_spec.rb +260 -0
  118. data/spec/install/gems/sudo_spec.rb +74 -0
  119. data/spec/install/gems/win32_spec.rb +26 -0
  120. data/spec/install/gemspec_spec.rb +170 -0
  121. data/spec/install/git_spec.rb +796 -0
  122. data/spec/install/invalid_spec.rb +35 -0
  123. data/spec/install/path_spec.rb +405 -0
  124. data/spec/install/upgrade_spec.rb +26 -0
  125. data/spec/lock/git_spec.rb +35 -0
  126. data/spec/lock/lockfile_spec.rb +809 -0
  127. data/spec/other/check_spec.rb +265 -0
  128. data/spec/other/clean_spec.rb +492 -0
  129. data/spec/other/config_spec.rb +138 -0
  130. data/spec/other/console_spec.rb +54 -0
  131. data/spec/other/exec_spec.rb +229 -0
  132. data/spec/other/ext_spec.rb +37 -0
  133. data/spec/other/help_spec.rb +39 -0
  134. data/spec/other/init_spec.rb +40 -0
  135. data/spec/other/newgem_spec.rb +87 -0
  136. data/spec/other/open_spec.rb +35 -0
  137. data/spec/other/outdated_spec.rb +93 -0
  138. data/spec/other/platform_spec.rb +881 -0
  139. data/spec/other/show_spec.rb +88 -0
  140. data/spec/quality_spec.rb +62 -0
  141. data/spec/realworld/edgecases_spec.rb +177 -0
  142. data/spec/resolver/basic_spec.rb +20 -0
  143. data/spec/resolver/platform_spec.rb +82 -0
  144. data/spec/runtime/executable_spec.rb +120 -0
  145. data/spec/runtime/load_spec.rb +107 -0
  146. data/spec/runtime/platform_spec.rb +90 -0
  147. data/spec/runtime/require_spec.rb +261 -0
  148. data/spec/runtime/setup_spec.rb +755 -0
  149. data/spec/runtime/with_clean_env_spec.rb +80 -0
  150. data/spec/spec_helper.rb +98 -0
  151. data/spec/support/artifice/endopint_marshal_fail_basic_authentication.rb +13 -0
  152. data/spec/support/artifice/endpoint.rb +54 -0
  153. data/spec/support/artifice/endpoint_500.rb +37 -0
  154. data/spec/support/artifice/endpoint_api_missing.rb +16 -0
  155. data/spec/support/artifice/endpoint_basic_authentication.rb +13 -0
  156. data/spec/support/artifice/endpoint_extra.rb +27 -0
  157. data/spec/support/artifice/endpoint_extra_missing.rb +15 -0
  158. data/spec/support/artifice/endpoint_fallback.rb +18 -0
  159. data/spec/support/artifice/endpoint_marshal_fail.rb +11 -0
  160. data/spec/support/artifice/endpoint_redirect.rb +15 -0
  161. data/spec/support/builders.rb +604 -0
  162. data/spec/support/fakeweb/rack-1.0.0.marshal +2 -0
  163. data/spec/support/fakeweb/windows.rb +23 -0
  164. data/spec/support/helpers.rb +317 -0
  165. data/spec/support/indexes.rb +112 -0
  166. data/spec/support/matchers.rb +77 -0
  167. data/spec/support/path.rb +73 -0
  168. data/spec/support/platforms.rb +86 -0
  169. data/spec/support/ruby_ext.rb +20 -0
  170. data/spec/support/rubygems_ext.rb +37 -0
  171. data/spec/support/rubygems_hax/platform.rb +22 -0
  172. data/spec/support/sudo.rb +21 -0
  173. data/spec/update/gems_spec.rb +134 -0
  174. data/spec/update/git_spec.rb +196 -0
  175. data/spec/update/source_spec.rb +51 -0
  176. metadata +338 -0
@@ -0,0 +1,79 @@
1
+ require "uri"
2
+ require "rubygems/spec_fetcher"
3
+
4
+ module Bundler
5
+ class LazySpecification
6
+ include MatchPlatform
7
+
8
+ attr_reader :name, :version, :dependencies, :platform
9
+ attr_accessor :source
10
+
11
+ def initialize(name, version, platform, source = nil)
12
+ @name = name
13
+ @version = version
14
+ @dependencies = []
15
+ @platform = platform
16
+ @source = source
17
+ @specification = nil
18
+ end
19
+
20
+ def full_name
21
+ if platform == Gem::Platform::RUBY or platform.nil? then
22
+ "#{@name}-#{@version}"
23
+ else
24
+ "#{@name}-#{@version}-#{platform}"
25
+ end
26
+ end
27
+
28
+ def ==(other)
29
+ [name, version, dependencies, platform, source] ==
30
+ [other.name, other.version, other.dependencies, other.platform, other.source]
31
+ end
32
+
33
+ def satisfies?(dependency)
34
+ @name == dependency.name && dependency.requirement.satisfied_by?(Gem::Version.new(@version))
35
+ end
36
+
37
+ def to_lock
38
+ if platform == Gem::Platform::RUBY or platform.nil?
39
+ out = " #{name} (#{version})\n"
40
+ else
41
+ out = " #{name} (#{version}-#{platform})\n"
42
+ end
43
+
44
+ dependencies.sort_by {|d| d.to_s }.each do |dep|
45
+ next if dep.type == :development
46
+ out << " #{dep.to_lock}\n"
47
+ end
48
+
49
+ out
50
+ end
51
+
52
+ def __materialize__
53
+ @specification = source.specs.search(Gem::Dependency.new(name, version)).last
54
+ end
55
+
56
+ def respond_to?(*args)
57
+ super || @specification.respond_to?(*args)
58
+ end
59
+
60
+ def to_s
61
+ "#{name} (#{version})"
62
+ end
63
+
64
+ private
65
+
66
+ def to_ary
67
+ nil
68
+ end
69
+
70
+ def method_missing(method, *args, &blk)
71
+ raise "LazySpecification has not been materialized yet (calling :#{method} #{args.inspect})" unless @specification
72
+
73
+ return super unless respond_to?(method)
74
+
75
+ @specification.send(method, *args, &blk)
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,127 @@
1
+ require "strscan"
2
+
3
+ # Some versions of the Bundler 1.1 RC series introduced corrupted
4
+ # lockfiles. There were two major problems:
5
+ #
6
+ # * multiple copies of the same GIT section appeared in the lockfile
7
+ # * when this happened, those sections got multiple copies of gems
8
+ # in those sections.
9
+ #
10
+ # As a result, Bundler 1.1 contains code that fixes the earlier
11
+ # corruption. We will remove this fix-up code in Bundler 1.2.
12
+
13
+ module Bundler
14
+ class LockfileParser
15
+ attr_reader :sources, :dependencies, :specs, :platforms
16
+
17
+ def initialize(lockfile)
18
+ @platforms = []
19
+ @sources = []
20
+ @dependencies = []
21
+ @specs = []
22
+ @state = :source
23
+
24
+ lockfile.split(/(\r?\n)+/).each do |line|
25
+ if line == "DEPENDENCIES"
26
+ @state = :dependency
27
+ elsif line == "PLATFORMS"
28
+ @state = :platform
29
+ else
30
+ send("parse_#{@state}", line)
31
+ end
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ TYPES = {
38
+ "GIT" => Bundler::Source::Git,
39
+ "GEM" => Bundler::Source::Rubygems,
40
+ "PATH" => Bundler::Source::Path
41
+ }
42
+
43
+ def parse_source(line)
44
+ case line
45
+ when "GIT", "GEM", "PATH"
46
+ @current_source = nil
47
+ @opts, @type = {}, line
48
+ when " specs:"
49
+ @current_source = TYPES[@type].from_lock(@opts)
50
+
51
+ # Strip out duplicate GIT sections
52
+ if @sources.include?(@current_source) && @current_source.is_a?(Bundler::Source::Git)
53
+ @current_source = @sources.find { |s| s == @current_source }
54
+ end
55
+
56
+ @sources << @current_source
57
+ when /^ ([a-z]+): (.*)$/i
58
+ value = $2
59
+ value = true if value == "true"
60
+ value = false if value == "false"
61
+
62
+ key = $1
63
+
64
+ if @opts[key]
65
+ @opts[key] = Array(@opts[key])
66
+ @opts[key] << value
67
+ else
68
+ @opts[key] = value
69
+ end
70
+ else
71
+ parse_spec(line)
72
+ end
73
+ end
74
+
75
+ NAME_VERSION = '(?! )(.*?)(?: \(([^-]*)(?:-(.*))?\))?'
76
+
77
+ def parse_dependency(line)
78
+ if line =~ %r{^ {2}#{NAME_VERSION}(!)?$}
79
+ name, version, pinned = $1, $2, $4
80
+ version = version.split(",").map { |d| d.strip } if version
81
+
82
+ dep = Bundler::Dependency.new(name, version)
83
+
84
+ if pinned && dep.name != 'bundler'
85
+ spec = @specs.find { |s| s.name == dep.name }
86
+ dep.source = spec.source if spec
87
+
88
+ # Path sources need to know what the default name / version
89
+ # to use in the case that there are no gemspecs present. A fake
90
+ # gemspec is created based on the version set on the dependency
91
+ # TODO: Use the version from the spec instead of from the dependency
92
+ if version && version.size == 1 && version.first =~ /^\s*= (.+)\s*$/ && dep.source.is_a?(Bundler::Source::Path)
93
+ dep.source.name = name
94
+ dep.source.version = $1
95
+ end
96
+ end
97
+
98
+ @dependencies << dep
99
+ end
100
+ end
101
+
102
+ def parse_spec(line)
103
+ if line =~ %r{^ {4}#{NAME_VERSION}$}
104
+ name, version = $1, Gem::Version.new($2)
105
+ platform = $3 ? Gem::Platform.new($3) : Gem::Platform::RUBY
106
+ @current_spec = LazySpecification.new(name, version, platform)
107
+ @current_spec.source = @current_source
108
+
109
+ # Avoid introducing multiple copies of the same spec (caused by
110
+ # duplicate GIT sections)
111
+ @specs << @current_spec unless @specs.include?(@current_spec)
112
+ elsif line =~ %r{^ {6}#{NAME_VERSION}$}
113
+ name, version = $1, $2
114
+ version = version.split(',').map { |d| d.strip } if version
115
+ dep = Gem::Dependency.new(name, version)
116
+ @current_spec.dependencies << dep
117
+ end
118
+ end
119
+
120
+ def parse_platform(line)
121
+ if line =~ /^ (.*)$/
122
+ @platforms << Gem::Platform.new($1)
123
+ end
124
+ end
125
+
126
+ end
127
+ end
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_helpers'
2
+
3
+ module Bundler
4
+ module MatchPlatform
5
+ include GemHelpers
6
+
7
+ def match_platform(p)
8
+ Gem::Platform::RUBY == platform or
9
+ platform.nil? or p == platform or
10
+ generic(Gem::Platform.new(platform)) == p
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ # Psych could be a gem
2
+ begin
3
+ gem 'psych'
4
+ rescue Gem::LoadError
5
+ end if defined?(Gem)
6
+
7
+ # Psych could be a stdlib
8
+ begin
9
+ # it's too late if Syck is already loaded
10
+ require 'psych' unless defined?(Syck)
11
+ rescue LoadError
12
+ end
13
+
14
+ # Psych might NOT EXIST AT ALL
15
+ require 'yaml'
@@ -0,0 +1,57 @@
1
+ require "uri"
2
+ require "rubygems/spec_fetcher"
3
+
4
+ module Bundler
5
+ # Represents a lazily loaded gem specification, where the full specification
6
+ # is on the source server in rubygems' "quick" index. The proxy object is to
7
+ # be seeded with what we're given from the source's abbreviated index - the
8
+ # full specification will only be fetched when necesary.
9
+ class RemoteSpecification
10
+ include MatchPlatform
11
+
12
+ attr_reader :name, :version, :platform
13
+ attr_accessor :source
14
+
15
+ def initialize(name, version, platform, spec_fetcher)
16
+ @name = name
17
+ @version = version
18
+ @platform = platform
19
+ @spec_fetcher = spec_fetcher
20
+ end
21
+
22
+ # Needed before installs, since the arch matters then and quick
23
+ # specs don't bother to include the arch in the platform string
24
+ def fetch_platform
25
+ @platform = _remote_specification.platform
26
+ end
27
+
28
+ def full_name
29
+ if platform == Gem::Platform::RUBY or platform.nil? then
30
+ "#{@name}-#{@version}"
31
+ else
32
+ "#{@name}-#{@version}-#{platform}"
33
+ end
34
+ end
35
+
36
+ # Because Rubyforge cannot be trusted to provide valid specifications
37
+ # once the remote gem is downloaded, the backend specification will
38
+ # be swapped out.
39
+ def __swap__(spec)
40
+ @specification = spec
41
+ end
42
+
43
+ private
44
+
45
+ def _remote_specification
46
+ @specification ||= @spec_fetcher.fetch_spec([@name, @version, @platform])
47
+ end
48
+
49
+ def method_missing(method, *args, &blk)
50
+ if Gem::Specification.new.respond_to?(method)
51
+ _remote_specification.send(method, *args, &blk)
52
+ else
53
+ super
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,486 @@
1
+ require 'set'
2
+ # This is the latest iteration of the gem dependency resolving algorithm. As of now,
3
+ # it can resolve (as a success or failure) any set of gem dependencies we throw at it
4
+ # in a reasonable amount of time. The most iterations I've seen it take is about 150.
5
+ # The actual implementation of the algorithm is not as good as it could be yet, but that
6
+ # can come later.
7
+
8
+ # Extending Gem classes to add necessary tracking information
9
+ module Gem
10
+ class Specification
11
+ def required_by
12
+ @required_by ||= []
13
+ end
14
+ end
15
+ class Dependency
16
+ def required_by
17
+ @required_by ||= []
18
+ end
19
+ end
20
+ end
21
+
22
+ module Bundler
23
+ class Resolver
24
+ ALL = Bundler::Dependency::PLATFORM_MAP.values.uniq.freeze
25
+
26
+ class SpecGroup < Array
27
+ include GemHelpers
28
+
29
+ attr_reader :activated, :required_by
30
+
31
+ def initialize(a)
32
+ super
33
+ @required_by = []
34
+ @activated = []
35
+ @dependencies = nil
36
+ @specs = {}
37
+
38
+ ALL.each do |p|
39
+ @specs[p] = reverse.find { |s| s.match_platform(p) }
40
+ end
41
+ end
42
+
43
+ def initialize_copy(o)
44
+ super
45
+ @required_by = o.required_by.dup
46
+ @activated = o.activated.dup
47
+ end
48
+
49
+ def to_specs
50
+ specs = {}
51
+
52
+ @activated.each do |p|
53
+ if s = @specs[p]
54
+ platform = generic(Gem::Platform.new(s.platform))
55
+ next if specs[platform]
56
+
57
+ lazy_spec = LazySpecification.new(name, version, platform, source)
58
+ lazy_spec.dependencies.replace s.dependencies
59
+ specs[platform] = lazy_spec
60
+ end
61
+ end
62
+ specs.values
63
+ end
64
+
65
+ def activate_platform(platform)
66
+ unless @activated.include?(platform)
67
+ @activated << platform
68
+ return __dependencies[platform] || []
69
+ end
70
+ []
71
+ end
72
+
73
+ def name
74
+ @name ||= first.name
75
+ end
76
+
77
+ def version
78
+ @version ||= first.version
79
+ end
80
+
81
+ def source
82
+ @source ||= first.source
83
+ end
84
+
85
+ def for?(platform)
86
+ @specs[platform]
87
+ end
88
+
89
+ def to_s
90
+ "#{name} (#{version})"
91
+ end
92
+
93
+ private
94
+
95
+ def __dependencies
96
+ @dependencies ||= begin
97
+ dependencies = {}
98
+ ALL.each do |p|
99
+ if spec = @specs[p]
100
+ dependencies[p] = []
101
+ spec.dependencies.each do |dep|
102
+ next if dep.type == :development
103
+ dependencies[p] << DepProxy.new(dep, p)
104
+ end
105
+ end
106
+ end
107
+ dependencies
108
+ end
109
+ end
110
+ end
111
+
112
+ attr_reader :errors
113
+
114
+ # Figures out the best possible configuration of gems that satisfies
115
+ # the list of passed dependencies and any child dependencies without
116
+ # causing any gem activation errors.
117
+ #
118
+ # ==== Parameters
119
+ # *dependencies<Gem::Dependency>:: The list of dependencies to resolve
120
+ #
121
+ # ==== Returns
122
+ # <GemBundle>,nil:: If the list of dependencies can be resolved, a
123
+ # collection of gemspecs is returned. Otherwise, nil is returned.
124
+ def self.resolve(requirements, index, source_requirements = {}, base = [])
125
+ base = SpecSet.new(base) unless base.is_a?(SpecSet)
126
+ resolver = new(index, source_requirements, base)
127
+ result = catch(:success) do
128
+ resolver.start(requirements)
129
+ raise resolver.version_conflict
130
+ nil
131
+ end
132
+ SpecSet.new(result)
133
+ end
134
+
135
+ def initialize(index, source_requirements, base)
136
+ @errors = {}
137
+ @stack = []
138
+ @base = base
139
+ @index = index
140
+ @deps_for = {}
141
+ @missing_gems = Hash.new(0)
142
+ @source_requirements = source_requirements
143
+ end
144
+
145
+ def debug
146
+ if ENV['DEBUG_RESOLVER']
147
+ debug_info = yield
148
+ debug_info = debug_info.inspect unless debug_info.is_a?(String)
149
+ $stderr.puts debug_info
150
+ end
151
+ end
152
+
153
+ def successify(activated)
154
+ activated.values.map { |s| s.to_specs }.flatten.compact
155
+ end
156
+
157
+ def start(reqs)
158
+ activated = {}
159
+ @gems_size = Hash[reqs.map { |r| [r, gems_size(r)] }]
160
+
161
+ resolve(reqs, activated)
162
+ end
163
+
164
+ def resolve(reqs, activated)
165
+ # If the requirements are empty, then we are in a success state. Aka, all
166
+ # gem dependencies have been resolved.
167
+ throw :success, successify(activated) if reqs.empty?
168
+
169
+ debug { print "\e[2J\e[f" ; "==== Iterating ====\n\n" }
170
+
171
+ # Sort dependencies so that the ones that are easiest to resolve are first.
172
+ # Easiest to resolve is defined by:
173
+ # 1) Is this gem already activated?
174
+ # 2) Do the version requirements include prereleased gems?
175
+ # 3) Sort by number of gems available in the source.
176
+ reqs = reqs.sort_by do |a|
177
+ [ activated[a.name] ? 0 : 1,
178
+ a.requirement.prerelease? ? 0 : 1,
179
+ @errors[a.name] ? 0 : 1,
180
+ activated[a.name] ? 0 : @gems_size[a] ]
181
+ end
182
+
183
+ debug { "Activated:\n" + activated.values.map {|a| " #{a}" }.join("\n") }
184
+ debug { "Requirements:\n" + reqs.map {|r| " #{r}"}.join("\n") }
185
+
186
+ activated = activated.dup
187
+
188
+ # Pull off the first requirement so that we can resolve it
189
+ current = reqs.shift
190
+
191
+ debug { "Attempting:\n #{current}"}
192
+
193
+ # Check if the gem has already been activated, if it has, we will make sure
194
+ # that the currently activated gem satisfies the requirement.
195
+ existing = activated[current.name]
196
+ if existing || current.name == 'bundler'
197
+ # Force the current
198
+ if current.name == 'bundler' && !existing
199
+ existing = search(DepProxy.new(Gem::Dependency.new('bundler', VERSION), Gem::Platform::RUBY)).first
200
+ raise GemNotFound, %Q{Bundler could not find gem "bundler" (#{VERSION})} unless existing
201
+ existing.required_by << existing
202
+ activated['bundler'] = existing
203
+ end
204
+
205
+ if current.requirement.satisfied_by?(existing.version)
206
+ debug { " * [SUCCESS] Already activated" }
207
+ @errors.delete(existing.name)
208
+ # Since the current requirement is satisfied, we can continue resolving
209
+ # the remaining requirements.
210
+
211
+ # I have no idea if this is the right way to do it, but let's see if it works
212
+ # The current requirement might activate some other platforms, so let's try
213
+ # adding those requirements here.
214
+ dependencies = existing.activate_platform(current.__platform)
215
+ reqs.concat dependencies
216
+
217
+ dependencies.each do |dep|
218
+ next if dep.type == :development
219
+ @gems_size[dep] ||= gems_size(dep)
220
+ end
221
+
222
+ resolve(reqs, activated)
223
+ else
224
+ debug { " * [FAIL] Already activated" }
225
+ @errors[existing.name] = [existing, current]
226
+ debug { current.required_by.map {|d| " * #{d.name} (#{d.requirement})" }.join("\n") }
227
+ # debug { " * All current conflicts:\n" + @errors.keys.map { |c| " - #{c}" }.join("\n") }
228
+ # Since the current requirement conflicts with an activated gem, we need
229
+ # to backtrack to the current requirement's parent and try another version
230
+ # of it (maybe the current requirement won't be present anymore). If the
231
+ # current requirement is a root level requirement, we need to jump back to
232
+ # where the conflicting gem was activated.
233
+ parent = current.required_by.last
234
+ # `existing` could not respond to required_by if it is part of the base set
235
+ # of specs that was passed to the resolver (aka, instance of LazySpecification)
236
+ parent ||= existing.required_by.last if existing.respond_to?(:required_by)
237
+ # We track the spot where the current gem was activated because we need
238
+ # to keep a list of every spot a failure happened.
239
+ if parent && parent.name != 'bundler'
240
+ debug { " -> Jumping to: #{parent.name}" }
241
+ required_by = existing.respond_to?(:required_by) && existing.required_by.last
242
+ throw parent.name, required_by && required_by.name
243
+ else
244
+ # The original set of dependencies conflict with the base set of specs
245
+ # passed to the resolver. This is by definition an impossible resolve.
246
+ raise version_conflict
247
+ end
248
+ end
249
+ else
250
+ # There are no activated gems for the current requirement, so we are going
251
+ # to find all gems that match the current requirement and try them in decending
252
+ # order. We also need to keep a set of all conflicts that happen while trying
253
+ # this gem. This is so that if no versions work, we can figure out the best
254
+ # place to backtrack to.
255
+ conflicts = Set.new
256
+
257
+ # Fetch all gem versions matching the requirement
258
+ matching_versions = search(current)
259
+
260
+ # If we found no versions that match the current requirement
261
+ if matching_versions.empty?
262
+ # If this is a top-level Gemfile requirement
263
+ if current.required_by.empty?
264
+ if base = @base[current.name] and !base.empty?
265
+ version = base.first.version
266
+ message = "You have requested:\n" \
267
+ " #{current.name} #{current.requirement}\n\n" \
268
+ "The bundle currently has #{current.name} locked at #{version}.\n" \
269
+ "Try running `bundle update #{current.name}`"
270
+ elsif current.source
271
+ name = current.name
272
+ versions = @source_requirements[name][name].map { |s| s.version }
273
+ message = "Could not find gem '#{current}' in #{current.source}.\n"
274
+ if versions.any?
275
+ message << "Source contains '#{name}' at: #{versions.join(', ')}"
276
+ else
277
+ message << "Source does not contain any versions of '#{current}'"
278
+ end
279
+ else
280
+ message = "Could not find gem '#{current}' "
281
+ if @index.source_types.include?(Bundler::Source::Rubygems)
282
+ message << "in any of the gem sources listed in your Gemfile."
283
+ else
284
+ message << "in the gems available on this machine."
285
+ end
286
+ end
287
+ raise GemNotFound, message
288
+ # This is not a top-level Gemfile requirement
289
+ else
290
+ @errors[current.name] = [nil, current]
291
+ end
292
+ end
293
+
294
+ matching_versions.reverse_each do |spec_group|
295
+ conflict = resolve_requirement(spec_group, current, reqs.dup, activated.dup)
296
+ conflicts << conflict if conflict
297
+ end
298
+ # If the current requirement is a root level gem and we have conflicts, we
299
+ # can figure out the best spot to backtrack to.
300
+ if current.required_by.empty? && !conflicts.empty?
301
+ # Check the current "catch" stack for the first one that is included in the
302
+ # conflicts set. That is where the parent of the conflicting gem was required.
303
+ # By jumping back to this spot, we can try other version of the parent of
304
+ # the conflicting gem, hopefully finding a combination that activates correctly.
305
+ @stack.reverse_each do |savepoint|
306
+ if conflicts.include?(savepoint)
307
+ debug { " -> Jumping to: #{savepoint}" }
308
+ throw savepoint
309
+ end
310
+ end
311
+ end
312
+ end
313
+ end
314
+
315
+ def resolve_requirement(spec_group, requirement, reqs, activated)
316
+ # We are going to try activating the spec. We need to keep track of stack of
317
+ # requirements that got us to the point of activating this gem.
318
+ spec_group.required_by.replace requirement.required_by
319
+ spec_group.required_by << requirement
320
+
321
+ activated[spec_group.name] = spec_group
322
+ debug { " Activating: #{spec_group.name} (#{spec_group.version})" }
323
+ debug { spec_group.required_by.map { |d| " * #{d.name} (#{d.requirement})" }.join("\n") }
324
+
325
+ dependencies = spec_group.activate_platform(requirement.__platform)
326
+
327
+ # Now, we have to loop through all child dependencies and add them to our
328
+ # array of requirements.
329
+ debug { " Dependencies"}
330
+ dependencies.each do |dep|
331
+ next if dep.type == :development
332
+ debug { " * #{dep.name} (#{dep.requirement})" }
333
+ dep.required_by.replace(requirement.required_by)
334
+ dep.required_by << requirement
335
+ @gems_size[dep] ||= gems_size(dep)
336
+ reqs << dep
337
+ end
338
+
339
+ # We create a savepoint and mark it by the name of the requirement that caused
340
+ # the gem to be activated. If the activated gem ever conflicts, we are able to
341
+ # jump back to this point and try another version of the gem.
342
+ length = @stack.length
343
+ @stack << requirement.name
344
+ retval = catch(requirement.name) do
345
+ # try to resolve the next option
346
+ resolve(reqs, activated)
347
+ end
348
+
349
+ # clear the search cache since the catch means we couldn't meet the
350
+ # requirement we need with the current constraints on search
351
+ clear_search_cache
352
+
353
+ # Since we're doing a lot of throw / catches. A push does not necessarily match
354
+ # up to a pop. So, we simply slice the stack back to what it was before the catch
355
+ # block.
356
+ @stack.slice!(length..-1)
357
+ retval
358
+ end
359
+
360
+ def gems_size(dep)
361
+ search(dep).size
362
+ end
363
+
364
+ def clear_search_cache
365
+ @deps_for = {}
366
+ end
367
+
368
+ def search(dep)
369
+ if base = @base[dep.name] and base.any?
370
+ reqs = [dep.requirement.as_list, base.first.version.to_s].flatten.compact
371
+ d = Gem::Dependency.new(base.first.name, *reqs)
372
+ else
373
+ d = dep.dep
374
+ end
375
+
376
+ @deps_for[d.hash] ||= begin
377
+ index = @source_requirements[d.name] || @index
378
+ results = index.search(d, @base[d.name])
379
+
380
+ if results.any?
381
+ version = results.first.version
382
+ nested = [[]]
383
+ results.each do |spec|
384
+ if spec.version != version
385
+ nested << []
386
+ version = spec.version
387
+ end
388
+ nested.last << spec
389
+ end
390
+ deps = nested.map{|a| SpecGroup.new(a) }.select{|sg| sg.for?(dep.__platform) }
391
+ else
392
+ deps = []
393
+ end
394
+ end
395
+ end
396
+
397
+ def clean_req(req)
398
+ if req.to_s.include?(">= 0")
399
+ req.to_s.gsub(/ \(.*?\)$/, '')
400
+ else
401
+ req.to_s.gsub(/\, (runtime|development)\)$/, ')')
402
+ end
403
+ end
404
+
405
+ def version_conflict
406
+ VersionConflict.new(errors.keys, error_message)
407
+ end
408
+
409
+ # For a given conflicted requirement, print out what exactly went wrong
410
+ def gem_message(requirement)
411
+ m = ""
412
+
413
+ # A requirement that is required by itself is actually in the Gemfile, and does
414
+ # not "depend on" itself
415
+ if requirement.required_by.first && requirement.required_by.first.name != requirement.name
416
+ m << " #{clean_req(requirement.required_by.first)} depends on\n"
417
+ m << " #{clean_req(requirement)}\n"
418
+ else
419
+ m << " #{clean_req(requirement)}\n"
420
+ end
421
+ m << "\n"
422
+ end
423
+
424
+ def error_message
425
+ errors.inject("") do |o, (conflict, (origin, requirement))|
426
+
427
+ # origin is the SpecSet of specs from the Gemfile that is conflicted with
428
+ if origin
429
+
430
+ o << %{Bundler could not find compatible versions for gem "#{origin.name}":\n}
431
+ o << " In Gemfile:\n"
432
+
433
+ o << gem_message(requirement)
434
+
435
+ # If the origin is "bundler", the conflict is us
436
+ if origin.name == "bundler"
437
+ o << " Current Bundler version:\n"
438
+ other_bundler_required = !requirement.requirement.satisfied_by?(origin.version)
439
+ # If the origin is a LockfileParser, it does not respond_to :required_by
440
+ elsif !origin.respond_to?(:required_by) || !(origin.required_by.first)
441
+ o << " In snapshot (Gemfile.lock):\n"
442
+ end
443
+
444
+ o << gem_message(origin)
445
+
446
+ # If the bundle wants a newer bundler than the running bundler, explain
447
+ if origin.name == "bundler" && other_bundler_required
448
+ o << "This Gemfile requires a different version of Bundler.\n"
449
+ o << "Perhaps you need to update Bundler by running `gem install bundler`?"
450
+ end
451
+
452
+ # origin is nil if the required gem and version cannot be found in any of
453
+ # the specified sources
454
+ else
455
+
456
+ # if the gem cannot be found because of a version conflict between lockfile and gemfile,
457
+ # print a useful error that suggests running `bundle update`, which may fix things
458
+ #
459
+ # @base is a SpecSet of the gems in the lockfile
460
+ # conflict is the name of the gem that could not be found
461
+ if locked = @base[conflict].first
462
+ o << "Bundler could not find compatible versions for gem #{conflict.inspect}:\n"
463
+ o << " In snapshot (Gemfile.lock):\n"
464
+ o << " #{clean_req(locked)}\n\n"
465
+
466
+ o << " In Gemfile:\n"
467
+ o << gem_message(requirement)
468
+ o << "Running `bundle update` will rebuild your snapshot from scratch, using only\n"
469
+ o << "the gems in your Gemfile, which may resolve the conflict.\n"
470
+
471
+ # the rest of the time, the gem cannot be found because it does not exist in the known sources
472
+ else
473
+ if requirement.required_by.first
474
+ o << "Could not find gem '#{clean_req(requirement)}', which is required by "
475
+ o << "gem '#{clean_req(requirement.required_by.first)}', in any of the sources."
476
+ else
477
+ o << "Could not find gem '#{clean_req(requirement)} in any of the sources\n"
478
+ end
479
+ end
480
+
481
+ end
482
+ o
483
+ end
484
+ end
485
+ end
486
+ end