Module: AwesomeSpawn
- Extended by:
- AwesomeSpawn
- Included in:
- AwesomeSpawn
- Defined in:
- lib/awesome_spawn.rb,
lib/awesome_spawn/version.rb,
lib/awesome_spawn/null_logger.rb,
lib/awesome_spawn/spec_helper.rb,
lib/awesome_spawn/command_result.rb,
lib/awesome_spawn/no_such_file_error.rb,
lib/awesome_spawn/command_line_builder.rb,
lib/awesome_spawn/command_result_error.rb
Defined Under Namespace
Modules: SpecHelper Classes: CommandLineBuilder, CommandResult, CommandResultError, NoSuchFileError, NullLogger
Constant Summary collapse
- VERSION =
"1.6.0"
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#build_command_line(command, params = nil) ⇒ String
Build the full command line.
-
#run(command, options = {}) ⇒ CommandResult
Execute
command
synchronously via Kernel.spawn and gather the output stream, error stream, pid, and exit status in a CommandResult. -
#run!(command, options = {}) ⇒ CommandResult
Same as #run, additionally raising a CommandResultError if the exit status is not 0.
-
#run_detached(command, options = {}) ⇒ Object
Execute
command
in a detached manner The defalt is to spawn a new child process that sends the output to the null device if a user defines :out or :err, it is assumed they will define both.
Instance Attribute Details
#logger ⇒ Object
15 16 17 |
# File 'lib/awesome_spawn.rb', line 15 def logger @logger ||= NullLogger.new end |
Instance Method Details
#build_command_line(command, params = nil) ⇒ String
Build the full command line.
147 148 149 |
# File 'lib/awesome_spawn.rb', line 147 def build_command_line(command, params = nil) CommandLineBuilder.new.build(command, params) end |
#run(command, options = {}) ⇒ CommandResult
Execute command
synchronously via Kernel.spawn and gather the output
stream, error stream, pid, and exit status in a CommandResult.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/awesome_spawn.rb', line 71 def run(command, = {}) bad_keys = (.keys.flatten & [:in, :out, :err]).map { |k| ":#{k}" } raise ArgumentError, "options cannot contain #{bad_keys.join(", ")}" if bad_keys.any? env, command_line, = (command, ) if (in_data = .delete(:in_data)) [:stdin_data] = in_data end output, error, process_status = launch(env, command_line, ) status = process_status && process_status.exitstatus pid = process_status.pid if process_status rescue Errno::ENOENT => err raise NoSuchFileError.new(err.) if NoSuchFileError.detected?(err.) raise else CommandResult.new(command_line, output, error, pid, status) end |
#run!(command, options = {}) ⇒ CommandResult
Same as #run, additionally raising a CommandResultError if the exit status is not 0.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/awesome_spawn.rb', line 102 def run!(command, = {}) command_result = run(command, ) if command_result.failure? = CommandResultError.(command, command_result.exit_status) error = command_result.error.nil? || command_result.error.empty? ? command_result.output : command_result.error logger.error("AwesomeSpawn: #{}") logger.error("AwesomeSpawn: #{error}") raise CommandResultError.new(, command_result) end command_result end |
#run_detached(command, options = {}) ⇒ Object
Execute command
in a detached manner
The defalt is to spawn a new child process that sends the output to the null device
if a user defines :out or :err, it is assumed they will define both
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/awesome_spawn.rb', line 131 def run_detached(command, = {}) env, command_line, = (command, ) # maybe add support later for this raise ArgumentError, "options cannot contain :in_data" if .include?(:in_data) [[:out, :err]] = [IO::NULL, "w"] unless (.keys.flatten & [:out, :err]).any? if Gem.win_platform? [:new_pgroup] = true unless .key?(:new_pgroup) else [:pgroup] = true unless .key?(:pgroup) end detach(env, command_line, ) end |