class GemDependencies def self.dependency_for(gem_name) dependencies.detect { |dep| dep[:name] == gem_name } end # generates code that should be included or eval'ed inside a Gemfile. # for every rc dependency, a 'gem' statement is generated. # this statement specifies that dependency using the _minimum_ allowed version. # # example: # if the RC depends on "json", "~> 1.2.3", the following statement will be generated: # gem "json", "= 1.2.3" def self.minimum_version_gemfile_code(options = {}) dependencies_minimum_versions = dependencies.map do |dependency| minimum_version = calculate_minimum_version(dependency[:version]) if minimum_version dependency.merge(version: "= #{minimum_version}") else dependency.delete(:version) end dependency end dependencies_minimum_versions.map do |dependency| gem_name = dependency[:name] next if gem_name == "rails" gem_options = "" gem_options << ", '#{dependency[:version]}'" if dependency[:version] gem_options << if options[:internal] && dependency[:git] ", :git => '#{dependency[:git]}', :ref => '#{dependency[:ref]}', :submodules => true" elsif options[:internal] && dependency[:path] ", :path => '#{dependency[:path]}'" else "" end # do not require the gems here, the rails connector gem needs to do that on it's own. # if these gems were loaded without :require => false, # the tests would not detect a failure of the rails connector to require them. "gem '#{gem_name}'#{gem_options}, :require => false" end.join("\n") end def self.minimum_version_for(gem, version = nil) dependency ||= dependencies.detect do |dep| dep[:name] == gem end version ||= dependency[:version] calculate_minimum_version(version) end def self.calculate_minimum_version(version) gem_requirement = Gem::Requirement.new(version) minimum_requirements = gem_requirement.requirements.select do |requirement| ["=", ">=", "~>"].include?(requirement[0]) end if minimum_requirements.length > 1 raise "multiple minimum version requirements found for version requirement #{version}" elsif minimum_requirements.length < 1 return nil end minimum_requirements[0][1] end end