switch_file 0.1.0 → 0.1.1
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/CHANGELOG.markdown +5 -0
- data/README.markdown +34 -0
- data/examples/gem.switch_file +17 -0
- data/examples/rails.switch_file +58 -0
- data/lib/switch_file.rb +9 -14
- data/lib/switch_file/app.rb +33 -11
- data/lib/switch_file/exception.rb +7 -0
- data/lib/switch_file/file_type.rb +7 -9
- data/lib/switch_file/project.rb +26 -0
- data/lib/switch_file/source.rb +45 -0
- data/lib/switch_file/version.rb +1 -1
- data/spec/fixtures/project/.switch_file +8 -6
- data/spec/fixtures/project/lib/some_class.rb +2 -0
- data/spec/lib/switch_file/app_spec.rb +18 -3
- data/spec/lib/switch_file/exception_spec.rb +9 -0
- data/spec/lib/switch_file/file_type_spec.rb +21 -22
- data/spec/lib/switch_file/project_spec.rb +46 -0
- data/spec/lib/switch_file/source_spec.rb +64 -0
- data/spec/lib/switch_file_spec.rb +6 -29
- data/spec/spec_helper.rb +1 -3
- data/switch_file.gemspec +2 -0
- metadata +48 -20
- data/lib/switch_file/file_type_shortcut.rb +0 -10
- data/lib/switch_file/source_path.rb +0 -22
- data/spec/lib/switch_file/file_type_shortcut_spec.rb +0 -21
- data/spec/lib/switch_file/source_path_spec.rb +0 -44
data/CHANGELOG.markdown
CHANGED
data/README.markdown
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
SwitchFile
|
|
2
|
+
----------
|
|
3
|
+
|
|
4
|
+
SwitchFile is a simple gem for opening a file related to a currently open file.
|
|
5
|
+
|
|
6
|
+
Usage
|
|
7
|
+
-----
|
|
8
|
+
|
|
9
|
+
1. Install SwitchFile
|
|
10
|
+
|
|
11
|
+
gem install switch_file
|
|
12
|
+
|
|
13
|
+
1. Add a `.switch_file` to your project root directory. A `.switch_file` is a
|
|
14
|
+
plain ruby file containing an array of hashes. Each hash represents
|
|
15
|
+
a file type. A file type has the following attributes:
|
|
16
|
+
* name - name of the file type
|
|
17
|
+
* shortcut - a short, unique key for the file type
|
|
18
|
+
* path_generator - a ruby block that will determine the path of the
|
|
19
|
+
matching file type of a class name
|
|
20
|
+
* path_regex - a regex that determines if a file path matches the file
|
|
21
|
+
type
|
|
22
|
+
* command - the editor that will open the file
|
|
23
|
+
|
|
24
|
+
You can find sample SwitchFile configuration files at the examples.
|
|
25
|
+
|
|
26
|
+
1. Configure your editor to launch switch_file based on the current file. With [geany](http://www.geany.org),
|
|
27
|
+
|
|
28
|
+
1. Go to Build/Set Build Commands.
|
|
29
|
+
1. Change the execute command to `switch_file --path %d/%f`.
|
|
30
|
+
1. Running Build/Execute will now launch switch_file based on the currently
|
|
31
|
+
open file.
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
:name => 'spec',
|
|
4
|
+
:shortcut => :s,
|
|
5
|
+
:path_generator => lambda {|class_name| "spec/lib/#{class_name}_spec.rb"},
|
|
6
|
+
:path_regex => %r{spec/lib/(.*)_spec.rb},
|
|
7
|
+
:command => 'geany'
|
|
8
|
+
},
|
|
9
|
+
|
|
10
|
+
{
|
|
11
|
+
:name => 'lib',
|
|
12
|
+
:shortcut => :l,
|
|
13
|
+
:path_generator => lambda {|class_name| "lib/#{class_name}.rb"},
|
|
14
|
+
:path_regex => %r{lib/(.*).rb},
|
|
15
|
+
:command => 'geany'
|
|
16
|
+
}
|
|
17
|
+
]
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
:name => 'model_spec',
|
|
4
|
+
:shortcut => :ms,
|
|
5
|
+
:path_generator => lambda {|class_name| "spec/models/#{class_name}_spec.rb"},
|
|
6
|
+
:path_regex => %r{spec/models/(.*)_spec.rb},
|
|
7
|
+
:command => 'geany'
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
:name => 'controller_spec',
|
|
11
|
+
:shortcut => :cs,
|
|
12
|
+
:path_generator => lambda {|class_name| "spec/controllers/#{class_name}_spec.rb"},
|
|
13
|
+
:path_regex => %r{spec/controllers/(.*)_spec.rb},
|
|
14
|
+
:command => 'geany'
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
:name => 'helper_spec',
|
|
18
|
+
:shortcut => :hs,
|
|
19
|
+
:path_generator => lambda {|class_name| "spec/helpers/#{class_name}_spec.rb"},
|
|
20
|
+
:path_regex => %r{spec/helpers/(.*)_spec.rb},
|
|
21
|
+
:command => 'geany'
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
:name => 'lib_spec',
|
|
25
|
+
:shortcut => :ls,
|
|
26
|
+
:path_generator => lambda {|class_name| "spec/lib/#{class_name}_spec.rb"},
|
|
27
|
+
:path_regex => %r{spec/lib/(.*)_spec.rb},
|
|
28
|
+
:command => 'geany'
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
:name => 'model',
|
|
32
|
+
:shortcut => :m,
|
|
33
|
+
:path_generator => lambda {|class_name| "app/models/#{class_name}.rb"},
|
|
34
|
+
:path_regex => %r{app/models/(.*).rb},
|
|
35
|
+
:command => 'geany'
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
:name => 'controller',
|
|
39
|
+
:shortcut => :c,
|
|
40
|
+
:path_generator => lambda {|class_name| "app/controllers/#{class_name}.rb"},
|
|
41
|
+
:path_regex => %r{app/controllers/(.*).rb},
|
|
42
|
+
:command => 'geany'
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
:name => 'helper',
|
|
46
|
+
:shortcut => :h,
|
|
47
|
+
:path_generator => lambda {|class_name| "app/helpers/#{class_name}.rb"},
|
|
48
|
+
:path_regex => %r{app/helpers/(.*).rb},
|
|
49
|
+
:command => 'geany'
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
:name => 'lib',
|
|
53
|
+
:shortcut => :l,
|
|
54
|
+
:path_generator => lambda {|class_name| "lib/#{class_name}.rb"},
|
|
55
|
+
:path_regex => %r{lib/(.*).rb},
|
|
56
|
+
:command => 'geany'
|
|
57
|
+
}
|
|
58
|
+
]
|
data/lib/switch_file.rb
CHANGED
|
@@ -1,27 +1,22 @@
|
|
|
1
|
+
require "active_support/inflector"
|
|
1
2
|
require 'pow'
|
|
2
3
|
require 'thor'
|
|
3
4
|
require 'valuable'
|
|
4
5
|
|
|
5
6
|
require 'switch_file/app'
|
|
7
|
+
require 'switch_file/exception'
|
|
6
8
|
require 'switch_file/file_type'
|
|
7
|
-
require 'switch_file/
|
|
8
|
-
require 'switch_file/
|
|
9
|
+
require 'switch_file/project'
|
|
10
|
+
require 'switch_file/source'
|
|
9
11
|
require "switch_file/version"
|
|
10
12
|
|
|
11
13
|
module SwitchFile
|
|
12
|
-
def self.
|
|
13
|
-
@
|
|
14
|
+
def self.production?
|
|
15
|
+
@production.nil? ? true : @production
|
|
14
16
|
end
|
|
15
17
|
|
|
16
|
-
def self.
|
|
17
|
-
@
|
|
18
|
+
def self.production=(value)
|
|
19
|
+
@production = value
|
|
18
20
|
end
|
|
21
|
+
end
|
|
19
22
|
|
|
20
|
-
def self.file_type_attributes
|
|
21
|
-
@file_type_attributes ||= eval(config_path.read)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def self.file_type_attributes=(attributes_array)
|
|
25
|
-
@file_type_attributes = attributes_array
|
|
26
|
-
end
|
|
27
|
-
end
|
data/lib/switch_file/app.rb
CHANGED
|
@@ -1,23 +1,45 @@
|
|
|
1
1
|
module SwitchFile
|
|
2
2
|
class App < Thor
|
|
3
|
-
|
|
3
|
+
default_task :execute
|
|
4
|
+
|
|
5
|
+
desc "[execute] [OPTIONS]...", "Switch to the matching file type of PATH."
|
|
4
6
|
|
|
5
7
|
method_option "shortcut",
|
|
6
8
|
:aliases => "-s",
|
|
7
9
|
:desc => "the shortcut of the file type you wish to open",
|
|
8
10
|
:type => :string
|
|
9
11
|
|
|
10
|
-
method_option "
|
|
11
|
-
:aliases => "-
|
|
12
|
-
:desc => "
|
|
13
|
-
:type => :string
|
|
12
|
+
method_option "path",
|
|
13
|
+
:aliases => "-p",
|
|
14
|
+
:desc => "the path of the source file",
|
|
15
|
+
:type => :string,
|
|
16
|
+
:required => true
|
|
17
|
+
|
|
18
|
+
def execute
|
|
19
|
+
source = Source.new(:path => options['path'])
|
|
20
|
+
|
|
21
|
+
begin
|
|
22
|
+
shortcut = options['shortcut'] || ask(source.prompt_message)
|
|
23
|
+
unless shortcut.strip.empty?
|
|
24
|
+
begin
|
|
25
|
+
target_command = source.project.file_type_with_shortcut!(shortcut).generate_open_command(source)
|
|
26
|
+
`#{target_command}`
|
|
27
|
+
rescue SwitchFile::Exception => exception
|
|
28
|
+
if SwitchFile.production?
|
|
29
|
+
say exception.message
|
|
30
|
+
retry
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
rescue SwitchFile::Exception => exception
|
|
36
|
+
say exception.message if SwitchFile.production?
|
|
37
|
+
end
|
|
38
|
+
end
|
|
14
39
|
|
|
15
|
-
def
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
shortcut = FileTypeShortcut.new(value: options['shortcut'] || ask(sp.prompt_message))
|
|
19
|
-
target_command = shortcut.file_type.generate_open_command(sp)
|
|
20
|
-
`#{target_command}`
|
|
40
|
+
def help
|
|
41
|
+
say "switch_file #{VERSION}"
|
|
42
|
+
super
|
|
21
43
|
end
|
|
22
44
|
end
|
|
23
45
|
end
|
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
module SwitchFile
|
|
2
2
|
# A type of file within the project
|
|
3
3
|
class FileType < Valuable
|
|
4
|
-
has_value :
|
|
4
|
+
has_value :command
|
|
5
5
|
has_value :name
|
|
6
|
+
has_value :path_generator
|
|
6
7
|
has_value :path_regex
|
|
7
8
|
has_value :shortcut
|
|
8
9
|
|
|
9
|
-
def
|
|
10
|
-
|
|
10
|
+
def generate_open_command(source)
|
|
11
|
+
"#{command} #{source.project.path}/#{relative_path(source)}"
|
|
11
12
|
end
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def generate_open_command(source_path)
|
|
18
|
-
open_command.call(source_path.class_name)
|
|
14
|
+
# Path of matching file to source, relative to source's project
|
|
15
|
+
def relative_path(source)
|
|
16
|
+
path_generator.call(source.class_name)
|
|
19
17
|
end
|
|
20
18
|
end
|
|
21
19
|
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module SwitchFile
|
|
2
|
+
class Project < Valuable
|
|
3
|
+
has_value :path
|
|
4
|
+
|
|
5
|
+
def config_path
|
|
6
|
+
"#{path}/.switch_file"
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def file_type_attributes
|
|
10
|
+
eval(Pow(config_path).read)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def file_type_with_shortcut!(shortcut)
|
|
14
|
+
file_types.detect{|file_type| shortcut.to_s == file_type.shortcut.to_s}.tap do |result|
|
|
15
|
+
raise NoMatchingFileTypeForShortcut.new(shortcut.to_s) if result.nil?
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def file_types
|
|
20
|
+
@file_types ||= file_type_attributes.map{|attributes| FileType.new(attributes) }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class NoMatchingFileTypeForShortcut < SwitchFile::Exception
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
module SwitchFile
|
|
2
|
+
# A source file
|
|
3
|
+
class Source < Valuable
|
|
4
|
+
has_value :path
|
|
5
|
+
|
|
6
|
+
def self.project_path(path)
|
|
7
|
+
parent = Pow(path).parent
|
|
8
|
+
if (parent / '.switch_file').file?
|
|
9
|
+
parent.to_s
|
|
10
|
+
elsif parent == Pow(path)
|
|
11
|
+
raise CannotFindProjectPath.new(path.to_s)
|
|
12
|
+
else
|
|
13
|
+
project_path(parent)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def class_name
|
|
18
|
+
path.match(file_type!.path_regex)[1]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def file_type!
|
|
22
|
+
project.file_types.detect{|file_type| path =~ file_type.path_regex }.tap do |result|
|
|
23
|
+
raise NoMatchingFileTypeForPath.new(path) if result.nil?
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def project
|
|
28
|
+
@project ||= Project.new(:path => self.class.project_path(path))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def prompt_message
|
|
32
|
+
file_type_options = project.file_types.map{|file_type|
|
|
33
|
+
"[#{file_type.shortcut}] #{file_type.name}: #{file_type.relative_path(self)}"
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
"Enter the shortcut of the file you want to open:\n\n#{file_type_options.join("\n")}\n\n"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
class CannotFindProjectPath < SwitchFile::Exception
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
class NoMatchingFileTypeForPath < SwitchFile::Exception
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
data/lib/switch_file/version.rb
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
[
|
|
2
2
|
{
|
|
3
|
-
shortcut
|
|
4
|
-
|
|
5
|
-
path_regex
|
|
3
|
+
:shortcut => :s,
|
|
4
|
+
:path_generator => lambda {|class_name| "spec/lib/#{class_name}_spec.rb > tmp/result.txt"},
|
|
5
|
+
:path_regex => %r{spec/fixtures/project/spec/lib/(.*)_spec.rb},
|
|
6
|
+
:command => 'echo'
|
|
6
7
|
},
|
|
7
8
|
|
|
8
9
|
{
|
|
9
|
-
shortcut
|
|
10
|
-
|
|
11
|
-
path_regex
|
|
10
|
+
:shortcut => :l,
|
|
11
|
+
:path_generator => lambda {|class_name| "lib/#{class_name}_spec.rb > tmp/result.txt"},
|
|
12
|
+
:path_regex => %r{spec/fixtures/project/lib/(.*).rb},
|
|
13
|
+
:command => 'echo'
|
|
12
14
|
}
|
|
13
15
|
]
|
|
@@ -2,10 +2,25 @@ require 'spec_helper'
|
|
|
2
2
|
|
|
3
3
|
describe SwitchFile::App do
|
|
4
4
|
describe "#execute" do
|
|
5
|
-
it "should open the matching file_type of the
|
|
5
|
+
it "should open the matching file_type of the source" do
|
|
6
6
|
app = described_class.new
|
|
7
|
-
app.invoke :execute, ["spec/fixtures/project/lib/
|
|
8
|
-
(test_tmp_dir / 'result.txt').read.chomp.should
|
|
7
|
+
app.invoke :execute, [], "path" => "spec/fixtures/project/lib/some_class.rb", "shortcut" => 's'
|
|
8
|
+
(test_tmp_dir / 'result.txt').read.chomp.should include('spec/lib/some_class_spec.rb')
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "should exit if the shortcut is blank" do
|
|
12
|
+
app = described_class.new
|
|
13
|
+
lambda { app.invoke :execute, [], "path" => "spec/fixtures/project/lib/some_class.rb", "shortcut" => "\n" }.should_not raise_error
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "should not fail if the shortcut is invalid" do
|
|
17
|
+
app = described_class.new
|
|
18
|
+
lambda { app.invoke :execute, [], "path" => "spec/fixtures/project/lib/some_class.rb", "shortcut" => "z" }.should_not raise_error
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "should fail if the path does not have a matching file type" do
|
|
22
|
+
app = described_class.new
|
|
23
|
+
lambda { app.invoke :execute, [], "path" => "spec/fixtures/project/Gemfile" }.should_not raise_error
|
|
9
24
|
end
|
|
10
25
|
end
|
|
11
26
|
end
|
|
@@ -1,35 +1,34 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe SwitchFile::FileType do
|
|
4
|
-
describe "#
|
|
5
|
-
it "should
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
describe "#generate_open_command" do
|
|
5
|
+
it "should return the command for opening the desired file type of the source" do
|
|
6
|
+
spec_file_type = FileType.new(
|
|
7
|
+
:path_generator => lambda{|class_name| "spec/lib/#{class_name}_spec.rb" },
|
|
8
|
+
:command => 'geany'
|
|
9
|
+
)
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
lib_file_type = FileType.new(
|
|
12
|
+
:path_regex => %r{lib/(.*).rb$}
|
|
13
|
+
)
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
source = Source.new(:path => Pow('spec/fixtures/project/lib/some_class.rb').to_s)
|
|
16
|
+
source.project.should_receive(:file_types).and_return([lib_file_type, spec_file_type])
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
command = spec_file_type.generate_open_command(source)
|
|
19
|
+
command.should == "geany #{Pow('spec/fixtures/project/spec/lib/some_class_spec.rb').to_s}"
|
|
18
20
|
end
|
|
19
21
|
end
|
|
20
22
|
|
|
21
|
-
describe "#
|
|
22
|
-
it "should
|
|
23
|
-
spec_file_type = FileType.new(
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
lib_file_type = FileType.new(path_regex: %r{lib/(.*).rb$})
|
|
28
|
-
|
|
29
|
-
FileType.all = [lib_file_type, spec_file_type]
|
|
23
|
+
describe "#path" do
|
|
24
|
+
it "should be the path of the matching file type of source" do
|
|
25
|
+
spec_file_type = FileType.new(
|
|
26
|
+
:path_generator => lambda{|class_name| "spec/lib/#{class_name}_spec.rb" },
|
|
27
|
+
:command => 'geany'
|
|
28
|
+
)
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
path = spec_file_type.relative_path(Source.new(:path => Pow('spec/fixtures/project/lib/some_class.rb').to_s))
|
|
31
|
+
path.should == "spec/lib/some_class_spec.rb"
|
|
33
32
|
end
|
|
34
33
|
end
|
|
35
34
|
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe SwitchFile::Project do
|
|
4
|
+
describe "#file_type_with_shortcut!" do
|
|
5
|
+
it "should be the file_type with the same name as the shortcut" do
|
|
6
|
+
file_type = FileType.new(:shortcut => :s)
|
|
7
|
+
project = Project.new
|
|
8
|
+
project.should_receive(:file_types).and_return([file_type])
|
|
9
|
+
|
|
10
|
+
project.file_type_with_shortcut!(:s).shortcut.should == :s
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "should raise an error if there is no matching file type" do
|
|
14
|
+
file_type = FileType.new(:shortcut => :r)
|
|
15
|
+
project = Project.new
|
|
16
|
+
project.should_receive(:file_types).and_return([file_type])
|
|
17
|
+
|
|
18
|
+
lambda { project.file_type_with_shortcut!(:s) }.should raise_error(Project::NoMatchingFileTypeForShortcut)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe "#file_types" do
|
|
23
|
+
it "should be the file types from the file_type_attributes" do
|
|
24
|
+
project = Project.new
|
|
25
|
+
project.should_receive(:file_type_attributes).and_return([{:shortcut => :s}])
|
|
26
|
+
|
|
27
|
+
project.file_types.should have(1).file_type
|
|
28
|
+
project.file_types[0].shortcut.should == :s
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
describe "#file_type_attributes" do
|
|
33
|
+
it "should be the file_type_attributes from the config_path, if there are no set file_type_attributes" do
|
|
34
|
+
project = Project.new(:path => 'spec/fixtures/project')
|
|
35
|
+
Pow(project.config_path).should be_file
|
|
36
|
+
project.file_type_attributes.should be_an(Array)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
describe "#config_path" do
|
|
41
|
+
it "should be the .switch_file in the path" do
|
|
42
|
+
project = Project.new(:path => 'spec/fixtures/project')
|
|
43
|
+
project.config_path.should == 'spec/fixtures/project/.switch_file'
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe SwitchFile::Source do
|
|
4
|
+
describe "#class_name" do
|
|
5
|
+
it "should be the part of the value that is the class name of the source path" do
|
|
6
|
+
source = Source.new(:path => 'spec/fixtures/project/lib/project/some_class.rb')
|
|
7
|
+
source.project.should_receive(:file_types).and_return([FileType.new(:path_regex => %r{lib/(.*).rb$})])
|
|
8
|
+
source.class_name.should == 'project/some_class'
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe "#file_type!" do
|
|
13
|
+
it "should be the file type that matches the source path" do
|
|
14
|
+
file_type = FileType.new(:path_regex => %r{lib/(.*).rb$})
|
|
15
|
+
|
|
16
|
+
source = Source.new(:path => 'spec/fixtures/project/lib/some_class.rb')
|
|
17
|
+
source.project.should_receive(:file_types).and_return([file_type])
|
|
18
|
+
source.file_type!.should == file_type
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe "#prompt_message" do
|
|
23
|
+
it "be the message to display if the user want to jump to file related to the source path" do
|
|
24
|
+
file_types = [
|
|
25
|
+
FileType.new(
|
|
26
|
+
:name => :spec,
|
|
27
|
+
:shortcut => :s,
|
|
28
|
+
:path_generator => lambda {|class_name| "spec/lib/#{class_name}_spec.rb"},
|
|
29
|
+
:path_regex => %r{spec/lib/(.*)_spec.rb},
|
|
30
|
+
:command => 'geany'
|
|
31
|
+
),
|
|
32
|
+
FileType.new(
|
|
33
|
+
:name => :lib,
|
|
34
|
+
:shortcut => :l,
|
|
35
|
+
:path_generator => lambda {|class_name| "lib/#{class_name}.rb"},
|
|
36
|
+
:path_regex => %r{lib/(.*).rb},
|
|
37
|
+
:command => 'geany'
|
|
38
|
+
)
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
source = Source.new(:path => Pow('spec/fixtures/project/lib/some_class.rb').to_s)
|
|
42
|
+
source.project.stub(:file_types).and_return(file_types)
|
|
43
|
+
source.prompt_message.should == "Enter the shortcut of the file you want to open:\n\n[s] spec: spec/lib/some_class_spec.rb\n[l] lib: lib/some_class.rb\n\n"
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
describe ".project_path" do
|
|
48
|
+
it "should be the ancestor of path that has a .switch_file file" do
|
|
49
|
+
Source.project_path(Pow('spec/fixtures/project/lib/some_class.rb').to_s).should == Pow('spec/fixtures/project').to_s
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "should raise an error if the path does not have a project_path" do
|
|
53
|
+
lambda { Source.project_path(Pow('/').to_s) }.should raise_error(Source::CannotFindProjectPath)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
describe "project" do
|
|
58
|
+
it "should be the project which owns this source" do
|
|
59
|
+
source = Source.new(:path => 'spec/fixtures/project/lib/some_class.rb')
|
|
60
|
+
source.project.should be_a(Project)
|
|
61
|
+
source.project.path.should == Pow('spec/fixtures/project').to_s
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -1,37 +1,14 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe SwitchFile do
|
|
4
|
-
describe "
|
|
5
|
-
it "should be
|
|
6
|
-
SwitchFile.
|
|
7
|
-
SwitchFile.
|
|
4
|
+
describe "#production?" do
|
|
5
|
+
it "should be true by default" do
|
|
6
|
+
SwitchFile.production = nil
|
|
7
|
+
SwitchFile.production?.should be_true
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
it "should be
|
|
11
|
-
SwitchFile.
|
|
12
|
-
SwitchFile.config_path.should be_a(Pow::Base)
|
|
13
|
-
SwitchFile.config_path.to_s.should =~ /spec\/fixtures\/project\/.switch_file/
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
describe ".config_path=" do
|
|
18
|
-
it "should set the stored config_path to nil, if value is nil" do
|
|
19
|
-
SwitchFile.config_path = nil
|
|
20
|
-
SwitchFile.instance_variable_get(:@config_path).should be_nil
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
describe ".file_type_attributes" do
|
|
25
|
-
it "should be the set file_type_attributes, if available" do
|
|
26
|
-
attribute_hash = {shortcut: :s}
|
|
27
|
-
SwitchFile.file_type_attributes = [attribute_hash]
|
|
28
|
-
SwitchFile.file_type_attributes.should == [attribute_hash]
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it "should be the file_type_attributes from the config_path, if there are no set file_type_attributes" do
|
|
32
|
-
SwitchFile.file_type_attributes = nil
|
|
33
|
-
SwitchFile.config_path = Pow('spec/fixtures/project/.switch_file')
|
|
34
|
-
SwitchFile.file_type_attributes.should be_an(Array)
|
|
10
|
+
it "should be false when testing" do
|
|
11
|
+
SwitchFile.production?.should be_false
|
|
35
12
|
end
|
|
36
13
|
end
|
|
37
14
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -5,11 +5,9 @@ RSpec.configure do |config|
|
|
|
5
5
|
include SwitchFile
|
|
6
6
|
|
|
7
7
|
config.before :each do
|
|
8
|
+
SwitchFile.production = false
|
|
8
9
|
test_tmp_dir.delete! if test_tmp_dir.exists?
|
|
9
10
|
test_tmp_dir.create_directory
|
|
10
|
-
SwitchFile.config_path = Pow('spec/fixtures/project/.switch_file')
|
|
11
|
-
SwitchFile.file_type_attributes = nil
|
|
12
|
-
FileType.all = nil
|
|
13
11
|
end
|
|
14
12
|
|
|
15
13
|
def test_tmp_dir
|
data/switch_file.gemspec
CHANGED
|
@@ -18,10 +18,12 @@ Gem::Specification.new do |s|
|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
19
19
|
s.require_paths = ["lib"]
|
|
20
20
|
|
|
21
|
+
s.add_runtime_dependency "active_support"
|
|
21
22
|
s.add_runtime_dependency 'pow'
|
|
22
23
|
s.add_runtime_dependency 'thor'
|
|
23
24
|
s.add_runtime_dependency 'valuable'
|
|
24
25
|
|
|
26
|
+
s.add_development_dependency "coderay"
|
|
25
27
|
s.add_development_dependency "guard"
|
|
26
28
|
s.add_development_dependency "guard-rspec"
|
|
27
29
|
s.add_development_dependency "pry"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: switch_file
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,11 +9,22 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2012-04-
|
|
12
|
+
date: 2012-04-24 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: active_support
|
|
16
|
+
requirement: &77491890 !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: *77491890
|
|
14
25
|
- !ruby/object:Gem::Dependency
|
|
15
26
|
name: pow
|
|
16
|
-
requirement: &
|
|
27
|
+
requirement: &77491400 !ruby/object:Gem::Requirement
|
|
17
28
|
none: false
|
|
18
29
|
requirements:
|
|
19
30
|
- - ! '>='
|
|
@@ -21,10 +32,10 @@ dependencies:
|
|
|
21
32
|
version: '0'
|
|
22
33
|
type: :runtime
|
|
23
34
|
prerelease: false
|
|
24
|
-
version_requirements: *
|
|
35
|
+
version_requirements: *77491400
|
|
25
36
|
- !ruby/object:Gem::Dependency
|
|
26
37
|
name: thor
|
|
27
|
-
requirement: &
|
|
38
|
+
requirement: &77490900 !ruby/object:Gem::Requirement
|
|
28
39
|
none: false
|
|
29
40
|
requirements:
|
|
30
41
|
- - ! '>='
|
|
@@ -32,10 +43,10 @@ dependencies:
|
|
|
32
43
|
version: '0'
|
|
33
44
|
type: :runtime
|
|
34
45
|
prerelease: false
|
|
35
|
-
version_requirements: *
|
|
46
|
+
version_requirements: *77490900
|
|
36
47
|
- !ruby/object:Gem::Dependency
|
|
37
48
|
name: valuable
|
|
38
|
-
requirement: &
|
|
49
|
+
requirement: &77490360 !ruby/object:Gem::Requirement
|
|
39
50
|
none: false
|
|
40
51
|
requirements:
|
|
41
52
|
- - ! '>='
|
|
@@ -43,10 +54,21 @@ dependencies:
|
|
|
43
54
|
version: '0'
|
|
44
55
|
type: :runtime
|
|
45
56
|
prerelease: false
|
|
46
|
-
version_requirements: *
|
|
57
|
+
version_requirements: *77490360
|
|
58
|
+
- !ruby/object:Gem::Dependency
|
|
59
|
+
name: coderay
|
|
60
|
+
requirement: &77489780 !ruby/object:Gem::Requirement
|
|
61
|
+
none: false
|
|
62
|
+
requirements:
|
|
63
|
+
- - ! '>='
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: '0'
|
|
66
|
+
type: :development
|
|
67
|
+
prerelease: false
|
|
68
|
+
version_requirements: *77489780
|
|
47
69
|
- !ruby/object:Gem::Dependency
|
|
48
70
|
name: guard
|
|
49
|
-
requirement: &
|
|
71
|
+
requirement: &77489310 !ruby/object:Gem::Requirement
|
|
50
72
|
none: false
|
|
51
73
|
requirements:
|
|
52
74
|
- - ! '>='
|
|
@@ -54,10 +76,10 @@ dependencies:
|
|
|
54
76
|
version: '0'
|
|
55
77
|
type: :development
|
|
56
78
|
prerelease: false
|
|
57
|
-
version_requirements: *
|
|
79
|
+
version_requirements: *77489310
|
|
58
80
|
- !ruby/object:Gem::Dependency
|
|
59
81
|
name: guard-rspec
|
|
60
|
-
requirement: &
|
|
82
|
+
requirement: &77488450 !ruby/object:Gem::Requirement
|
|
61
83
|
none: false
|
|
62
84
|
requirements:
|
|
63
85
|
- - ! '>='
|
|
@@ -65,10 +87,10 @@ dependencies:
|
|
|
65
87
|
version: '0'
|
|
66
88
|
type: :development
|
|
67
89
|
prerelease: false
|
|
68
|
-
version_requirements: *
|
|
90
|
+
version_requirements: *77488450
|
|
69
91
|
- !ruby/object:Gem::Dependency
|
|
70
92
|
name: pry
|
|
71
|
-
requirement: &
|
|
93
|
+
requirement: &77487630 !ruby/object:Gem::Requirement
|
|
72
94
|
none: false
|
|
73
95
|
requirements:
|
|
74
96
|
- - ! '>='
|
|
@@ -76,10 +98,10 @@ dependencies:
|
|
|
76
98
|
version: '0'
|
|
77
99
|
type: :development
|
|
78
100
|
prerelease: false
|
|
79
|
-
version_requirements: *
|
|
101
|
+
version_requirements: *77487630
|
|
80
102
|
- !ruby/object:Gem::Dependency
|
|
81
103
|
name: rspec
|
|
82
|
-
requirement: &
|
|
104
|
+
requirement: &77487370 !ruby/object:Gem::Requirement
|
|
83
105
|
none: false
|
|
84
106
|
requirements:
|
|
85
107
|
- - ! '>='
|
|
@@ -87,7 +109,7 @@ dependencies:
|
|
|
87
109
|
version: '0'
|
|
88
110
|
type: :development
|
|
89
111
|
prerelease: false
|
|
90
|
-
version_requirements: *
|
|
112
|
+
version_requirements: *77487370
|
|
91
113
|
description: A configurable utility for jumping across files
|
|
92
114
|
email:
|
|
93
115
|
- gsmendoza@gmail.com
|
|
@@ -100,19 +122,25 @@ files:
|
|
|
100
122
|
- CHANGELOG.markdown
|
|
101
123
|
- Gemfile
|
|
102
124
|
- Guardfile
|
|
125
|
+
- README.markdown
|
|
103
126
|
- Rakefile
|
|
104
127
|
- bin/switch_file
|
|
128
|
+
- examples/gem.switch_file
|
|
129
|
+
- examples/rails.switch_file
|
|
105
130
|
- lib/switch_file.rb
|
|
106
131
|
- lib/switch_file/app.rb
|
|
132
|
+
- lib/switch_file/exception.rb
|
|
107
133
|
- lib/switch_file/file_type.rb
|
|
108
|
-
- lib/switch_file/
|
|
109
|
-
- lib/switch_file/
|
|
134
|
+
- lib/switch_file/project.rb
|
|
135
|
+
- lib/switch_file/source.rb
|
|
110
136
|
- lib/switch_file/version.rb
|
|
111
137
|
- spec/fixtures/project/.switch_file
|
|
138
|
+
- spec/fixtures/project/lib/some_class.rb
|
|
112
139
|
- spec/lib/switch_file/app_spec.rb
|
|
113
|
-
- spec/lib/switch_file/
|
|
140
|
+
- spec/lib/switch_file/exception_spec.rb
|
|
114
141
|
- spec/lib/switch_file/file_type_spec.rb
|
|
115
|
-
- spec/lib/switch_file/
|
|
142
|
+
- spec/lib/switch_file/project_spec.rb
|
|
143
|
+
- spec/lib/switch_file/source_spec.rb
|
|
116
144
|
- spec/lib/switch_file_spec.rb
|
|
117
145
|
- spec/spec_helper.rb
|
|
118
146
|
- switch_file.gemspec
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
module SwitchFile
|
|
2
|
-
# Path to a source file
|
|
3
|
-
class SourcePath < Valuable
|
|
4
|
-
has_value :value
|
|
5
|
-
|
|
6
|
-
def class_name
|
|
7
|
-
value.match(file_type.path_regex)[1]
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def file_type
|
|
11
|
-
FileType.all.detect{|file_type| value =~ file_type.path_regex }
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def prompt_message
|
|
15
|
-
file_type_options = FileType.all.map{|file_type|
|
|
16
|
-
"[#{file_type.shortcut}] #{file_type.name}: #{file_type.generate_open_command(self)}"
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
"Enter the shortcut of the file you want to open:\n\n#{file_type_options.join("\n")}\n\n"
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe SwitchFile::FileTypeShortcut do
|
|
4
|
-
describe "#file_type" do
|
|
5
|
-
it "should be the file_type with the same name as the short cut" do
|
|
6
|
-
file_type = FileType.new(shortcut: :s)
|
|
7
|
-
FileType.all = [file_type]
|
|
8
|
-
|
|
9
|
-
short_cut = described_class.new(value: :s)
|
|
10
|
-
short_cut.file_type.shortcut.should == :s
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
it "should be nil if there is no matching file type" do
|
|
14
|
-
file_type = FileType.new(shortcut: :l)
|
|
15
|
-
FileType.all = [file_type]
|
|
16
|
-
|
|
17
|
-
short_cut = described_class.new(value: :s)
|
|
18
|
-
short_cut.file_type.should be_nil
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe SwitchFile::SourcePath do
|
|
4
|
-
describe "#class_name" do
|
|
5
|
-
it "should be the part of the value that is the class name of the source path" do
|
|
6
|
-
FileType.all = [FileType.new(path_regex: %r{lib/(.*).rb$})]
|
|
7
|
-
|
|
8
|
-
source_path = SourcePath.new(value: '/home/user/project/lib/project/some_class.rb')
|
|
9
|
-
source_path.class_name.should == 'project/some_class'
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
describe "#file_type" do
|
|
14
|
-
it "should be the file type that matches the source path" do
|
|
15
|
-
file_type = FileType.new(path_regex: %r{lib/(.*).rb$})
|
|
16
|
-
FileType.all = [file_type]
|
|
17
|
-
|
|
18
|
-
source_path = SourcePath.new(value: '/home/user/project/lib/project/some_class.rb')
|
|
19
|
-
source_path.file_type.should == file_type
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
describe "#prompt_message" do
|
|
24
|
-
it "be the message to display if the user want to jump to file related to the source path" do
|
|
25
|
-
FileType.all = [
|
|
26
|
-
FileType.new(
|
|
27
|
-
name: :spec,
|
|
28
|
-
shortcut: :s,
|
|
29
|
-
open_command: lambda {|class_name| "geany spec/lib/#{class_name}_spec.rb"},
|
|
30
|
-
path_regex: %r{spec/lib/(.*)_spec.rb}
|
|
31
|
-
),
|
|
32
|
-
FileType.new(
|
|
33
|
-
name: :lib,
|
|
34
|
-
shortcut: :l,
|
|
35
|
-
open_command: lambda {|class_name| "geany lib/#{class_name}.rb"},
|
|
36
|
-
path_regex: %r{lib/(.*).rb}
|
|
37
|
-
)
|
|
38
|
-
]
|
|
39
|
-
|
|
40
|
-
source_path = SourcePath.new(value: '/home/user/project/lib/project/some_class.rb')
|
|
41
|
-
source_path.prompt_message.should == "Enter the shortcut of the file you want to open:\n\n[s] spec: geany spec/lib/project/some_class_spec.rb\n[l] lib: geany lib/project/some_class.rb\n\n"
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
end
|