Class: Bakery::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/bakery/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Command

Returns a new instance of Command.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
# File 'lib/bakery/command.rb', line 7

def initialize(name)
  raise ArgumentError, 'name must be specified' if '' == name.to_s
  @name = name.to_s
  @result = {
    :out => nil,
    :err => nil }
  check_for_dependency!
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/bakery/command.rb', line 5

def name
  @name
end

#resultObject (readonly)

Returns the value of attribute result.



5
6
7
# File 'lib/bakery/command.rb', line 5

def result
  @result
end

Instance Method Details

#check_for_dependency!Object



16
17
18
19
20
# File 'lib/bakery/command.rb', line 16

def check_for_dependency!
  return true unless '' == `which #{name}`.strip
  raise MissingSystemDependencyError, "The command line tool #{name} " +
    'does not appear to exist on this system, have you installed teTeX?'
end

#exec(*options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bakery/command.rb', line 26

def exec(*options)
  command = to_command(options)
  Open3.popen3(command) do |_, stdout, stderr|
    result[:out] = stdout.readlines.join("\n")
    result[:err] = stderr.readlines.join("\n")
  end
  return result[:out] if '' == result[:err].strip
  raise ShellCommandCallError, "The command: #{command} failed to " +
    "execute properly. The output was:\n\n#{result[:out]}\n\nThe error " +
    "output was:\n\n#{result[:err]}"
end

#to_command(*options) ⇒ Object



22
23
24
# File 'lib/bakery/command.rb', line 22

def to_command(*options)
  [name, transform_options(options)].join(' ')
end