rbsh 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env rbsh
2
+ echo("test1")
3
+ cat("/etc/hosts").grep("localhost")
4
+ echo("test2")
@@ -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
 
@@ -7,36 +7,41 @@ class Rbsh::Pipeline < BasicObject
7
7
  _push(name, *args)
8
8
  end
9
9
 
10
- def to_ary
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
- result = stdout.readlines
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)
@@ -1,43 +1,44 @@
1
1
  require "open3"
2
2
 
3
3
  class Rbsh::Shell < BasicObject
4
- attr_writer :_queue
5
- attr_accessor :_result
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 to_ary
16
- _result
17
- end
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
- pipeline = ::Rbsh::Pipeline.new(name, *args)
26
- _queue.push(pipeline)
27
- pipeline
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
- protected
26
+ class Result
27
+ def initialize
28
+ @lines = []
29
+ end
35
30
 
36
- def _queue
37
- @_queue ||= []
38
- end
31
+ def append(lines)
32
+ @lines += lines.to_ary
33
+ end
39
34
 
40
- def _result
41
- @_result ||= []
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
@@ -1,3 +1,3 @@
1
1
  module Rbsh
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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
@@ -1,16 +1,16 @@
1
1
  describe Rbsh::Pipeline do
2
2
  describe "echo test" do
3
- subject { Rbsh::Pipeline.new(:echo, "test").to_s }
4
- it { should eq "test\n" }
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").to_s }
9
- it { should eq "test\n" }
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").to_s }
14
- it { should eq "\n" }
13
+ subject { Rbsh::Pipeline.new(:echo, "test").grep("no").run! }
14
+ its(:to_s) { should eq "\n" }
15
15
  end
16
16
  end
@@ -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
- it { should eq "test\n" }
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
- it { should eq "test\n" }
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
- it { should eq "\n" }
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
- it { should eq "test\n" }
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
- it { should eq "test1\ntest2\n" }
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.1
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: 1108712678994075046
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: 1108712678994075046
112
+ hash: -2892255319277490126
112
113
  requirements: []
113
114
  rubyforge_project:
114
115
  rubygems_version: 1.8.23