reditor 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
- $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
5
-
6
4
  require 'reditor'
7
5
 
8
6
  $PROGRAM_NAME = File.basename(__FILE__)
@@ -3,7 +3,8 @@ require 'rubygems'
3
3
  module Reditor
4
4
  class LibraryNotFound < LoadError; end
5
5
 
6
- autoload :LibraryLocator, 'reditor/library_locator'
7
- autoload :Command, 'reditor/command'
8
- autoload :VERSION, 'reditor/version'
6
+ autoload :LibraryLocator, 'reditor/library_locator'
7
+ autoload :CandidatesLocator, 'reditor/candidates_locator'
8
+ autoload :Command, 'reditor/command'
9
+ autoload :VERSION, 'reditor/version'
9
10
  end
@@ -0,0 +1,44 @@
1
+ require 'pathname'
2
+ require 'bundler'
3
+
4
+ module Reditor
5
+ class CandidatesLocator
6
+ def self.detect(keyword)
7
+ new(keyword.to_s).detect
8
+ end
9
+
10
+ attr_reader :pattern
11
+
12
+ def initialize(keyword)
13
+ tokens = keyword.split(/[_\-]/).map {|keyword|
14
+ keyword.length > 4 ? keyword[0...-3] : keyword
15
+ }
16
+
17
+ @pattern = /#{tokens.map {|t| Regexp.quote(t) }.join('|')}/i
18
+ end
19
+
20
+ def detect
21
+ (detect_from_bundler + detect_from_loadpath + detect_from_gem).sort.uniq
22
+ end
23
+
24
+ def detect_from_bundler
25
+ Bundler.load.specs.select {|spec| spec.name =~ pattern }.map(&:name)
26
+ rescue NameError, Bundler::GemfileNotFound
27
+ []
28
+ end
29
+
30
+ def detect_from_loadpath
31
+ $LOAD_PATH.map {|path|
32
+ pathname = Pathname.new(File.expand_path(path))
33
+
34
+ pathname.entries.select {|entry|
35
+ entry.extname == '.rb' && entry.to_path =~ pattern
36
+ }.map {|entry| entry.basename('.*').to_path }
37
+ }.flatten
38
+ end
39
+
40
+ def detect_from_gem
41
+ Gem::Specification.each.select {|spec| spec.name =~ pattern }.map(&:name)
42
+ end
43
+ end
44
+ end
@@ -24,6 +24,7 @@ module Reditor
24
24
  desc :sh, 'Open a shell and move to the library'
25
25
  def sh(name)
26
26
  detect_exec name do |dir, _|
27
+ say "Moving to #{dir}", :green
27
28
  Dir.chdir dir do
28
29
  exec shell_command
29
30
  end
@@ -43,9 +44,35 @@ module Reditor
43
44
  private
44
45
 
45
46
  def detect_exec(name, &block)
46
- block.call *LibraryLocator.detect(name)
47
+ path = LibraryLocator.detect(name)
48
+
49
+ return choose_exec name, &block unless path
50
+
51
+ dir, file =
52
+ if path.file?
53
+ [path.dirname.to_path, path.basename.to_path]
54
+ else
55
+ [path.to_path, '.']
56
+ end
57
+
58
+ block.call dir, file
47
59
  rescue LibraryNotFound => e
48
- say e.message, :red
60
+ warn e.message
61
+ end
62
+
63
+ def choose_exec(name, &block)
64
+ candidates = CandidatesLocator.detect(name)
65
+
66
+ raise LibraryNotFound, "Library #{name} not found" if candidates.empty?
67
+
68
+ candidates.each.with_index do |candidate, i|
69
+ say "[#{i}] #{candidate}"
70
+ end
71
+ print "Choose number of library [0]> "
72
+
73
+ chosen = Integer($stdin.gets.strip) rescue 0
74
+
75
+ detect_exec candidates[chosen], &block
49
76
  end
50
77
 
51
78
  def editor_command
@@ -1,12 +1,11 @@
1
1
  require 'pathname'
2
2
  require 'bundler'
3
3
 
4
- # TODO: care multi candidates
5
4
  # TODO: care version specification
6
5
  module Reditor
7
6
  class LibraryLocator
8
7
  def self.detect(name)
9
- new(name).detect
8
+ new(name.to_s).detect
10
9
  end
11
10
 
12
11
  attr_reader :name
@@ -16,22 +15,18 @@ module Reditor
16
15
  end
17
16
 
18
17
  def detect
19
- path = detect_library_path or raise LibraryNotFound, 'No library found'
20
-
21
- if path.file?
22
- [path.dirname.to_path, path.basename.to_path]
23
- else
24
- [path.to_path, '.']
25
- end
18
+ detect_from_bundler || detect_from_loadpath || detect_from_gem
26
19
  end
27
20
 
28
- def detect_library_path
29
- detect_library_path_from_bundler ||
30
- detect_library_path_from_loadpath ||
31
- detect_library_path_from_gem
21
+ def detect_from_bundler
22
+ return nil unless spec = Bundler.load.specs.find {|spec| spec.name == name }
23
+
24
+ Pathname.new(spec.full_gem_path)
25
+ rescue NameError # ensure enviroments that bundler isn't installed
26
+ rescue Bundler::GemNotFound, Bundler::GemfileNotFound
32
27
  end
33
28
 
34
- def detect_library_path_from_loadpath
29
+ def detect_from_loadpath
35
30
  basename = "#{name}.rb"
36
31
 
37
32
  $LOAD_PATH.map {|path|
@@ -41,20 +36,12 @@ module Reditor
41
36
  }.detect(&:exist?)
42
37
  end
43
38
 
44
- def detect_library_path_from_gem
45
- spec = Gem::Specification.find_by_name(name.to_s)
39
+ def detect_from_gem
40
+ spec = Gem::Specification.find_by_name(name)
46
41
 
47
42
  Pathname.new(spec.full_gem_path)
48
43
  rescue Gem::LoadError
49
44
  nil
50
45
  end
51
-
52
- def detect_library_path_from_bundler
53
- return nil unless spec = Bundler.load.specs.find {|spec| spec.name == name.to_s }
54
-
55
- Pathname.new(spec.full_gem_path)
56
- rescue Bundler::GemNotFound, Bundler::GemfileNotFound
57
- nil
58
- end
59
46
  end
60
47
  end
@@ -1,3 +1,3 @@
1
1
  module Reditor
2
- VERSION = '0.0.4'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -2,12 +2,12 @@ require 'spec_helper'
2
2
  require 'open3'
3
3
 
4
4
  describe 'reditor command' do
5
- def capture_reditor(library_name, options)
5
+ def capture_reditor(library_name, options = {})
6
6
  options = {editor: 'echo', where: nil}.merge(options)
7
7
  command = PROJECT_ROOT.join('bin/reditor').to_path
8
8
  project = PROJECT_ROOT.join("spec/samples/#{options[:where]}").to_path
9
9
 
10
- Open3.capture2(
10
+ Open3.capture2e(
11
11
  {'EDITOR' => options[:editor]},
12
12
  command,
13
13
  library_name,
@@ -15,6 +15,8 @@ describe 'reditor command' do
15
15
  ).first
16
16
  end
17
17
 
18
+ let(:thor_in_bundler_project) { /thor-0\.14\.6/ }
19
+
18
20
  describe '#open' do
19
21
  context 'Standard library in non-bundler broject (happy case)' do
20
22
  subject { capture_reditor('thread', where: 'blank_project') }
@@ -25,7 +27,7 @@ describe 'reditor command' do
25
27
  context 'Standard library in non-bundler broject (no library found)' do
26
28
  subject { capture_reditor('3600645b1f28d73e9cd0384b5b9664d6ceeabe7e', where: 'blank_project') }
27
29
 
28
- it { should match 'No library found' }
30
+ it { should match 'Library 3600645b1f28d73e9cd0384b5b9664d6ceeabe7e not found' }
29
31
  end
30
32
 
31
33
  context 'rubygems library in non-bundler project' do
@@ -34,7 +36,7 @@ describe 'reditor command' do
34
36
  it { should match /thor-/m }
35
37
 
36
38
  specify 'global gem should be used' do
37
- subject.should_not match /thor-0\.14\.6/
39
+ subject.should_not match thor_in_bundler_project
38
40
  end
39
41
  end
40
42
 
@@ -42,14 +44,22 @@ describe 'reditor command' do
42
44
  subject { capture_reditor('thor', where: 'bundler_project') }
43
45
 
44
46
  specify 'bundler gem should be used' do
45
- subject.should match /thor-0\.14\.6/
47
+ subject.should match thor_in_bundler_project
46
48
  end
47
49
  end
48
50
 
49
51
  context 'rubygems library in bundler project (not yet installed)' do
50
52
  subject { capture_reditor('thor', where: 'bundler_project_without_lockfile') }
51
53
 
52
- it { should match /thor-0\.14\.6/ }
54
+ it { should match thor_in_bundler_project }
55
+ end
56
+
57
+ context 'with incorrect name' do
58
+ subject { capture_reditor('thief') }
59
+
60
+ it { should match /\[\d+\]\sthread/ }
61
+ it { should match /\[\d+\]\sthor/ }
62
+ it { should match /Choose number of library/ }
53
63
  end
54
64
  end
55
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reditor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-06 00:00:00.000000000 Z
12
+ date: 2013-01-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -74,6 +74,7 @@ files:
74
74
  - Rakefile
75
75
  - bin/reditor
76
76
  - lib/reditor.rb
77
+ - lib/reditor/candidates_locator.rb
77
78
  - lib/reditor/command.rb
78
79
  - lib/reditor/library_locator.rb
79
80
  - lib/reditor/version.rb