reditor 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -28,7 +28,7 @@ Or install it yourself as:
28
28
 
29
29
  ## Usage
30
30
 
31
- ### Open Standard Library
31
+ ### Open Standard Library (pure ruby only)
32
32
 
33
33
  ```
34
34
  $ reditor set
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -5,4 +5,5 @@ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
5
5
 
6
6
  require 'reditor'
7
7
 
8
+ $PROGRAM_NAME = File.basename(__FILE__)
8
9
  Reditor::Command.start
@@ -1,58 +1,9 @@
1
- require 'pathname'
2
1
  require 'rubygems'
3
2
 
4
- def gemfile_found?
5
- if File.exists? 'Gemfile'
6
- true
7
- elsif (Dir.getwd != '/')
8
- Dir.chdir('..')
9
- gemfile_found?
10
- end
11
- end
12
-
13
- if (gemfile_found?)
14
- require 'bundler'
15
- Bundler.setup
16
- end
17
-
18
3
  module Reditor
19
4
  class LibraryNotFound < LoadError; end
20
5
 
21
- extend self
22
-
23
- def detect(name)
24
- path = detect_library_path(name) or raise LibraryNotFound, 'No library found'
25
-
26
- if path.file?
27
- [path.dirname.to_path, path.basename.to_path]
28
- else
29
- [path.to_path, '.']
30
- end
31
- end
32
-
33
- def detect_library_path(name)
34
- detect_library_path_from_loadpath(name) || detect_library_path_from_gem(name)
35
- end
36
-
37
- def detect_library_path_from_loadpath(name)
38
- basename = "#{name}.rb"
39
-
40
- $LOAD_PATH.map {|path|
41
- full_path = File.expand_path(path + '/' + basename)
42
-
43
- Pathname.new(full_path)
44
- }.detect(&:exist?)
45
- end
46
-
47
- # TODO: care version specification
48
- def detect_library_path_from_gem(name)
49
- spec = Gem::Specification.find_by_name(name.to_s)
50
-
51
- Pathname.new(spec.full_gem_path)
52
- rescue Gem::LoadError
53
- nil
54
- end
55
-
56
- autoload :Command, 'reditor/command'
57
- autoload :VERSION, 'reditor/version'
6
+ autoload :LibraryLocator, 'reditor/library_locator'
7
+ autoload :Command, 'reditor/command'
8
+ autoload :VERSION, 'reditor/version'
58
9
  end
@@ -4,8 +4,6 @@ require 'thor'
4
4
 
5
5
  module Reditor
6
6
  class Command < Thor
7
- include Reditor
8
-
9
7
  map '-v' => :version,
10
8
  '-h' => :help,
11
9
  '--version' => :version,
@@ -13,24 +11,22 @@ module Reditor
13
11
 
14
12
  desc :open, 'Open the library'
15
13
  def open(name)
16
- dir, file = *detect(name)
17
-
18
- say "Moving to #{dir}", :green
19
- Dir.chdir dir do
20
- say "Opening #{file}", :green
14
+ detect_exec name do |dir, file|
15
+ say "Moving to #{dir}", :green
16
+ Dir.chdir dir do
17
+ say "Opening #{file}", :green
21
18
 
22
- exec editor_command, file
19
+ exec editor_command, file
20
+ end
23
21
  end
24
- rescue LibraryNotFound => e
25
- say e.message, :red
26
22
  end
27
23
 
28
24
  desc :sh, 'Open a shell and move to the library'
29
25
  def sh(name)
30
- dir, _ = *detect(name)
31
-
32
- Dir.chdir dir do
33
- exec shell_command
26
+ detect_exec name do |dir, _|
27
+ Dir.chdir dir do
28
+ exec shell_command
29
+ end
34
30
  end
35
31
  end
36
32
 
@@ -46,6 +42,12 @@ module Reditor
46
42
 
47
43
  private
48
44
 
45
+ def detect_exec(name, &block)
46
+ block.call *LibraryLocator.detect(name)
47
+ rescue LibraryNotFound => e
48
+ say e.message, :red
49
+ end
50
+
49
51
  def editor_command
50
52
  ENV['EDITOR'] or raise '$EDITOR is not provided.'
51
53
  end
@@ -0,0 +1,60 @@
1
+ require 'pathname'
2
+ require 'bundler'
3
+
4
+ # TODO: care multi candidates
5
+ # TODO: care version specification
6
+ module Reditor
7
+ class LibraryLocator
8
+ def self.detect(name)
9
+ new(name).detect
10
+ end
11
+
12
+ attr_reader :name
13
+
14
+ def initialize(name)
15
+ @name = name
16
+ end
17
+
18
+ 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
26
+ end
27
+
28
+ def detect_library_path
29
+ detect_library_path_from_bundler ||
30
+ detect_library_path_from_loadpath ||
31
+ detect_library_path_from_gem
32
+ end
33
+
34
+ def detect_library_path_from_loadpath
35
+ basename = "#{name}.rb"
36
+
37
+ $LOAD_PATH.map {|path|
38
+ full_path = File.expand_path(path + '/' + basename)
39
+
40
+ Pathname.new(full_path)
41
+ }.detect(&:exist?)
42
+ end
43
+
44
+ def detect_library_path_from_gem
45
+ spec = Gem::Specification.find_by_name(name.to_s)
46
+
47
+ Pathname.new(spec.full_gem_path)
48
+ rescue Gem::LoadError
49
+ nil
50
+ 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
+ end
60
+ end
@@ -1,3 +1,3 @@
1
1
  module Reditor
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -15,6 +15,8 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ['lib']
16
16
  gem.version = Reditor::VERSION
17
17
 
18
- gem.add_runtime_dependency 'thor', '>= 0.14.6'
19
- gem.add_runtime_dependency 'bundler', '>= 1.1.4'
18
+ gem.add_runtime_dependency 'thor', '>= 0.15.4'
19
+
20
+ gem.add_development_dependency 'tapp', '>= 1.3.1'
21
+ gem.add_development_dependency 'rspec', '>= 2.10.0'
20
22
  end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+ require 'open3'
3
+
4
+ describe 'reditor command' do
5
+ def capture_reditor(library_name, options)
6
+ options = {editor: 'echo', where: nil}.merge(options)
7
+ command = PROJECT_ROOT.join('bin/reditor').to_path
8
+ project = PROJECT_ROOT.join("spec/samples/#{options[:where]}").to_path
9
+
10
+ Open3.capture2(
11
+ {'EDITOR' => options[:editor]},
12
+ command,
13
+ library_name,
14
+ chdir: project
15
+ ).first
16
+ end
17
+
18
+ describe '#open' do
19
+ context 'Standard library in non-bundler broject (happy case)' do
20
+ subject { capture_reditor('thread', where: 'blank_project') }
21
+
22
+ it { should match /thread\.rb$/ }
23
+ end
24
+
25
+ context 'Standard library in non-bundler broject (no library found)' do
26
+ subject { capture_reditor('3600645b1f28d73e9cd0384b5b9664d6ceeabe7e', where: 'blank_project') }
27
+
28
+ it { should match 'No library found' }
29
+ end
30
+
31
+ context 'rubygems library in non-bundler project' do
32
+ subject { capture_reditor('thor', where: 'blank_project') }
33
+
34
+ it { should match /thor-/m }
35
+
36
+ specify 'global gem should be used' do
37
+ subject.should_not match /thor-0\.14\.6/
38
+ end
39
+ end
40
+
41
+ context 'rubygems library in bundler project (installed)' do
42
+ subject { capture_reditor('thor', where: 'bundler_project') }
43
+
44
+ specify 'bundler gem should be used' do
45
+ subject.should match /thor-0\.14\.6/
46
+ end
47
+ end
48
+
49
+ context 'rubygems library in bundler project (not yet installed)' do
50
+ subject { capture_reditor('thor', where: 'bundler_project_without_lockfile') }
51
+
52
+ it { should match /thor-0\.14\.6/ }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gem 'thor', '0.14.6'
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gem 'thor', '0.14.6'
@@ -0,0 +1,16 @@
1
+ # coding: utf-8
2
+
3
+ require 'pathname'
4
+
5
+ Dir[File.dirname(__FILE__) + '/support/*'].each {|f| require f }
6
+
7
+ PROJECT_ROOT = Pathname.new(__FILE__).dirname.join('..').realpath
8
+
9
+ RSpec.configure do |config|
10
+ config.filter_run focus: true
11
+ config.run_all_when_everything_filtered = true
12
+
13
+ config.before :suite do
14
+ system 'bundle', chdir: PROJECT_ROOT.join('spec/samples/bundler_project').to_path
15
+ end
16
+ 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.3
4
+ version: 0.0.4
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-06-05 00:00:00.000000000 Z
12
+ date: 2012-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.14.6
21
+ version: 0.15.4
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,23 +26,39 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.14.6
29
+ version: 0.15.4
30
30
  - !ruby/object:Gem::Dependency
31
- name: bundler
31
+ name: tapp
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: 1.1.4
38
- type: :runtime
37
+ version: 1.3.1
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.3.1
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 2.10.0
54
+ type: :development
39
55
  prerelease: false
40
56
  version_requirements: !ruby/object:Gem::Requirement
41
57
  none: false
42
58
  requirements:
43
59
  - - ! '>='
44
60
  - !ruby/object:Gem::Version
45
- version: 1.1.4
61
+ version: 2.10.0
46
62
  description: Open a ruby library by your editor
47
63
  email:
48
64
  - celluloid.key@gmail.com
@@ -59,8 +75,13 @@ files:
59
75
  - bin/reditor
60
76
  - lib/reditor.rb
61
77
  - lib/reditor/command.rb
78
+ - lib/reditor/library_locator.rb
62
79
  - lib/reditor/version.rb
63
80
  - reditor.gemspec
81
+ - spec/command_spec.rb
82
+ - spec/samples/bundler_project/Gemfile
83
+ - spec/samples/bundler_project_without_lockfile/Gemfile
84
+ - spec/spec_helper.rb
64
85
  homepage: https://github.com/hibariya/reditor
65
86
  licenses: []
66
87
  post_install_message:
@@ -85,4 +106,8 @@ rubygems_version: 1.8.23
85
106
  signing_key:
86
107
  specification_version: 3
87
108
  summary: Open a ruby library by your editor
88
- test_files: []
109
+ test_files:
110
+ - spec/command_spec.rb
111
+ - spec/samples/bundler_project/Gemfile
112
+ - spec/samples/bundler_project_without_lockfile/Gemfile
113
+ - spec/spec_helper.rb