Class: RuntimeCommand::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/runtime_command/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_dir = '.') ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
14
# File 'lib/runtime_command/builder.rb', line 9

def initialize(base_dir = '.')
  @base_dir = base_dir
  @output = true
  @buffered_log = ''
  @stdin_prefix = '>'
end

Instance Attribute Details

#buffered_logObject (readonly)

Returns the value of attribute buffered_log.



6
7
8
# File 'lib/runtime_command/builder.rb', line 6

def buffered_log
  @buffered_log
end

#colorsObject

Returns the value of attribute colors.



7
8
9
# File 'lib/runtime_command/builder.rb', line 7

def colors
  @colors
end

#outputObject

Returns the value of attribute output.



7
8
9
# File 'lib/runtime_command/builder.rb', line 7

def output
  @output
end

#stdin_prefixObject

Returns the value of attribute stdin_prefix.



7
8
9
# File 'lib/runtime_command/builder.rb', line 7

def stdin_prefix
  @stdin_prefix
end

Instance Method Details

#exec(command, chdir = nil) ⇒ Object



16
17
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
# File 'lib/runtime_command/builder.rb', line 16

def exec(command, chdir = nil)
  chdir ||= @base_dir

  logger = Logger.new(@output, @colors)
  logger.stdin(@stdin_prefix + ' ' + command)

  begin
    Open3.popen3(command, chdir: chdir) do |stdin, stdout, stderr, wait_thr|
      stdin.close

      stdout.each do |line|
        logger.stdout(line)
      end

      stderr.each do |line|
        logger.stderr(line)
      end
    end

    @buffered_log << logger.buffered_log

  rescue => e
    logger.stderr(e.to_s)
    @buffered_log << logger.buffered_log
  end

  logger
end