tryrb 1.0.2 → 1.1.0
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.
- checksums.yaml +4 -4
- data/lib/tryrb/cli.rb +39 -13
- data/lib/tryrb/config.rb +4 -0
- data/lib/tryrb/version.rb +2 -2
- data/spec/cli_spec.rb +41 -66
- data/tryrb.gemspec +1 -0
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5938ffd17f0b2e187e365bf48e959c3113189c82
|
4
|
+
data.tar.gz: a00e195b696b75dc49183127cea6a3c4f534dbe4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0fd3a62db0ff9417e7241d7ce645300f7bb7e13698ca49a10095c93268d8a627d850e70bc2038f6a1094ea4738cc3206b0aacba690fa55d24c02720e55e1d4c0
|
7
|
+
data.tar.gz: 88a942b594d4a84202d5b8e47ad6e48075874024c73656e3403b58a19929e4da41bc04faa1e521e07a13ac06c2e8ca675971eaaf6cb7a7672ef7eba73fc15d78
|
data/lib/tryrb/cli.rb
CHANGED
@@ -1,18 +1,45 @@
|
|
1
|
-
require 'tryrb/config'
|
2
1
|
require 'fileutils'
|
2
|
+
require 'thor'
|
3
|
+
require 'tryrb/config'
|
3
4
|
|
4
5
|
module TryRb
|
5
|
-
class CLI
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
class CLI < Thor
|
7
|
+
def initialize(*)
|
8
|
+
@config = TryRb::Config.instance
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
desc "create [FILENAME]", 'create a file to edit (short-cut alias: "c")'
|
13
|
+
def create(key_name="")
|
14
|
+
@key_name = key_name
|
15
|
+
system([@config.editor, fullpath] * ' ')
|
16
|
+
end
|
17
|
+
|
18
|
+
map 'c' => :create
|
19
|
+
|
20
|
+
option :last, aliases: "-l", banner: 'N', desc: 'open the last N file in the files you specify via filename, default is the 1', type: :numeric, lazy_default: 1
|
21
|
+
desc "exec [FILENAME]", 'open a file to edit, default is last one of all files (short-cut alias: "e")'
|
22
|
+
def exec(filename=nil)
|
23
|
+
file_path = find_file(filename: filename, last_n: options[:last])
|
24
|
+
abort "Can't find the file you want" unless file_path
|
25
|
+
system(['ruby', file_path] * ' ')
|
26
|
+
end
|
27
|
+
|
28
|
+
map 'e' => :exec
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def find_file(options={})
|
33
|
+
filename = options[:filename]
|
34
|
+
last_n = options[:last_n] || 1
|
35
|
+
names = Dir[File.join(@config.expanded_tmp_dir, '*.rb')]
|
36
|
+
names = names.select { |n| n =~ /#{filename}\.rb$/ } if filename
|
37
|
+
names[-last_n]
|
11
38
|
end
|
12
39
|
|
13
|
-
def fullpath
|
14
|
-
filename = get_filename
|
15
|
-
expanded_path =
|
40
|
+
def fullpath
|
41
|
+
filename = get_filename
|
42
|
+
expanded_path = @config.expanded_tmp_dir
|
16
43
|
FileUtils.mkdir_p expanded_path unless Dir.exists?(expanded_path)
|
17
44
|
File.join @config.tmp_dir, filename
|
18
45
|
rescue Errno::EEXIST => e
|
@@ -27,11 +54,10 @@ module TryRb
|
|
27
54
|
'.rb'
|
28
55
|
end
|
29
56
|
|
30
|
-
def get_filename
|
57
|
+
def get_filename
|
31
58
|
parts = [filename_prefix]
|
32
|
-
parts <<
|
59
|
+
parts << @key_name.gsub(/\.rb$/, '') unless @key_name.empty?
|
33
60
|
parts * '_' + filename_extname
|
34
61
|
end
|
35
|
-
end
|
36
62
|
end
|
37
63
|
end
|
data/lib/tryrb/config.rb
CHANGED
data/lib/tryrb/version.rb
CHANGED
data/spec/cli_spec.rb
CHANGED
@@ -3,80 +3,55 @@ require 'helper'
|
|
3
3
|
describe TryRb::CLI do
|
4
4
|
before :each do
|
5
5
|
Time.stub(:now) { Date.parse('2014-1-1').to_time }
|
6
|
+
TryRb::Config.instance.path = fixture_file_path('tryrbrc')
|
7
|
+
@cli = TryRb::CLI.new
|
6
8
|
end
|
7
|
-
describe '::start' do
|
8
|
-
it "should call system method with an editor" do
|
9
|
-
TryRb::Config.instance.path = fixture_file_path('tryrbrc')
|
10
|
-
expect(TryRb::CLI).to receive(:system).with('emacs ~/tmp/foo/tryrb/20140101000000_filename.rb')
|
11
|
-
TryRb::CLI.start(["filename"])
|
12
|
-
end
|
13
9
|
|
14
|
-
|
15
|
-
|
16
|
-
expect(
|
17
|
-
|
10
|
+
describe '#create' do
|
11
|
+
it 'create a file 201401010000.rb' do
|
12
|
+
expect(@cli).to receive(:system).with('emacs ~/tmp/foo/tryrb/20140101000000.rb')
|
13
|
+
@cli.create
|
14
|
+
end
|
15
|
+
it 'create a file 20140101000000_foo.rb' do
|
16
|
+
expect(@cli).to receive(:system).with('emacs ~/tmp/foo/tryrb/20140101000000_foo.rb')
|
17
|
+
@cli.create 'foo'
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
describe '
|
21
|
+
describe '#exec' do
|
22
22
|
before :each do
|
23
|
-
|
24
|
-
FileUtils.
|
23
|
+
FileUtils.mkdir_p 'tmp/foo/tryrb'
|
24
|
+
FileUtils.touch 'tmp/foo/tryrb/20140101000000_foo.rb'
|
25
|
+
FileUtils.touch 'tmp/foo/tryrb/20140102000000_foo.rb'
|
26
|
+
FileUtils.touch 'tmp/foo/tryrb/20140103000000_bar.rb'
|
27
|
+
expect(TryRb::Config.instance).to receive(:expanded_tmp_dir).and_return('tmp/foo/tryrb')
|
25
28
|
end
|
26
29
|
after :each do
|
27
|
-
FileUtils.
|
28
|
-
end
|
29
|
-
it '
|
30
|
-
expect(
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
context 'when there is a file which has same name with dir' do
|
55
|
-
it 'aborts' do
|
56
|
-
TryRb::Config.instance.stub(:tmp_dir) { project_path + '/tmp/foo/tryrb' }
|
57
|
-
FileUtils.touch project_path + '/tmp/foo'
|
58
|
-
expect(TryRb::CLI).to receive(:abort).with(/File .+ exists. The tmp directory can't be created! Please delete the file first./)
|
59
|
-
TryRb::CLI.fullpath('bar')
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
describe '#filename_prefix' do
|
65
|
-
it "return now string" do
|
66
|
-
expect(TryRb::CLI.filename_prefix).to eq("20140101000000")
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
describe '#get_filename' do
|
71
|
-
context 'when name is given' do
|
72
|
-
it 'returns string containing the name' do
|
73
|
-
expect(TryRb::CLI.get_filename('bar')).to eq('20140101000000_bar.rb')
|
74
|
-
end
|
75
|
-
end
|
76
|
-
context 'when name is not given' do
|
77
|
-
it 'returns just time string' do
|
78
|
-
expect(TryRb::CLI.get_filename(nil)).to eq('20140101000000.rb')
|
79
|
-
end
|
30
|
+
FileUtils.rm_rf 'tmp/foo'
|
31
|
+
end
|
32
|
+
it 'exec last file' do
|
33
|
+
expect(@cli).to receive(:system).with('ruby tmp/foo/tryrb/20140103000000_bar.rb')
|
34
|
+
@cli.exec
|
35
|
+
end
|
36
|
+
it 'exec last file with filename' do
|
37
|
+
expect(@cli).to receive(:system).with('ruby tmp/foo/tryrb/20140102000000_foo.rb')
|
38
|
+
@cli.exec 'foo'
|
39
|
+
end
|
40
|
+
it 'exec last second file' do
|
41
|
+
expect(@cli).to receive(:options).and_return({last: 2})
|
42
|
+
expect(@cli).to receive(:system).with('ruby tmp/foo/tryrb/20140102000000_foo.rb')
|
43
|
+
@cli.exec
|
44
|
+
end
|
45
|
+
it 'exec last second file with filename' do
|
46
|
+
expect(@cli).to receive(:options).and_return({last: 2})
|
47
|
+
expect(@cli).to receive(:system).with('ruby tmp/foo/tryrb/20140101000000_foo.rb')
|
48
|
+
@cli.exec 'foo'
|
49
|
+
end
|
50
|
+
it 'abort because there is no file' do
|
51
|
+
@cli.stub(:system)
|
52
|
+
expect(@cli).to receive(:options).and_return({last: 4})
|
53
|
+
expect(@cli).to receive(:abort).with("Can't find the file you want")
|
54
|
+
@cli.exec
|
80
55
|
end
|
81
56
|
end
|
82
57
|
end
|
data/tryrb.gemspec
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tryrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tony Han
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
12
|
-
dependencies:
|
11
|
+
date: 2014-01-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
description: Try ruby code in a temporary file created.
|
14
28
|
email: h.bing612@gmail.com
|
15
29
|
executables:
|