-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation and better separation of classes
- Loading branch information
1 parent
e9d13d5
commit 61eff0e
Showing
7 changed files
with
155 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Methadone | ||
module ExecutionStrategy | ||
# ExecutionStrategy for the JVM that uses JVM classes to run the command and get its results. | ||
class JVM | ||
def run_command(command) | ||
process = java.lang.Runtime.get_runtime.exec(command) | ||
process.get_output_stream.close | ||
stdout = input_stream_to_string(process.get_input_stream) | ||
stderr = input_stream_to_string(process.get_error_stream) | ||
exitstatus = process.wait_for | ||
[stdout.chomp,stderr.chomp,OpenStruct.new(:exitstatus => exitstatus)] | ||
end | ||
|
||
def exception_meaning_command_not_found | ||
NativeException | ||
end | ||
|
||
private | ||
def input_stream_to_string(is) | ||
''.tap do |string| | ||
ch = is.read | ||
while ch != -1 | ||
string << ch | ||
ch = is.read | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Methadone | ||
# Module to contain ExecutionStrategy implementations. | ||
# To build your own simply implement two methods: | ||
# | ||
# * <tt>run_command(command)</tt> - takes the command to run, and runs it, returning an array of size 3. Index 0 | ||
# should be the standard output as a String (never nil), Index 1 should be the | ||
# standard error output as a String (never nil) and Index 2 should be a | ||
# Process::Status representing the results of running the command. Since it's | ||
# not straightforward to create an instance of this class, the returned object | ||
# in this slot need only respond to <tt>exitstatus</tt>, which returns the exit code. | ||
# * <tt>exception_meaning_command_not_found</tt> - return the class that, if caught, means that the underlying command | ||
# couldn't be found. This is needed because currently impelmentations | ||
# throw an exception, but they don't all throw the same one. | ||
module ExecutionStrategy | ||
# Base strategy for MRI rubies. | ||
class MRI | ||
def run_command(command) | ||
raise "subclass must implement" | ||
end | ||
|
||
def exception_meaning_command_not_found | ||
Errno::ENOENT | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Methadone | ||
module ExecutionStrategy | ||
# Implementation for modern Rubies that uses the built-in Open3 library | ||
class Open_3 < MRI | ||
def run_command(command) | ||
stdout,stderr,status = Open3.capture3(command) | ||
[stdout.chomp,stderr.chomp,status] | ||
end | ||
|
||
def exception_meaning_command_not_found | ||
Errno::ENOENT | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Methadone | ||
module ExecutionStrategy | ||
# ExecutionStrategy for non-modern Rubies that must rely on | ||
# Open4 to get access to the standard output AND error. | ||
class Open_4 < MRI | ||
def run_command(command) | ||
pid, stdin_io, stdout_io, stderr_io = Open4::popen4(command) | ||
stdin_io.close | ||
stdout = stdout_io.read | ||
stderr = stderr_io.read | ||
_ , status = Process::waitpid2(pid) | ||
[stdout.chomp,stderr.chomp,status] | ||
end | ||
|
||
def exception_meaning_command_not_found | ||
Errno::ENOENT | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters