Skip to content
This repository

A naive implementation of so-called 'Did you mean?' #1557

Closed
wants to merge 1 commit into from

4 participants

OZAWA Sakuro Gabor Garami Don't Add Me To Your Organization a.k.a The Travis Bot André Arko
OZAWA Sakuro

When given name is not in the bundle, bundler now asks you to choose one from upto 3 candidates.

I am sorry I could not finish the spec for this.
https://gist.github.com/1405112 (this spec does not work)

OZAWA Sakuro Add a naive implementation of so-called 'Did you mean?' to locate_gem.
It now interactively tries to guess the requested gem when gem with
the given name is missing.
111a15c
Gabor Garami

+1 for this. But it needs to add dependency for diff-lcs at least for tests.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request fails (merged 111a15c into 2c2770e).

André Arko
Owner

Thanks for the patch! We have merged "did you mean" functionality into Bundler 1.3. You can try a prerelease right now by running gem install bundler --pre.

André Arko indirect closed this February 18, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 29, 2011
OZAWA Sakuro Add a naive implementation of so-called 'Did you mean?' to locate_gem.
It now interactively tries to guess the requested gem when gem with
the given name is missing.
111a15c
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 21 additions and 0 deletions. Show diff stats Hide diff stats

  1. 21  lib/bundler/cli.rb
21  lib/bundler/cli.rb
... ...
@@ -1,6 +1,11 @@
1 1
 require 'bundler/vendored_thor'
2 2
 require 'rubygems/user_interaction'
3 3
 require 'rubygems/config_file'
  4
+begin
  5
+  require 'diff/lcs'
  6
+  require 'diff/lcs/string'
  7
+rescue LoadError => e
  8
+end
4 9
 
5 10
 module Bundler
6 11
   class CLI < Thor
@@ -590,6 +595,10 @@ def have_groff?
590 595
 
591 596
     def locate_gem(name)
592 597
       spec = Bundler.load.specs.find{|s| s.name == name }
  598
+      unless spec
  599
+        candidates = candidate_specs(name)
  600
+        spec = choose_from_candidates(candidates) unless candidates.empty?
  601
+      end
593 602
       raise GemNotFound, "Could not find gem '#{name}' in the current bundle." unless spec
594 603
       if spec.name == 'bundler'
595 604
         return File.expand_path('../../../', __FILE__)
@@ -597,5 +606,17 @@ def locate_gem(name)
597 606
       spec.full_gem_path
598 607
     end
599 608
 
  609
+    def choose_from_candidates(specs)
  610
+      Bundler.ui.warn 'Did you mean:'
  611
+      specs.each.with_index {|spec,i| Bundler.ui.warn '%d. %s-%s' % [ i+1, spec.name, spec.version ] }
  612
+      Bundler.ui.warn '> ', false
  613
+      chosen = STDIN.gets.to_i
  614
+      chosen > 0 ? specs[chosen-1] : nil
  615
+    end
  616
+
  617
+    def candidate_specs(name)
  618
+      name.respond_to?(:lcs) ? Bundler.load.specs.sort_by{|spec| -name.lcs(spec.name).size*2.0/(name.size + spec.name.size) }.first(3) : []
  619
+    end
  620
+
600 621
   end
601 622
 end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.