public
Last active

'bundle outdated' extracted into a method

  • Download Gist
bundle_outdated.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
require 'bundler'
 
def outdated?(options = {})
gems = Array(options[:gems])
sources = Array(options[:source])
 
Bundler.definition.validate_ruby!
current_specs = Bundler.load.specs
 
if gems.empty? && sources.empty?
# We're doing a full update
definition = Bundler.definition(true)
else
definition = Bundler.definition(:gems => gems, :sources => sources)
end
options[:local] ? definition.resolve_with_cache! : definition.resolve_remotely!
 
# Loop through the current specs
current_specs.sort_by { |s| s.name }.each do |current_spec|
next if !gems.empty? && !gems.include?(current_spec.name)
 
active_spec = definition.index[current_spec.name].sort_by { |b| b.version }
 
if !current_spec.version.prerelease? && !options[:pre] && active_spec.size > 1
active_spec = active_spec.delete_if { |b| b.respond_to?(:version) && b.version.prerelease? }
end
 
active_spec = active_spec.last
next if active_spec.nil?
 
gem_outdated = Gem::Version.new(active_spec.version) > Gem::Version.new(current_spec.version)
git_outdated = current_spec.git_version != active_spec.git_version
if gem_outdated || git_outdated
return true
end
end
return false
end
 
# When Gemfile contains 'highline' . '0.4.0'
puts outdated?({gems: ['highline']}) #=> true
 
# When Gemfile contains 'highline' . '1.6.16'
puts outdated?({gems: ['highline']}) #=> false
 
# To check all the gems
puts outdated?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.