coffee_machine 0.1.0
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 +3 -0
- data/README +0 -0
- data/Rakefile +15 -0
- data/VERSION +1 -0
- data/lib/coffee_machine.rb +107 -0
- data/test/coffee_machine_test.rb +128 -0
- metadata +61 -0
data/.gitignore
ADDED
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = 'coffee_machine'
|
8
|
+
gem.summary = 'Run Java classes and jars from Ruby.'
|
9
|
+
gem.homepage = 'http://github.com/codespeaks/coffee_machine'
|
10
|
+
gem.authors = ['Tobie Langel', 'Samuel Lebeau']
|
11
|
+
end
|
12
|
+
Jeweler::GemcutterTasks.new
|
13
|
+
rescue LoadError
|
14
|
+
puts 'Jeweler not available. Install it with: gem install jeweler'
|
15
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,107 @@
|
|
1
|
+
module CoffeeMachine
|
2
|
+
extend self
|
3
|
+
|
4
|
+
def run_class(java_class, *args, &block)
|
5
|
+
java_class = java_class.inspect if java_class =~ /\.class$/
|
6
|
+
JavaRunner.run(java_class, *args, &block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def run_jar(path_to_jar, *args, &block)
|
10
|
+
JavaRunner.run("-jar #{path_to_jar.inspect}", *args, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
class JavaRunner # :nodoc:
|
14
|
+
autoload :Tempfile, 'tempfile'
|
15
|
+
|
16
|
+
TEMPFILE_BASENAME = 'coffee_machine'.freeze
|
17
|
+
|
18
|
+
DEFAULT_OPTIONS = {
|
19
|
+
:java => 'java'.freeze,
|
20
|
+
:java_args => nil,
|
21
|
+
:class_path => nil
|
22
|
+
}.freeze
|
23
|
+
|
24
|
+
attr_reader :class_or_jar, :options, :stderr
|
25
|
+
|
26
|
+
def self.run(*args, &block)
|
27
|
+
new(*args).run(&block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(class_or_jar, program_args = nil, options = {})
|
31
|
+
@class_or_jar = class_or_jar
|
32
|
+
options, program_args = program_args, nil if options?(program_args)
|
33
|
+
@program_args, @options = program_args, DEFAULT_OPTIONS.merge(options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def run
|
37
|
+
create_stderr do |stderr|
|
38
|
+
@stderr = stderr
|
39
|
+
IO.popen(command, IO::RDWR) do |pipe|
|
40
|
+
if block_given?
|
41
|
+
return yield(pipe, stderr)
|
42
|
+
else
|
43
|
+
return pipe.read, stderr.read
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
protected
|
50
|
+
def options?(arg)
|
51
|
+
arg.is_a?(Hash) && arg.keys.any? { |k| k.is_a?(Symbol) }
|
52
|
+
end
|
53
|
+
|
54
|
+
def create_stderr(&block)
|
55
|
+
tempfile = Tempfile.open(TEMPFILE_BASENAME)
|
56
|
+
tempfile.close
|
57
|
+
File.open(tempfile.path, &block)
|
58
|
+
end
|
59
|
+
|
60
|
+
def command
|
61
|
+
cmd = []
|
62
|
+
cmd << options[:java]
|
63
|
+
cmd << java_args
|
64
|
+
cmd << classpath
|
65
|
+
cmd << class_or_jar
|
66
|
+
cmd << program_args
|
67
|
+
cmd << redirect_stderr
|
68
|
+
cmd.compact.join(' ')
|
69
|
+
end
|
70
|
+
|
71
|
+
def java_args
|
72
|
+
format_args(options[:java_args])
|
73
|
+
end
|
74
|
+
|
75
|
+
def classpath
|
76
|
+
if (classpath = options[:classpath]) && !classpath.empty?
|
77
|
+
if classpath.is_a?(Enumerable) && !classpath.is_a?(String)
|
78
|
+
classpath = classpath.collect { |dir| dir.inspect }.join(':')
|
79
|
+
end
|
80
|
+
"-classpath #{classpath}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def program_args
|
85
|
+
format_args(@program_args)
|
86
|
+
end
|
87
|
+
|
88
|
+
def format_args(args)
|
89
|
+
case args
|
90
|
+
when Hash
|
91
|
+
args.inject([]) do |array, (key, value)|
|
92
|
+
array << key
|
93
|
+
array << value unless value == true
|
94
|
+
array
|
95
|
+
end.join(' ')
|
96
|
+
when String
|
97
|
+
args
|
98
|
+
when Enumerable
|
99
|
+
args.to_a.join(' ')
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def redirect_stderr
|
104
|
+
"2> #{stderr.path.inspect}"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../lib/coffee_machine'
|
2
|
+
|
3
|
+
begin
|
4
|
+
gem 'test-unit'
|
5
|
+
rescue Gem::LoadError
|
6
|
+
puts "Run `gem install test-unit` if encountering the following exception:"
|
7
|
+
puts "uninitialized constant Test::Unit::TestResult::TestResultFailureSupport (NameError)".inspect
|
8
|
+
end if RUBY_PLATFORM =~ /(win|w)32/
|
9
|
+
|
10
|
+
require 'test/unit'
|
11
|
+
require 'mocha'
|
12
|
+
require 'stringio'
|
13
|
+
require 'set'
|
14
|
+
|
15
|
+
class CoffeeMachineTest < Test::Unit::TestCase
|
16
|
+
def test_run_class
|
17
|
+
CoffeeMachine::JavaRunner.expects(:run).with('Foo')
|
18
|
+
CoffeeMachine.run_class('Foo')
|
19
|
+
|
20
|
+
CoffeeMachine::JavaRunner.expects(:run).with('"path/to/Foo.class"')
|
21
|
+
CoffeeMachine.run_class('path/to/Foo.class')
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_run_jar
|
25
|
+
CoffeeMachine::JavaRunner.expects(:run).with('-jar "path/to/foo.jar"')
|
26
|
+
CoffeeMachine.run_jar('path/to/foo.jar')
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_run_methods_forward_args
|
30
|
+
CoffeeMachine::JavaRunner.expects(:run).with('Foo', '--foo -bar')
|
31
|
+
CoffeeMachine.run_class('Foo', '--foo -bar')
|
32
|
+
CoffeeMachine::JavaRunner.expects(:run).with('Foo', '--foo -bar', :foo => :bar)
|
33
|
+
CoffeeMachine.run_class('Foo', '--foo -bar', :foo => :bar)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_program_args_as_string
|
37
|
+
should_run_command %{java Foo -bar --baz}
|
38
|
+
CoffeeMachine::JavaRunner.run('Foo', '-bar --baz')
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_program_args_as_hash
|
42
|
+
should_run_command do |command|
|
43
|
+
if command =~ /^java Foo (.*) 2>/
|
44
|
+
$1.scan(/--?\w+(?: \w+)?/).to_set == Set.new(['-bar', '--baz', '--foo 42'])
|
45
|
+
end
|
46
|
+
end
|
47
|
+
CoffeeMachine::JavaRunner.run('Foo', {
|
48
|
+
'-bar' => true,
|
49
|
+
'--baz' => true,
|
50
|
+
'--foo' => 42
|
51
|
+
})
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_program_args_as_array
|
55
|
+
should_run_command %{java Foo -bar --baz --foo 42}
|
56
|
+
CoffeeMachine::JavaRunner.run('Foo', ['-bar', '--baz', '--foo 42'])
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_java_option
|
60
|
+
should_run_command %{/path/to/java Foo}
|
61
|
+
CoffeeMachine::JavaRunner.run('Foo', :java => '/path/to/java')
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_java_args_options_as_string
|
65
|
+
should_run_command %{java -bar --baz Foo}
|
66
|
+
CoffeeMachine::JavaRunner.run('Foo', :java_args => '-bar --baz')
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_java_args_options_as_hash
|
70
|
+
should_run_command do |command|
|
71
|
+
if command =~ /^java (.*) Foo/
|
72
|
+
$1.scan(/--?\w+(?: \w+)?/).to_set == Set.new(['-bar', '--baz', '--foo 42'])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
CoffeeMachine::JavaRunner.run('Foo', :java_args => {
|
76
|
+
'-bar' => true,
|
77
|
+
'--baz' => true,
|
78
|
+
'--foo' => 42
|
79
|
+
})
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_classpath_option
|
83
|
+
should_run_command %{java -classpath /path/to/foo:bar Foo}
|
84
|
+
CoffeeMachine::JavaRunner.run('Foo', :classpath => '/path/to/foo:bar')
|
85
|
+
|
86
|
+
should_run_command %{java -classpath "/path/to/foo":"bar"}
|
87
|
+
CoffeeMachine::JavaRunner.run('Foo', :classpath => ['/path/to/foo', 'bar'])
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_block_given
|
91
|
+
should_run_command(/^java Foo 2> "(.*)"$/).yields(:a_stream)
|
92
|
+
return_value = CoffeeMachine::JavaRunner.run('Foo') do |pipe, stderr|
|
93
|
+
assert_equal(:a_stream, pipe)
|
94
|
+
assert_respond_to stderr, :read
|
95
|
+
assert_equal stderr.path, @match.captures.first
|
96
|
+
:value_returned_by_block
|
97
|
+
end
|
98
|
+
assert_equal :value_returned_by_block, return_value
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_stdout_return_value
|
102
|
+
should_run_command(%{java Foo}).yields(StringIO.new('STDOUT content'))
|
103
|
+
stdout_content, stderr_content = CoffeeMachine::JavaRunner.run('Foo')
|
104
|
+
assert_equal 'STDOUT content', stdout_content
|
105
|
+
assert_equal '', stderr_content
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_stderr_return_value
|
109
|
+
stdout_content, stderr_content = CoffeeMachine::JavaRunner.run('',
|
110
|
+
:java => 'ruby',
|
111
|
+
:java_args => %{-e "STDERR.print('STDERR content')"}
|
112
|
+
)
|
113
|
+
assert_equal '', stdout_content
|
114
|
+
assert_equal 'STDERR content', stderr_content
|
115
|
+
end
|
116
|
+
|
117
|
+
protected
|
118
|
+
def should_run_command(pattern = nil)
|
119
|
+
pattern = Regexp.compile("^#{pattern}") unless pattern.nil? || pattern.is_a?(Regexp)
|
120
|
+
IO.expects(:popen).with do |command, mode|
|
121
|
+
if block_given?
|
122
|
+
yield(command)
|
123
|
+
else
|
124
|
+
@match = pattern.match(command)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coffee_machine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tobie Langel
|
8
|
+
- Samuel Lebeau
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2010-03-24 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description:
|
18
|
+
email:
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- README
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- README
|
28
|
+
- Rakefile
|
29
|
+
- VERSION
|
30
|
+
- lib/coffee_machine.rb
|
31
|
+
- test/coffee_machine_test.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/codespeaks/coffee_machine
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --charset=UTF-8
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.3.5
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Run Java classes and jars from Ruby.
|
60
|
+
test_files:
|
61
|
+
- test/coffee_machine_test.rb
|