rbsh 0.0.1
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.
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +10 -0
- data/lib/rbsh.rb +7 -0
- data/lib/rbsh/cli.rb +38 -0
- data/lib/rbsh/pipeline.rb +49 -0
- data/lib/rbsh/shell.rb +43 -0
- data/lib/rbsh/version.rb +3 -0
- data/rbsh.gemspec +23 -0
- data/spec/rbsh/cli_spec.rb +12 -0
- data/spec/rbsh/pipeline_spec.rb +16 -0
- data/spec/rbsh/shell_spec.rb +35 -0
- data/spec/rbsh_spec.rb +6 -0
- data/spec/spec_helper.rb +24 -0
- metadata +123 -0
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ryosuke IWANAGA
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Rbsh
|
2
|
+
|
3
|
+
[](https://travis-ci.org/riywo/rbsh)
|
4
|
+
|
5
|
+
Yet Another shell script
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
echo("test")
|
10
|
+
#=> "test\n"
|
11
|
+
|
12
|
+
echo("test").grep("test")
|
13
|
+
#=> "test\n"
|
14
|
+
|
15
|
+
echo("test").grep("no")
|
16
|
+
#=> "\n"
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
gem 'rbsh'
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install rbsh
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
TODO: Write usage instructions here
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/rbsh.rb
ADDED
data/lib/rbsh/cli.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "rbsh"
|
2
|
+
require "optparse"
|
3
|
+
|
4
|
+
class Rbsh::CLI
|
5
|
+
def initialize(args=[], config={})
|
6
|
+
@config = config
|
7
|
+
@args, @opts = parse(args)
|
8
|
+
|
9
|
+
@shell = Rbsh::Shell.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.start(given_args=ARGV, config={})
|
13
|
+
instance = new(given_args, config)
|
14
|
+
instance.run
|
15
|
+
end
|
16
|
+
|
17
|
+
def run
|
18
|
+
if @opts.has_key? :v
|
19
|
+
puts Rbsh::VERSION
|
20
|
+
elsif @opts.has_key? :e
|
21
|
+
puts @shell.load_script!(@opts[:e])
|
22
|
+
else
|
23
|
+
script_file = ARGV[0]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def parse(args)
|
30
|
+
opts = {}
|
31
|
+
parser = OptionParser.new
|
32
|
+
parser.on("-e 'command'") { |v| opts[:e] = v }
|
33
|
+
parser.on("-v", "--version") { |v| opts[:v] = v }
|
34
|
+
|
35
|
+
parser.parse!(args)
|
36
|
+
return args, opts
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require "open3"
|
2
|
+
|
3
|
+
class Rbsh::Pipeline < BasicObject
|
4
|
+
attr_writer :_queue
|
5
|
+
|
6
|
+
def initialize(name, *args, &block)
|
7
|
+
_push(name, *args)
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_ary
|
11
|
+
commands = _queue.map do |c|
|
12
|
+
[ c[:command], *c[:args] ].compact.map(&:to_s)
|
13
|
+
end
|
14
|
+
|
15
|
+
result = []
|
16
|
+
::Open3.pipeline_rw(*commands) do |stdin, stdout, wait_threads|
|
17
|
+
stdin.close
|
18
|
+
result = stdout.readlines
|
19
|
+
end
|
20
|
+
result << "\n" if result == []
|
21
|
+
result
|
22
|
+
end
|
23
|
+
|
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
|
+
def method_missing(name, *args, &block)
|
36
|
+
_push(name, *args)
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def _push(name, *args, &block)
|
43
|
+
_queue.push(command: name, args: args)
|
44
|
+
end
|
45
|
+
|
46
|
+
def _queue
|
47
|
+
@_queue ||= []
|
48
|
+
end
|
49
|
+
end
|
data/lib/rbsh/shell.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "open3"
|
2
|
+
|
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
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_ary
|
16
|
+
_result
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
to_ary.join("")
|
21
|
+
end
|
22
|
+
alias inspect to_s
|
23
|
+
|
24
|
+
def method_missing(name, *args, &block)
|
25
|
+
pipeline = ::Rbsh::Pipeline.new(name, *args)
|
26
|
+
_queue.push(pipeline)
|
27
|
+
pipeline
|
28
|
+
end
|
29
|
+
|
30
|
+
def load_script!(script)
|
31
|
+
instance_eval(script)
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
|
36
|
+
def _queue
|
37
|
+
@_queue ||= []
|
38
|
+
end
|
39
|
+
|
40
|
+
def _result
|
41
|
+
@_result ||= []
|
42
|
+
end
|
43
|
+
end
|
data/lib/rbsh/version.rb
ADDED
data/rbsh.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rbsh/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "rbsh"
|
8
|
+
gem.version = Rbsh::VERSION
|
9
|
+
gem.authors = ["Ryosuke IWANAGA"]
|
10
|
+
gem.email = ["riywo.jp@gmail.com"]
|
11
|
+
gem.description = %q{Ruby-sh shell script}
|
12
|
+
gem.summary = %q{Yet Another shell script like ruby}
|
13
|
+
gem.homepage = "https://github.com/riywo/rbsh"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rake', '>= 0.9.2.2'
|
21
|
+
gem.add_development_dependency 'rspec'
|
22
|
+
gem.add_development_dependency 'tapp'
|
23
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
describe Rbsh::CLI do
|
2
|
+
describe "version" do
|
3
|
+
it "displays gem version on shortcut command" do
|
4
|
+
rbsh("-v").chomp.should == Rbsh::VERSION
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
describe %q{rbsh -e 'echo("test")'} do
|
9
|
+
subject { rbsh("-e" 'echo("test")') }
|
10
|
+
it { should eq "test\n" }
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
describe Rbsh::Pipeline do
|
2
|
+
describe "echo test" do
|
3
|
+
subject { Rbsh::Pipeline.new(:echo, "test").to_s }
|
4
|
+
it { should eq "test\n" }
|
5
|
+
end
|
6
|
+
|
7
|
+
describe "echo test | grep test" do
|
8
|
+
subject { Rbsh::Pipeline.new(:echo, "test").grep("test").to_s }
|
9
|
+
it { should eq "test\n" }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "echo test | grep no" do
|
13
|
+
subject { Rbsh::Pipeline.new(:echo, "test").grep("no").to_s }
|
14
|
+
it { should eq "\n" }
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
describe Rbsh::Shell do
|
2
|
+
before :each do
|
3
|
+
@shell = Rbsh::Shell.new
|
4
|
+
end
|
5
|
+
|
6
|
+
describe "echo test" do
|
7
|
+
before { @shell.echo("test") }
|
8
|
+
subject { @shell.run! }
|
9
|
+
it { should eq "test\n" }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "echo test | grep test" do
|
13
|
+
before { @shell.echo("test").grep("test") }
|
14
|
+
subject { @shell.run! }
|
15
|
+
it { should eq "test\n" }
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "echo test | grep no" do
|
19
|
+
before { @shell.echo("test").grep("no") }
|
20
|
+
subject { @shell.run! }
|
21
|
+
it { should eq "\n" }
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "load_script! 'echo(\"test\")'" do
|
25
|
+
before { @shell.load_script!('echo("test")') }
|
26
|
+
subject { @shell.run! }
|
27
|
+
it { should eq "test\n" }
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "echo test1; echo test2" do
|
31
|
+
before { @shell.echo("test1"); @shell.echo("test2") }
|
32
|
+
subject { @shell.run! }
|
33
|
+
it { should eq "test1\ntest2\n" }
|
34
|
+
end
|
35
|
+
end
|
data/spec/rbsh_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rbsh'
|
3
|
+
require 'rbsh/cli'
|
4
|
+
require 'tapp'
|
5
|
+
|
6
|
+
def capture_stdout
|
7
|
+
old_stdout = $stdout.dup
|
8
|
+
rd, wr = IO.method(:pipe).arity.zero? ? IO.pipe : IO.pipe("BINARY")
|
9
|
+
$stdout = wr
|
10
|
+
yield
|
11
|
+
wr.close
|
12
|
+
rd.read
|
13
|
+
ensure
|
14
|
+
$stdout = old_stdout
|
15
|
+
end
|
16
|
+
|
17
|
+
def rbsh(*args)
|
18
|
+
capture_stdout do
|
19
|
+
begin
|
20
|
+
Rbsh::CLI.start(args)
|
21
|
+
rescue SystemExit
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rbsh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryosuke IWANAGA
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.9.2.2
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.9.2.2
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: tapp
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Ruby-sh shell script
|
63
|
+
email:
|
64
|
+
- riywo.jp@gmail.com
|
65
|
+
executables:
|
66
|
+
- rbsh
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- .travis.yml
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- bin/rbsh
|
77
|
+
- lib/rbsh.rb
|
78
|
+
- lib/rbsh/cli.rb
|
79
|
+
- lib/rbsh/pipeline.rb
|
80
|
+
- lib/rbsh/shell.rb
|
81
|
+
- lib/rbsh/version.rb
|
82
|
+
- rbsh.gemspec
|
83
|
+
- spec/rbsh/cli_spec.rb
|
84
|
+
- spec/rbsh/pipeline_spec.rb
|
85
|
+
- spec/rbsh/shell_spec.rb
|
86
|
+
- spec/rbsh_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: https://github.com/riywo/rbsh
|
89
|
+
licenses: []
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
hash: 1108712678994075046
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
hash: 1108712678994075046
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 1.8.23
|
115
|
+
signing_key:
|
116
|
+
specification_version: 3
|
117
|
+
summary: Yet Another shell script like ruby
|
118
|
+
test_files:
|
119
|
+
- spec/rbsh/cli_spec.rb
|
120
|
+
- spec/rbsh/pipeline_spec.rb
|
121
|
+
- spec/rbsh/shell_spec.rb
|
122
|
+
- spec/rbsh_spec.rb
|
123
|
+
- spec/spec_helper.rb
|