From b51005d343b50c1cc151601a033647ab722c7e17 Mon Sep 17 00:00:00 2001 From: Smit Shah Date: Wed, 8 Jan 2014 09:02:52 +0530 Subject: Refactored open and fixed few failing specs --- lib/bundler/cli/common.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'lib/bundler/cli/common.rb') diff --git a/lib/bundler/cli/common.rb b/lib/bundler/cli/common.rb index c1a9645f..e7e3f250 100644 --- a/lib/bundler/cli/common.rb +++ b/lib/bundler/cli/common.rb @@ -7,5 +7,41 @@ module Bundler group_str = (groups.size == 1) ? "group" : "groups" "Gems in the #{group_str} #{group_list} were not installed." end + + def self.select_spec(name, regex_match = nil) + specs = [] + regexp = Regexp.new(name) if regex_match + + Bundler.definition.specs.each do |spec| + return spec if spec.name == name + specs << spec if regexp && spec.name =~ regexp + end + + case specs.count + when 0 + raise GemNotFound, not_found_message(name, Bundler.definition.dependencies) + when 1 + specs.first + else + specs.each_with_index do |spec, index| + Bundler.ui.info "#{index.succ} : #{spec.name}", true + end + Bundler.ui.info '0 : - exit -', true + + input = Bundler.ui.ask('> ') + (num = input.to_i) > 0 ? specs[num - 1] : nil + end + end + + def self.not_found_message(missing_gem_name, alternatives) + message = "Could not find gem '#{missing_gem_name}'." + + # This is called as the result of a GemNotFound, let's see if + # there's any similarly named ones we can propose instead + alternate_names = alternatives.map { |a| a.respond_to?(:name) ? a.name : a } + suggestions = SimilarityDetector.new(alternate_names).similar_word_list(missing_gem_name) + message += "\nDid you mean #{suggestions}?" if suggestions + message + end end end -- cgit v1.2.3