Class: Buildbox::Command
- Inherits:
-
Object
- Object
- Buildbox::Command
- Defined in:
- lib/buildbox/command.rb
Defined Under Namespace
Classes: Result
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(command, options = {}) ⇒ Command
constructor
A new instance of Command.
- #run(&block) ⇒ Object
Constructor Details
#initialize(command, options = {}) ⇒ Command
Returns a new instance of Command.
12 13 14 15 16 |
# File 'lib/buildbox/command.rb', line 12 def initialize(command, = {}) @command = command @directory = [:directory] || "." @read_interval = [:read_interval] || 5 end |
Class Method Details
.run(command, options = {}, &block) ⇒ Object
8 9 10 |
# File 'lib/buildbox/command.rb', line 8 def self.run(command, = {}, &block) new(command, ).run(&block) end |
Instance Method Details
#run(&block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/buildbox/command.rb', line 18 def run(&block) output = "" read_io, write_io, pid = nil # spawn the process in a pseudo terminal so colors out outputted read_io, write_io, pid = PTY.spawn("cd #{expanded_directory} && #{@command}") # we don't need to write to the spawned io write_io.close loop do fds, = IO.select([read_io], nil, nil, read_interval) if fds # should have some data to read begin chunk = read_io.read_nonblock(10240) cleaned_chunk = UTF8.clean(chunk) output << chunk yield cleaned_chunk if block_given? rescue Errno::EAGAIN, Errno::EWOULDBLOCK # do select again rescue EOFError, Errno::EIO # EOFError from OSX, EIO is raised by ubuntu break end end # if fds are empty, timeout expired - run another iteration end # we're done reading, yay! read_io.close # just wait until its finally finished closing Process.waitpid(pid) # the final result! Result.new(output.chomp, $?.exitstatus) end |