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
44
45
46
47
48
49
50
# 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)

  begin
    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

  rescue Interrupt
    logger.stderr('Interrupt error')
  rescue => e
    logger.stderr(e.to_s)
  ensure
    @buffered_log << logger.buffered_log
  end

  logger
end

#puts(message) ⇒ RuntimeCommand::Logger

Parameters:

  • message (String)

Returns:



54
55
56
57
58
59
60
# File 'lib/runtime_command/builder.rb', line 54

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:



64
65
66
67
68
69
70
# File 'lib/runtime_command/builder.rb', line 64

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

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