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 = '.') ⇒ RuntimeCommand::Builder

Parameters:

  • base_dir (String) (defaults to: '.')


11
12
13
14
15
16
17
# File 'lib/runtime_command/builder.rb', line 11

def initialize(base_dir = '.')
  @base_dir = base_dir
  @output = true
  @buffered_log = ''
  @stdin_prefix = '>'
  @colors = {}
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) ⇒ RuntimeCommand::Logger

Parameters:

  • command (String)
  • chdir (String) (defaults to: nil)

Returns:



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 22

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

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

  invoke_command(logger) do
    Open3.popen3(command, chdir: chdir) do |stdin, stdout, stderr|
      stdin.close

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

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

  logger
end

#exec_capture(command, chdir = nil) ⇒ RuntimeCommand::Logger

Parameters:

  • command (String)
  • chdir (String) (defaults to: nil)

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/runtime_command/builder.rb', line 48

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

  logger = Logger.new(@output, @colors)
  invoke_command(logger) do
    stdout, stderr = Open3.capture3(command, chdir: chdir)

    unless stdout.empty?
      logger.stdout(stdout)
    end

    unless stderr.empty?
      logger.stderr(stderr)
    end
  end

  logger
end

#puts(message) ⇒ RuntimeCommand::Logger

Parameters:

  • message (String)

Returns:



69
70
71
72
73
74
75
# File 'lib/runtime_command/builder.rb', line 69

def puts(message)
  logger = Logger.new(@output, @colors)
  logger.stdout(message) unless message.nil?

  @buffered_log << logger.buffered_log + "\n"
  logger
end

#puts_error(message) ⇒ RuntimeCommand::Logger

Parameters:

  • message (String)

Returns:



79
80
81
82
83
84
85
# File 'lib/runtime_command/builder.rb', line 79

def puts_error(message)
  logger = Logger.new(@output, @colors)
  logger.stderr(message) unless message.nil?

  @buffered_log << logger.buffered_log + "\n"
  logger
end