tryrb 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5938ffd17f0b2e187e365bf48e959c3113189c82
4
- data.tar.gz: a00e195b696b75dc49183127cea6a3c4f534dbe4
3
+ metadata.gz: 7433c7c65a665d4cd86719f2867b18ff718a7679
4
+ data.tar.gz: 1e173c59ac6d9b45697d81da0899fc8a38200144
5
5
  SHA512:
6
- metadata.gz: 0fd3a62db0ff9417e7241d7ce645300f7bb7e13698ca49a10095c93268d8a627d850e70bc2038f6a1094ea4738cc3206b0aacba690fa55d24c02720e55e1d4c0
7
- data.tar.gz: 88a942b594d4a84202d5b8e47ad6e48075874024c73656e3403b58a19929e4da41bc04faa1e521e07a13ac06c2e8ca675971eaaf6cb7a7672ef7eba73fc15d78
6
+ metadata.gz: f91459a4b90f0dbb5564531ac0aa99363c685798ea49dbadeddbd1149cec9757f56d6e2dd42e9646542b92275769194c983c2dac38bd98ba9c1a048f1c13d688
7
+ data.tar.gz: 8873c2cedaf63641f6cc5f45b6df2d026891777e49f66d6664ce8248116dc5b48b2112456faa2ee475e5aab6a0ed270c97511745f1487450b703c8307fedcdc4
data/Rakefile CHANGED
@@ -1,11 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
- require 'rake/testtask'
3
2
 
4
- Rake::TestTask.new do |t|
5
- t.libs << "spec"
6
- t.test_files = FileList['spec/*_spec.rb']
7
- t.verbose = true
3
+ task :console do
4
+ require 'pry'
5
+ require 'tryrb'
6
+ ARGV.clear
7
+ Pry.start
8
8
  end
9
-
10
- desc "Run tests"
11
- task :default => :test
@@ -5,43 +5,66 @@ require 'tryrb/config'
5
5
  module TryRb
6
6
  class CLI < Thor
7
7
  def initialize(*)
8
- @config = TryRb::Config.instance
9
8
  super
10
9
  end
11
10
 
11
+ no_commands do
12
+ def conf
13
+ TryRb::Config.instance
14
+ end
15
+ end
16
+
12
17
  desc "create [FILENAME]", 'create a file to edit (short-cut alias: "c")'
13
18
  def create(key_name="")
14
19
  @key_name = key_name
15
- system([@config.editor, fullpath] * ' ')
20
+ system([conf.editor, fullpath] * ' ')
16
21
  end
17
-
18
22
  map 'c' => :create
19
23
 
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")'
24
+ option :last, :aliases => "-l", :banner => 'N', :desc => 'execute the last N file in the files you specify via filename, default is the 1', :type => :numeric, :lazy_default => 1
25
+ desc "exec [FILENAME]", 'execute a ruby script, default is last one of all files (short-cut alias: "e")'
22
26
  def exec(filename=nil)
23
- file_path = find_file(filename: filename, last_n: options[:last])
27
+ file_path = find_file(:filename => filename, :last_n => options[:last])
24
28
  abort "Can't find the file you want" unless file_path
25
29
  system(['ruby', file_path] * ' ')
26
30
  end
27
-
28
31
  map 'e' => :exec
29
32
 
33
+ desc 'config', "Config your editor and tmp_dir via command"
34
+ def config
35
+ default_tmp_path = "~/tmp/tryrb"
36
+ tmp_dir = ask("Please specify your dir of the tmp files(default: #{default_tmp_path}):")
37
+ tmp_dir = default_tmp_path if tmp_dir.empty?
38
+
39
+ default_editor = "vim"
40
+ editor = ask("Please specify your favorite editor(default: #{default_editor}):")
41
+ editor = default_editor if editor.empty?
42
+
43
+ config = {'tmp_dir' => tmp_dir, 'editor' => editor}
44
+
45
+ rc_path = conf.expanded_rc_path
46
+ File.open(rc_path, 'w') do |f|
47
+ f.write(config.to_yaml)
48
+ end
49
+
50
+ say("The config have been writen to ~/.tryrbrc", :green)
51
+ end
52
+
30
53
  private
31
54
 
32
55
  def find_file(options={})
33
56
  filename = options[:filename]
34
57
  last_n = options[:last_n] || 1
35
- names = Dir[File.join(@config.expanded_tmp_dir, '*.rb')]
58
+ names = Dir[File.join(conf.expanded_tmp_dir, '*.rb')]
36
59
  names = names.select { |n| n =~ /#{filename}\.rb$/ } if filename
37
60
  names[-last_n]
38
61
  end
39
62
 
40
63
  def fullpath
41
64
  filename = get_filename
42
- expanded_path = @config.expanded_tmp_dir
65
+ expanded_path = conf.expanded_tmp_dir
43
66
  FileUtils.mkdir_p expanded_path unless Dir.exists?(expanded_path)
44
- File.join @config.tmp_dir, filename
67
+ File.join conf.tmp_dir, filename
45
68
  rescue Errno::EEXIST => e
46
69
  abort "File #{e.message.split[-1]} exists. The tmp directory can't be created! Please delete the file first."
47
70
  end
@@ -3,42 +3,32 @@ require 'singleton'
3
3
  module TryRb
4
4
  class Config
5
5
  include Singleton
6
- attr_reader :path
7
- FILE_NAME = '.tryrbrc'
8
6
 
9
7
  %w[tmp_dir editor].each do |key|
10
8
  define_method key.to_sym do
11
- @data[key]
9
+ data[key]
12
10
  end
13
11
  end
14
12
 
15
- def initialize
16
- @path = File.join(File.expand_path('~'), FILE_NAME)
17
- @data = load_file
18
- end
19
-
20
- def path=(path)
21
- @path = path
22
- @data = load_file
23
- @path
13
+ def data
14
+ @data ||= load_file
24
15
  end
25
16
 
26
17
  def load_file
27
18
  require 'yaml'
28
- YAML.load_file(@path)
19
+ YAML.load_file(expanded_rc_path)
29
20
  rescue Errno::ENOENT
30
- default_config
21
+ TryRb::CLI::Shell::Color.new.say("Please run `tryrb config` to configure.", :red)
22
+ abort
23
+ end
24
+
25
+ def expanded_rc_path
26
+ File.expand_path('~/.tryrbrc')
31
27
  end
32
28
 
33
29
  def expanded_tmp_dir
34
30
  File.expand_path(tmp_dir)
35
31
  end
36
32
 
37
- private
38
-
39
- def default_config
40
- {'tmp_dir' => "~/tmp/tryrb",
41
- 'editor' => 'mvim'}
42
- end
43
33
  end
44
34
  end
@@ -2,7 +2,7 @@ module TryRb
2
2
  class Version
3
3
  MAJOR = 1
4
4
  MINOR = 1
5
- PATCH = 0
5
+ PATCH = 1
6
6
  PRE = nil
7
7
 
8
8
  class << self
@@ -3,12 +3,12 @@ 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')
6
+ TryRb::Config.instance.stub(:expanded_rc_path) { fixture_file_path('tryrbrc') }
7
7
  @cli = TryRb::CLI.new
8
8
  end
9
9
 
10
10
  describe '#create' do
11
- it 'create a file 201401010000.rb' do
11
+ it 'create a file 20140101000000.rb' do
12
12
  expect(@cli).to receive(:system).with('emacs ~/tmp/foo/tryrb/20140101000000.rb')
13
13
  @cli.create
14
14
  end
@@ -16,42 +16,78 @@ describe TryRb::CLI do
16
16
  expect(@cli).to receive(:system).with('emacs ~/tmp/foo/tryrb/20140101000000_foo.rb')
17
17
  @cli.create 'foo'
18
18
  end
19
+ it 'abort caused by existing file' do
20
+ FakeFS.activate!
21
+ FileUtils.mkdir_p '/tmp'
22
+ FileUtils.touch '/tmp/foo'
23
+ @cli.stub(:system)
24
+ expect(TryRb::Config.instance).to receive(:expanded_tmp_dir).and_return('/tmp/foo/tryrb')
25
+ expect(@cli).to receive(:abort).with("File foo exists. The tmp directory can't be created! Please delete the file first.")
26
+ @cli.create
27
+ FileUtils.rm_rf '/tmp/foo'
28
+ FakeFS.deactivate!
29
+ end
19
30
  end
20
31
 
21
32
  describe '#exec' do
22
33
  before :each do
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')
34
+ FakeFS.activate!
35
+ FileUtils.mkdir_p '/tmp/foo/tryrb'
36
+ FileUtils.touch '/tmp/foo/tryrb/20140101000000_foo.rb'
37
+ FileUtils.touch '/tmp/foo/tryrb/20140102000000_foo.rb'
38
+ FileUtils.touch '/tmp/foo/tryrb/20140103000000_bar.rb'
39
+ expect(TryRb::Config.instance).to receive(:expanded_tmp_dir).and_return('/tmp/foo/tryrb')
28
40
  end
29
41
  after :each do
30
- FileUtils.rm_rf 'tmp/foo'
42
+ FileUtils.rm_rf '/tmp/foo'
43
+ FakeFS.deactivate!
31
44
  end
32
45
  it 'exec last file' do
33
- expect(@cli).to receive(:system).with('ruby tmp/foo/tryrb/20140103000000_bar.rb')
46
+ expect(@cli).to receive(:system).with('ruby /tmp/foo/tryrb/20140103000000_bar.rb')
34
47
  @cli.exec
35
48
  end
36
49
  it 'exec last file with filename' do
37
- expect(@cli).to receive(:system).with('ruby tmp/foo/tryrb/20140102000000_foo.rb')
50
+ expect(@cli).to receive(:system).with('ruby /tmp/foo/tryrb/20140102000000_foo.rb')
38
51
  @cli.exec 'foo'
39
52
  end
40
53
  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')
54
+ expect(@cli).to receive(:options).and_return({:last => 2})
55
+ expect(@cli).to receive(:system).with('ruby /tmp/foo/tryrb/20140102000000_foo.rb')
43
56
  @cli.exec
44
57
  end
45
58
  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')
59
+ expect(@cli).to receive(:options).and_return({:last => 2})
60
+ expect(@cli).to receive(:system).with('ruby /tmp/foo/tryrb/20140101000000_foo.rb')
48
61
  @cli.exec 'foo'
49
62
  end
50
63
  it 'abort because there is no file' do
51
64
  @cli.stub(:system)
52
- expect(@cli).to receive(:options).and_return({last: 4})
65
+ expect(@cli).to receive(:options).and_return({:last => 4})
53
66
  expect(@cli).to receive(:abort).with("Can't find the file you want")
54
67
  @cli.exec
55
68
  end
56
69
  end
70
+
71
+ describe '#config' do
72
+ before :each do
73
+ @shell = TryRb::CLI::Shell::Basic.new
74
+ FakeFS.activate!
75
+ end
76
+ after :each do
77
+ FileUtils.rm '/tmp/.tryrbrc'
78
+ FakeFS.deactivate!
79
+ end
80
+ it "save config data in tryrbrc" do
81
+ cli = TryRb::CLI.new
82
+ expect(TryRb::Config.instance).to receive(:expanded_rc_path).and_return('/tmp/.tryrbrc')
83
+ expect(cli).to receive(:ask).with("Please specify your dir of the tmp files(default: ~/tmp/tryrb):").and_return('~/foo/tryrb')
84
+ expect(cli).to receive(:ask).with("Please specify your favorite editor(default: vim):").and_return('emacs')
85
+ cli.config
86
+ if RUBY_VERSION < '2.1.0'
87
+ expect(File.open('/tmp/.tryrbrc').read).to eq("---\ntmp_dir: ~/foo/tryrb\neditor: emacs\n")
88
+ else
89
+ expect(File.open('/tmp/.tryrbrc').read).to eq("---\ntmp_dir: \"~/foo/tryrb\"\neditor: emacs\n")
90
+ end
91
+ end
92
+ end
57
93
  end
@@ -15,7 +15,7 @@ describe TryRb::Config do
15
15
  describe '#tmp_dir' do
16
16
  it "returns tmp dir" do
17
17
  config = TryRb::Config.instance
18
- config.path = fixture_file_path('tryrbrc')
18
+ config.stub(:expanded_rc_path) { fixture_file_path('tryrbrc') }
19
19
  expect(config.tmp_dir).to eq '~/tmp/foo/tryrb'
20
20
  end
21
21
  end
@@ -23,37 +23,26 @@ describe TryRb::Config do
23
23
  describe '#editor' do
24
24
  it "returns editor" do
25
25
  config = TryRb::Config.instance
26
- config.path = fixture_file_path('tryrbrc')
26
+ config.stub(:expanded_rc_path) { fixture_file_path('tryrbrc') }
27
27
  expect(config.editor).to eq 'emacs'
28
28
  end
29
29
  end
30
30
 
31
- describe '#path=' do
32
- it 'overrides path' do
33
- config = TryRb::Config.instance
34
- config.path = fixture_file_path('foorc')
35
- expect(config.path).to eq fixture_file_path('foorc')
36
- end
37
- it 'reloads data' do
38
- config = TryRb::Config.instance
39
- config.path = fixture_file_path('tryrbrc')
40
- expect(config.load_file.keys).to eq %w[tmp_dir editor]
41
- end
42
- end
43
-
44
31
  describe 'load_file' do
45
32
  context 'when file exists at path' do
46
33
  it 'loads data from the file' do
47
34
  config = TryRb::Config.instance
48
- config.path = fixture_file_path('tryrbrc')
35
+ config.stub(:expanded_rc_path) { fixture_file_path('tryrbrc') }
49
36
  expect(config.tmp_dir).to eq '~/tmp/foo/tryrb'
50
37
  end
51
38
  end
52
39
  context 'when file does not exist' do
53
40
  it 'loads default data' do
54
41
  config = TryRb::Config.instance
55
- config.path = fixture_file_path('foorc')
56
- expect(config.tmp_dir).to eq '~/tmp/tryrb'
42
+ config.stub(:expanded_rc_path) { fixture_file_path('foorc') }
43
+ expect(config).to receive(:abort)
44
+ TryRb::CLI::Shell::Color.any_instance.stub(:say)
45
+ config.load_file
57
46
  end
58
47
  end
59
48
  end
@@ -1,5 +1,19 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+
9
+ SimpleCov.start do
10
+ add_filter '/spec/'
11
+ minimum_coverage(99)
12
+ end
13
+
1
14
  require 'tryrb'
2
15
  require 'rspec'
16
+ require 'fakefs/spec_helpers'
3
17
 
4
18
  RSpec.configure do |config|
5
19
  config.expect_with :rspec do |c|
@@ -16,5 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.executables << 'tryrb'
17
17
  s.require_paths = ["lib"]
18
18
  s.homepage = 'https://github.com/tony612/tryrb'
19
- s.add_dependency 'thor'
19
+ s.add_dependency 'thor', '~> 0.18.1'
20
+ s.add_development_dependency 'bundler', '~> 1.0'
21
+ s.post_install_message = "Please use `tryrb config` to configure."
20
22
  end
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tryrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
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-07 00:00:00.000000000 Z
11
+ date: 2014-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 0.18.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 0.18.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
27
41
  description: Try ruby code in a temporary file created.
28
42
  email: h.bing612@gmail.com
29
43
  executables:
@@ -47,7 +61,7 @@ homepage: https://github.com/tony612/tryrb
47
61
  licenses:
48
62
  - MIT
49
63
  metadata: {}
50
- post_install_message:
64
+ post_install_message: Please use `tryrb config` to configure.
51
65
  rdoc_options: []
52
66
  require_paths:
53
67
  - lib