rails-sh 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/rails/sh.rb +8 -49
- data/lib/rails/sh/bundler.rb +22 -0
- data/lib/rails/sh/command.rb +15 -1
- data/lib/rails/sh/commands.rb +32 -13
- data/lib/rails/sh/{hook_for_fork.rb → forkable.rb} +14 -1
- data/lib/rails/sh/rails.rb +35 -0
- data/lib/rails/sh/rake.rb +16 -12
- data/rails-sh.gemspec +4 -2
- data/spec/rails/sh/command_spec.rb +32 -2
- data/spec/rails/sh_spec.rb +1 -22
- metadata +5 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.4.0
|
data/lib/rails/sh.rb
CHANGED
@@ -1,28 +1,20 @@
|
|
1
1
|
require 'readline'
|
2
|
-
require 'rails/sh/
|
2
|
+
require 'rails/sh/forkable'
|
3
|
+
require 'rails/sh/rails'
|
3
4
|
require 'rails/sh/rake'
|
4
5
|
require 'rails/sh/command'
|
5
|
-
require 'rails/sh/
|
6
|
+
require 'rails/sh/bundler'
|
6
7
|
|
7
8
|
module Rails
|
8
9
|
module Sh
|
9
|
-
extend HookForFork
|
10
|
-
|
11
|
-
RAILS_SUB_COMMANDS = ['generate', 'destroy', 'plugin', 'benchmarker', 'profiler',
|
12
|
-
'console', 'server', 'dbconsole', 'application', 'runner']
|
13
|
-
|
14
10
|
class << self
|
15
11
|
def start
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
after_fork do
|
20
|
-
ActiveRecord::Base.establish_connection if defined?(ActiveRecord::Base)
|
21
|
-
end
|
12
|
+
::Rails::Sh::Rails.init
|
13
|
+
::Rails::Sh::Rake.init
|
22
14
|
|
23
|
-
|
15
|
+
require 'rails/sh/commands'
|
24
16
|
|
25
|
-
puts "Rails.env: #{Rails.env}"
|
17
|
+
puts "Rails.env: #{::Rails.env}"
|
26
18
|
puts "type `help` to print help"
|
27
19
|
|
28
20
|
setup_readline
|
@@ -40,25 +32,9 @@ module Rails
|
|
40
32
|
end
|
41
33
|
end
|
42
34
|
|
43
|
-
def init_rake
|
44
|
-
Rails::Sh::Rake.init
|
45
|
-
Rails::Sh::Rake.before_fork do
|
46
|
-
run_before_fork
|
47
|
-
end
|
48
|
-
Rails::Sh::Rake.after_fork do
|
49
|
-
run_after_fork
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
35
|
def setup_readline
|
54
36
|
Readline.basic_word_break_characters = ""
|
55
|
-
Readline.completion_proc =
|
56
|
-
(
|
57
|
-
Command.command_names.map { |name| name.to_s } +
|
58
|
-
RAILS_SUB_COMMANDS.map { |name| "rails #{name}" } +
|
59
|
-
Rails::Sh::Rake.task_names.map { |name| "rake #{name}" }
|
60
|
-
).grep(/#{Regexp.quote(word)}/)
|
61
|
-
end
|
37
|
+
Readline.completion_proc = Command.completion_proc
|
62
38
|
end
|
63
39
|
|
64
40
|
def execute(line)
|
@@ -71,23 +47,6 @@ module Rails
|
|
71
47
|
puts "\e[41mCommand not found\e[0m"
|
72
48
|
end
|
73
49
|
end
|
74
|
-
|
75
|
-
def execute_rails_command(line)
|
76
|
-
run_before_fork
|
77
|
-
pid = fork do
|
78
|
-
run_after_fork
|
79
|
-
reload!
|
80
|
-
ARGV.clear
|
81
|
-
ARGV.concat line.split(/\s+/)
|
82
|
-
puts "\e[42m$ rails #{ARGV.join(" ")}\e[0m"
|
83
|
-
require 'rails/commands'
|
84
|
-
end
|
85
|
-
Process.waitpid(pid)
|
86
|
-
end
|
87
|
-
|
88
|
-
def reload!
|
89
|
-
ActionDispatch::Callbacks.new(Proc.new {}, false).call({})
|
90
|
-
end
|
91
50
|
end
|
92
51
|
end
|
93
52
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Rails
|
2
|
+
module Sh
|
3
|
+
module Bundler
|
4
|
+
extend Forkable
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def _invoke(line)
|
8
|
+
line ||= 'install'
|
9
|
+
command, *args = line.split(/\s+/)
|
10
|
+
ARGV.clear
|
11
|
+
ARGV.concat args
|
12
|
+
require 'bundler/cli'
|
13
|
+
::Bundler::CLI.new.send(command.to_sym)
|
14
|
+
end
|
15
|
+
|
16
|
+
def sub_commands
|
17
|
+
%w(exec install update open package config check list show console open viz init gem)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/rails/sh/command.rb
CHANGED
@@ -9,6 +9,7 @@ module Rails
|
|
9
9
|
def define(*names, &block)
|
10
10
|
names.each do |name|
|
11
11
|
commands[name.to_sym] = block
|
12
|
+
completions << name.to_s
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
@@ -25,7 +26,20 @@ module Rails
|
|
25
26
|
end
|
26
27
|
|
27
28
|
def [](name)
|
28
|
-
commands[name]
|
29
|
+
commands[name.to_sym]
|
30
|
+
end
|
31
|
+
|
32
|
+
def completion_proc
|
33
|
+
lambda { |line| completions.grep(/#{Regexp.quote(line)}/) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def completions
|
37
|
+
@completions ||= []
|
38
|
+
end
|
39
|
+
|
40
|
+
def clear
|
41
|
+
commands.clear
|
42
|
+
completions.clear
|
29
43
|
end
|
30
44
|
end
|
31
45
|
end
|
data/lib/rails/sh/commands.rb
CHANGED
@@ -1,25 +1,36 @@
|
|
1
1
|
include Rails::Sh
|
2
2
|
|
3
3
|
Command.define 'help' do
|
4
|
+
print "\e[36m"
|
4
5
|
puts <<HELP
|
5
|
-
|
6
|
-
rails ARG
|
7
|
-
rake TASK
|
8
|
-
t, tasks PATTERN
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
help print help
|
7
|
+
rails ARG execute rails command
|
8
|
+
rake TASK execute rake task
|
9
|
+
t, tasks PATTERN print rake tasks
|
10
|
+
bundle execute bundler command
|
11
|
+
exit exit from rails-sh
|
12
|
+
restart restart rails-sh
|
13
|
+
reload reload the environment
|
14
|
+
! execute a system command
|
15
|
+
eval eval as ruby script
|
14
16
|
HELP
|
17
|
+
print "\e[0m"
|
15
18
|
end
|
16
19
|
|
17
20
|
Command.define 'rails' do |arg|
|
18
|
-
Rails::Sh.
|
21
|
+
Rails::Sh::Rails.invoke(arg)
|
22
|
+
end
|
23
|
+
|
24
|
+
Rails::Sh::Rails.sub_commands.map do |c|
|
25
|
+
Command.completions << "rails #{c}"
|
19
26
|
end
|
20
27
|
|
21
28
|
Command.define 'rake' do |arg|
|
22
|
-
Rails::Sh::Rake.invoke(arg
|
29
|
+
Rails::Sh::Rake.invoke(arg)
|
30
|
+
end
|
31
|
+
|
32
|
+
Rails::Sh::Rake.task_names.map do |name|
|
33
|
+
Command.completions << "rake #{name}"
|
23
34
|
end
|
24
35
|
|
25
36
|
Command.define 'tasks', 't' do |arg|
|
@@ -27,7 +38,15 @@ Command.define 'tasks', 't' do |arg|
|
|
27
38
|
Rake.application.display_tasks_and_comments
|
28
39
|
end
|
29
40
|
|
30
|
-
Command.define '
|
41
|
+
Command.define 'bundle' do |arg|
|
42
|
+
Rails::Sh::Bundler.invoke(arg)
|
43
|
+
end
|
44
|
+
|
45
|
+
Rails::Sh::Bundler.sub_commands.map do |c|
|
46
|
+
Command.completions << "bundle #{c}"
|
47
|
+
end
|
48
|
+
|
49
|
+
Command.define '!' do |arg|
|
31
50
|
system arg
|
32
51
|
end
|
33
52
|
|
@@ -41,7 +60,7 @@ Command.define 'restart' do
|
|
41
60
|
end
|
42
61
|
|
43
62
|
Command.define 'reload' do
|
44
|
-
Rails::Sh.reload!
|
63
|
+
Rails::Sh::Rails.reload!
|
45
64
|
end
|
46
65
|
|
47
66
|
Command.define 'exit' do
|
@@ -1,6 +1,19 @@
|
|
1
1
|
module Rails
|
2
2
|
module Sh
|
3
|
-
module
|
3
|
+
module Forkable
|
4
|
+
def invoke(line)
|
5
|
+
run_before_fork
|
6
|
+
pid = fork do
|
7
|
+
run_after_fork
|
8
|
+
_invoke(line)
|
9
|
+
end
|
10
|
+
Process.waitpid(pid)
|
11
|
+
end
|
12
|
+
|
13
|
+
def _invoke
|
14
|
+
raise NotImplementedError
|
15
|
+
end
|
16
|
+
|
4
17
|
def before_fork(&block)
|
5
18
|
@before_fork = block
|
6
19
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Rails
|
2
|
+
module Sh
|
3
|
+
module Rails
|
4
|
+
extend Forkable
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def init
|
8
|
+
before_fork do
|
9
|
+
ActiveRecord::Base.remove_connection if defined?(ActiveRecord::Base)
|
10
|
+
end
|
11
|
+
after_fork do
|
12
|
+
ActiveRecord::Base.establish_connection if defined?(ActiveRecord::Base)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def _invoke(line)
|
17
|
+
reload!
|
18
|
+
ARGV.clear
|
19
|
+
ARGV.concat line.split(/\s+/)
|
20
|
+
puts "\e[42m$ rails #{ARGV.join(" ")}\e[0m"
|
21
|
+
require 'rails/commands'
|
22
|
+
end
|
23
|
+
|
24
|
+
def reload!
|
25
|
+
ActionDispatch::Callbacks.new(Proc.new {}, false).call({})
|
26
|
+
end
|
27
|
+
|
28
|
+
def sub_commands
|
29
|
+
%w(generate destroy plugin benchmarker profiler
|
30
|
+
console server dbconsole application runner)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/rails/sh/rake.rb
CHANGED
@@ -4,11 +4,19 @@ require 'stringio'
|
|
4
4
|
module Rails
|
5
5
|
module Sh
|
6
6
|
module Rake
|
7
|
-
extend
|
7
|
+
extend Forkable
|
8
8
|
|
9
9
|
class << self
|
10
10
|
def init
|
11
11
|
$stdout = StringIO.new
|
12
|
+
|
13
|
+
before_fork do
|
14
|
+
ActiveRecord::Base.remove_connection if defined?(ActiveRecord::Base)
|
15
|
+
end
|
16
|
+
after_fork do
|
17
|
+
ActiveRecord::Base.establish_connection if defined?(ActiveRecord::Base)
|
18
|
+
end
|
19
|
+
|
12
20
|
::Rake.application = ::Rake::Application.new
|
13
21
|
::Rake.application.init
|
14
22
|
::Rake.application.load_rakefile
|
@@ -17,19 +25,15 @@ module Rails
|
|
17
25
|
$stdout = STDOUT
|
18
26
|
end
|
19
27
|
|
20
|
-
def
|
28
|
+
def _invoke(line)
|
29
|
+
line ||= 'default'
|
21
30
|
name, *args = line.split(/\s+/)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
env, value = arg.split('=')
|
27
|
-
next unless env && !env.empty? && value && !value.empty?
|
28
|
-
ENV[env] = value
|
29
|
-
end
|
30
|
-
::Rake.application[name].invoke
|
31
|
+
args.each do |arg|
|
32
|
+
env, value = arg.split('=')
|
33
|
+
next unless env && !env.empty? && value && !value.empty?
|
34
|
+
ENV[env] = value
|
31
35
|
end
|
32
|
-
|
36
|
+
::Rake.application[name].invoke
|
33
37
|
end
|
34
38
|
|
35
39
|
def task_names
|
data/rails-sh.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rails-sh}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["jugyo"]
|
@@ -29,9 +29,11 @@ Gem::Specification.new do |s|
|
|
29
29
|
"VERSION",
|
30
30
|
"bin/rails-sh",
|
31
31
|
"lib/rails/sh.rb",
|
32
|
+
"lib/rails/sh/bundler.rb",
|
32
33
|
"lib/rails/sh/command.rb",
|
33
34
|
"lib/rails/sh/commands.rb",
|
34
|
-
"lib/rails/sh/
|
35
|
+
"lib/rails/sh/forkable.rb",
|
36
|
+
"lib/rails/sh/rails.rb",
|
35
37
|
"lib/rails/sh/rake.rb",
|
36
38
|
"rails-sh.gemspec",
|
37
39
|
"spec/rails/sh/command_spec.rb",
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Rails::Sh::Command do
|
4
4
|
before do
|
5
|
-
Rails::Sh::Command.
|
5
|
+
Rails::Sh::Command.clear
|
6
6
|
end
|
7
7
|
|
8
8
|
context 'when define a command' do
|
@@ -23,10 +23,40 @@ describe Rails::Sh::Command do
|
|
23
23
|
Rails::Sh::Command.command_names.should =~ [:foo]
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
describe 'Command.[]' do
|
27
27
|
it 'can get a command' do
|
28
28
|
Rails::Sh::Command['foo'].should eq(@block)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
32
|
+
|
33
|
+
describe '.completions' do
|
34
|
+
context 'when command does not exist' do
|
35
|
+
it 'completions is empty' do
|
36
|
+
Rails::Sh::Command.completions.should be_empty
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when commands exist' do
|
41
|
+
before do
|
42
|
+
Rails::Sh::Command.define('foo') {}
|
43
|
+
Rails::Sh::Command.define('bar') {}
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'completions is empty' do
|
47
|
+
Rails::Sh::Command.completions.should =~ ['foo', 'bar']
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe '.completion_proc' do
|
53
|
+
before do
|
54
|
+
['foo', 'rails generate', 'rake routes', 'rake spec'].each { |c| Rails::Sh::Command.completions << c }
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'return completions' do
|
58
|
+
Rails::Sh::Command.completion_proc.call('foo').should =~ ['foo']
|
59
|
+
Rails::Sh::Command.completion_proc.call('rake').should =~ ['rake routes', 'rake spec']
|
60
|
+
end
|
61
|
+
end
|
32
62
|
end
|
data/spec/rails/sh_spec.rb
CHANGED
@@ -1,26 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Rails::Sh do
|
4
|
-
|
5
|
-
before do
|
6
|
-
@block = lambda {}
|
7
|
-
Rails::Sh::Command.define('foo', &@block)
|
8
|
-
end
|
9
|
-
|
10
|
-
['foo'].each do |line|
|
11
|
-
it "the command should be executed if the line is '#{line}'" do
|
12
|
-
@block.should_receive(:call).with(nil).once
|
13
|
-
Rails::Sh.should_receive(:execute_rails_command).with(line).exactly(0).times
|
14
|
-
Rails::Sh.execute(line)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
['fo', 'bar'].each do |line|
|
19
|
-
it "the command should not be executed if the line is '#{line}'" do
|
20
|
-
@block.should_receive(:call).with(nil).exactly(0).times
|
21
|
-
Rails::Sh.should_receive(:execute_rails_command).with(line).once
|
22
|
-
Rails::Sh.execute(line)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
4
|
+
pending
|
26
5
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rails-sh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.4.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- jugyo
|
@@ -77,9 +77,11 @@ files:
|
|
77
77
|
- VERSION
|
78
78
|
- bin/rails-sh
|
79
79
|
- lib/rails/sh.rb
|
80
|
+
- lib/rails/sh/bundler.rb
|
80
81
|
- lib/rails/sh/command.rb
|
81
82
|
- lib/rails/sh/commands.rb
|
82
|
-
- lib/rails/sh/
|
83
|
+
- lib/rails/sh/forkable.rb
|
84
|
+
- lib/rails/sh/rails.rb
|
83
85
|
- lib/rails/sh/rake.rb
|
84
86
|
- rails-sh.gemspec
|
85
87
|
- spec/rails/sh/command_spec.rb
|
@@ -99,7 +101,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
101
|
requirements:
|
100
102
|
- - ">="
|
101
103
|
- !ruby/object:Gem::Version
|
102
|
-
hash: -
|
104
|
+
hash: -857922454327872908
|
103
105
|
segments:
|
104
106
|
- 0
|
105
107
|
version: "0"
|