Skip to content

Instantly share code, notes, and snippets.

@emilsoman
Last active December 16, 2015 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilsoman/5353137 to your computer and use it in GitHub Desktop.
Save emilsoman/5353137 to your computer and use it in GitHub Desktop.
'bundle outdated' extracted into a method
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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment