rbsh 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/example/test.rbsh +4 -0
- data/lib/rbsh/cli.rb +3 -1
- data/lib/rbsh/pipeline.rb +20 -15
- data/lib/rbsh/shell.rb +26 -25
- data/lib/rbsh/version.rb +1 -1
- data/spec/rbsh/cli_spec.rb +5 -0
- data/spec/rbsh/pipeline_spec.rb +6 -6
- data/spec/rbsh/shell_spec.rb +11 -5
- metadata +4 -3
data/example/test.rbsh
ADDED
data/lib/rbsh/cli.rb
CHANGED
@@ -18,9 +18,11 @@ class Rbsh::CLI
|
|
18
18
|
if @opts.has_key? :v
|
19
19
|
puts Rbsh::VERSION
|
20
20
|
elsif @opts.has_key? :e
|
21
|
-
puts @shell.load_script!(@opts[:e])
|
21
|
+
puts @shell.load_script!(@opts[:e]).run!.to_s
|
22
22
|
else
|
23
23
|
script_file = ARGV[0]
|
24
|
+
script = File.open(script_file).read
|
25
|
+
puts @shell.load_script!(script).run!.to_s
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
data/lib/rbsh/pipeline.rb
CHANGED
@@ -7,36 +7,41 @@ class Rbsh::Pipeline < BasicObject
|
|
7
7
|
_push(name, *args)
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def run!
|
11
11
|
commands = _queue.map do |c|
|
12
12
|
[ c[:command], *c[:args] ].compact.map(&:to_s)
|
13
13
|
end
|
14
14
|
|
15
|
-
result =
|
15
|
+
result = Result.new
|
16
16
|
::Open3.pipeline_rw(*commands) do |stdin, stdout, wait_threads|
|
17
17
|
stdin.close
|
18
|
-
|
18
|
+
lines = stdout.readlines
|
19
|
+
lines = ["\n"] if lines.size == 0
|
20
|
+
result.set(lines)
|
19
21
|
end
|
20
|
-
result << "\n" if result == []
|
21
22
|
result
|
22
23
|
end
|
23
24
|
|
24
|
-
def to_s
|
25
|
-
result = to_ary
|
26
|
-
result.join("")
|
27
|
-
end
|
28
|
-
alias inspect to_s
|
29
|
-
alias to_str to_s
|
30
|
-
|
31
|
-
def equal?(str)
|
32
|
-
to_s == str
|
33
|
-
end
|
34
|
-
|
35
25
|
def method_missing(name, *args, &block)
|
36
26
|
_push(name, *args)
|
37
27
|
self
|
38
28
|
end
|
39
29
|
|
30
|
+
class Result
|
31
|
+
def set(lines)
|
32
|
+
@lines = lines
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_ary
|
36
|
+
@lines
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_s
|
40
|
+
@lines.join("")
|
41
|
+
end
|
42
|
+
alias inspect to_s
|
43
|
+
end
|
44
|
+
|
40
45
|
protected
|
41
46
|
|
42
47
|
def _push(name, *args, &block)
|
data/lib/rbsh/shell.rb
CHANGED
@@ -1,43 +1,44 @@
|
|
1
1
|
require "open3"
|
2
2
|
|
3
3
|
class Rbsh::Shell < BasicObject
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
def run!
|
8
|
-
@_result = [] unless @_result
|
9
|
-
_queue.each do |pipeline|
|
10
|
-
@_result += pipeline.to_ary
|
11
|
-
end
|
12
|
-
to_s
|
4
|
+
def initialize
|
5
|
+
@_result = Result.new
|
13
6
|
end
|
14
7
|
|
15
|
-
def
|
16
|
-
_result
|
17
|
-
|
18
|
-
|
19
|
-
def to_s
|
20
|
-
to_ary.join("")
|
8
|
+
def run!
|
9
|
+
@_result.append(@_pipeline.run!)
|
10
|
+
@_result
|
21
11
|
end
|
22
|
-
alias inspect to_s
|
23
12
|
|
24
13
|
def method_missing(name, *args, &block)
|
25
|
-
|
26
|
-
|
27
|
-
|
14
|
+
if @_pipeline
|
15
|
+
@_result.append(@_pipeline.run!)
|
16
|
+
end
|
17
|
+
@_pipeline = ::Rbsh::Pipeline.new(name, *args)
|
18
|
+
@_pipeline
|
28
19
|
end
|
29
20
|
|
30
21
|
def load_script!(script)
|
31
22
|
instance_eval(script)
|
23
|
+
self
|
32
24
|
end
|
33
25
|
|
34
|
-
|
26
|
+
class Result
|
27
|
+
def initialize
|
28
|
+
@lines = []
|
29
|
+
end
|
35
30
|
|
36
|
-
|
37
|
-
|
38
|
-
|
31
|
+
def append(lines)
|
32
|
+
@lines += lines.to_ary
|
33
|
+
end
|
39
34
|
|
40
|
-
|
41
|
-
|
35
|
+
def to_ary
|
36
|
+
@lines
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_s
|
40
|
+
@lines.join("")
|
41
|
+
end
|
42
|
+
alias inspect to_s
|
42
43
|
end
|
43
44
|
end
|
data/lib/rbsh/version.rb
CHANGED
data/spec/rbsh/cli_spec.rb
CHANGED
@@ -9,4 +9,9 @@ describe Rbsh::CLI do
|
|
9
9
|
subject { rbsh("-e" 'echo("test")') }
|
10
10
|
it { should eq "test\n" }
|
11
11
|
end
|
12
|
+
|
13
|
+
describe %q{rbsh -e 'echo("test1")\necho("test2")'} do
|
14
|
+
subject { rbsh("-e" "echo(\"test1\")\necho(\"test2\")") }
|
15
|
+
it { should eq "test1\ntest2\n" }
|
16
|
+
end
|
12
17
|
end
|
data/spec/rbsh/pipeline_spec.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
describe Rbsh::Pipeline do
|
2
2
|
describe "echo test" do
|
3
|
-
subject { Rbsh::Pipeline.new(:echo, "test").
|
4
|
-
|
3
|
+
subject { Rbsh::Pipeline.new(:echo, "test").run! }
|
4
|
+
its(:to_s) { should eq "test\n" }
|
5
5
|
end
|
6
6
|
|
7
7
|
describe "echo test | grep test" do
|
8
|
-
subject { Rbsh::Pipeline.new(:echo, "test").grep("test").
|
9
|
-
|
8
|
+
subject { Rbsh::Pipeline.new(:echo, "test").grep("test").run! }
|
9
|
+
its(:to_s) { should eq "test\n" }
|
10
10
|
end
|
11
11
|
|
12
12
|
describe "echo test | grep no" do
|
13
|
-
subject { Rbsh::Pipeline.new(:echo, "test").grep("no").
|
14
|
-
|
13
|
+
subject { Rbsh::Pipeline.new(:echo, "test").grep("no").run! }
|
14
|
+
its(:to_s) { should eq "\n" }
|
15
15
|
end
|
16
16
|
end
|
data/spec/rbsh/shell_spec.rb
CHANGED
@@ -6,30 +6,36 @@ describe Rbsh::Shell do
|
|
6
6
|
describe "echo test" do
|
7
7
|
before { @shell.echo("test") }
|
8
8
|
subject { @shell.run! }
|
9
|
-
|
9
|
+
its(:to_s) { should eq "test\n" }
|
10
10
|
end
|
11
11
|
|
12
12
|
describe "echo test | grep test" do
|
13
13
|
before { @shell.echo("test").grep("test") }
|
14
14
|
subject { @shell.run! }
|
15
|
-
|
15
|
+
its(:to_s) { should eq "test\n" }
|
16
16
|
end
|
17
17
|
|
18
18
|
describe "echo test | grep no" do
|
19
19
|
before { @shell.echo("test").grep("no") }
|
20
20
|
subject { @shell.run! }
|
21
|
-
|
21
|
+
its(:to_s) { should eq "\n" }
|
22
22
|
end
|
23
23
|
|
24
24
|
describe "load_script! 'echo(\"test\")'" do
|
25
25
|
before { @shell.load_script!('echo("test")') }
|
26
26
|
subject { @shell.run! }
|
27
|
-
|
27
|
+
its(:to_s) { should eq "test\n" }
|
28
28
|
end
|
29
29
|
|
30
30
|
describe "echo test1; echo test2" do
|
31
31
|
before { @shell.echo("test1"); @shell.echo("test2") }
|
32
32
|
subject { @shell.run! }
|
33
|
-
|
33
|
+
its(:to_s) { should eq "test1\ntest2\n" }
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "load_script! 'echo(\"test1\")\\necho(\"test2\")'" do
|
37
|
+
before { @shell.load_script!("echo(\"test1\")\necho(\"test2\")") }
|
38
|
+
subject { @shell.run! }
|
39
|
+
its(:to_s) { should eq "test1\ntest2\n" }
|
34
40
|
end
|
35
41
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rbsh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- README.md
|
75
75
|
- Rakefile
|
76
76
|
- bin/rbsh
|
77
|
+
- example/test.rbsh
|
77
78
|
- lib/rbsh.rb
|
78
79
|
- lib/rbsh/cli.rb
|
79
80
|
- lib/rbsh/pipeline.rb
|
@@ -99,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
100
|
version: '0'
|
100
101
|
segments:
|
101
102
|
- 0
|
102
|
-
hash:
|
103
|
+
hash: -2892255319277490126
|
103
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
105
|
none: false
|
105
106
|
requirements:
|
@@ -108,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
109
|
version: '0'
|
109
110
|
segments:
|
110
111
|
- 0
|
111
|
-
hash:
|
112
|
+
hash: -2892255319277490126
|
112
113
|
requirements: []
|
113
114
|
rubyforge_project:
|
114
115
|
rubygems_version: 1.8.23
|