blue_shell 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/.gitignore +17 -0
- data/.rbenv-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +6 -0
- data/blue_shell.gemspec +21 -0
- data/lib/blue_shell/bash.rb +68 -0
- data/lib/blue_shell/version.rb +3 -0
- data/lib/blue_shell.rb +7 -0
- data/spec/blue_shell_spec.rb +22 -0
- metadata +74 -0
data/.gitignore
ADDED
data/.rbenv-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
jruby-1.6.8
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Thomas Meeks
|
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,30 @@
|
|
1
|
+
# BlueShell
|
2
|
+
|
3
|
+
A very simple and straightforward way to shell out in JRuby. Created due to frequent gotchas
|
4
|
+
encountered with the other methods.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'blue_shell'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install blue_shell
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
TODO: Write usage instructions here
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/blue_shell.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'blue_shell/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "blue_shell"
|
8
|
+
gem.version = BlueShell::VERSION
|
9
|
+
gem.authors = ["Thomas Meeks"]
|
10
|
+
gem.email = ["thomas@thomasmeeks.com"]
|
11
|
+
gem.description = %q{Provides an easy way to shell out inside a java process.}
|
12
|
+
gem.summary = %q{Provides an easy way to shell out inside a java process.}
|
13
|
+
gem.homepage = "https://github.com/thomas/blue_shell"
|
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('minitest')
|
21
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'timeout'
|
3
|
+
|
4
|
+
# Executes shell commands inside a java runtime
|
5
|
+
module BlueShell
|
6
|
+
class Bash
|
7
|
+
BufferedReader = java.io.BufferedReader
|
8
|
+
InputStreamReader = java.io.InputStreamReader
|
9
|
+
|
10
|
+
# Execute the given command. Will block until the command completes.
|
11
|
+
# param [String] the command to execute in bash
|
12
|
+
# param [Integer] the number of seconds to wait for it to complete
|
13
|
+
def execute!(command, timeout = 5)
|
14
|
+
# Executing via a file seems clunky, but it is the best way to
|
15
|
+
# ensure everything happens exactly as we expect it to without
|
16
|
+
# trying to deal with escaping the world properly.
|
17
|
+
file = Tempfile.new('command')
|
18
|
+
begin
|
19
|
+
file << command
|
20
|
+
file.flush
|
21
|
+
|
22
|
+
Timeout::timeout(timeout) do
|
23
|
+
@proc = runtime.exec("/bin/bash #{file.path}")
|
24
|
+
@proc.waitFor
|
25
|
+
end
|
26
|
+
ensure
|
27
|
+
file.close
|
28
|
+
file.unlink
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Return the stdout of the process
|
33
|
+
# return [String] the full stdout output
|
34
|
+
def out
|
35
|
+
stream_to_string(@proc.getInputStream)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Return the stderr of the process
|
39
|
+
# return [String] the full stderr output
|
40
|
+
def err
|
41
|
+
stream_to_string(@proc.getErrorStream)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Return the exit value of the process
|
45
|
+
# return [Integer] the process' exit value
|
46
|
+
def exit
|
47
|
+
@proc.exitValue
|
48
|
+
end
|
49
|
+
|
50
|
+
def stream_to_string(stream)
|
51
|
+
br = BufferedReader.new(InputStreamReader.new(stream))
|
52
|
+
line = nil
|
53
|
+
lines = []
|
54
|
+
|
55
|
+
while(line = br.readLine) do
|
56
|
+
lines << line
|
57
|
+
end
|
58
|
+
|
59
|
+
lines.join("\n")
|
60
|
+
end
|
61
|
+
private :stream_to_string
|
62
|
+
|
63
|
+
def runtime
|
64
|
+
@runtime ||= java.lang.Runtime.getRuntime
|
65
|
+
end
|
66
|
+
private :runtime
|
67
|
+
end
|
68
|
+
end
|
data/lib/blue_shell.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'blue_shell'
|
3
|
+
|
4
|
+
describe BlueShell::Bash do
|
5
|
+
it "should return the stdout" do
|
6
|
+
shell = BlueShell::Bash.new
|
7
|
+
shell.execute!("echo hello")
|
8
|
+
shell.out.must_equal "hello"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return the stderr" do
|
12
|
+
shell = BlueShell::Bash.new
|
13
|
+
shell.execute!("omg")
|
14
|
+
shell.err.must_include "omg: command not found"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return exit status" do
|
18
|
+
shell = BlueShell::Bash.new
|
19
|
+
shell.execute!("echo hello")
|
20
|
+
shell.exit.must_equal 0
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blue_shell
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Thomas Meeks
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ! '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
none: false
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
none: false
|
28
|
+
prerelease: false
|
29
|
+
type: :development
|
30
|
+
description: Provides an easy way to shell out inside a java process.
|
31
|
+
email:
|
32
|
+
- thomas@thomasmeeks.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rbenv-version
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- blue_shell.gemspec
|
44
|
+
- lib/blue_shell.rb
|
45
|
+
- lib/blue_shell/bash.rb
|
46
|
+
- lib/blue_shell/version.rb
|
47
|
+
- spec/blue_shell_spec.rb
|
48
|
+
homepage: https://github.com/thomas/blue_shell
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
none: false
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
none: false
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.24
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Provides an easy way to shell out inside a java process.
|
72
|
+
test_files:
|
73
|
+
- spec/blue_shell_spec.rb
|
74
|
+
...
|