Class: Rake::Local

Inherits:
Object
  • Object
show all
Defined in:
lib/rake/pro/localsh.rb,
lib/rake/pro/ssh_tunnel.rb

Instance Method Summary collapse

Instance Method Details

#capture_output(stdout, stderr, echo_command_output, native_output_only) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rake/pro/localsh.rb', line 42

def capture_output stdout, stderr, echo_command_output, native_output_only
  stdout_lines = ""
  stderr_lines = ""
  command_output = ""
  loop do
    begin
      # check whether stdout, stderr or both are
      #  ready to be read from without blocking
      IO.select([stdout,stderr]).flatten.compact.each { |io|
        # stdout, if ready, goes to stdout_lines
        stdout_lines += io.readpartial(1024) if io.fileno == stdout.fileno
        # stderr, if ready, goes to stdout_lines
        stderr_lines += io.readpartial(1024) if io.fileno == stderr.fileno
      }
      break if stdout.closed? && stderr.closed?
    rescue EOFError
      # Note, readpartial triggers the EOFError too soon.  Continue to flush the
      # pending io (via readpartial) until we have received all characters
      # out from the IO socket.
      break if stdout_lines.length == 0  &&  stderr_lines.length == 0
    ensure
      # if we acumulated any complete lines (\n-terminated)
      #  in either stdout/err_lines, output them now
      stdout_lines.sub!(/.*\n/) {
        command_output << $&
        if echo_command_output
          if native_output_only
            puts $&.strip
          else
            puts $&.strip
          end
        end
      }
      stderr_lines.sub!(/.*\n/) {
        command_output << $&
        if echo_command_output
          if native_output_only
            puts $&.strip
          else
            puts $&.strip
          end
        end
      }
    end
  end
  command_output
end

#sh(command_line, opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rake/pro/localsh.rb', line 18

def sh command_line, opts = {}
  native_output_only = command_line.include?('--terse') || opts[:terse]
  if native_output_only
    command_line.sub!(' --terse', '')
    opts[:echo] = true
    opts[:echo_cmd] = false
  end
  echo_command_output = opts[:echo] || true
  command_line = "#{command_line}"
  puts "$ #{command_line}"
  command_output = ""
  status = Open4::popen4(command_line) do |pid, stdin, stdout, stderr|
    command_output = capture_output(stdout, stderr, echo_command_output, native_output_only)
  end
  if status.exitstatus != 0
    raise AbnormalExitStatus.new(status.exitstatus, command_output) if opts[:raise_on_error]
  end
  command_output.strip
rescue AbnormalExitStatus
  raise
rescue Errno::ENOENT
  raise CommandNotFound, "Bash Error.  Command or file arguments not found." if opts[:raise_on_error]
end