Class: Command::Runner::Backends::Backticks

Inherits:
Fake
  • Object
show all
Defined in:
lib/command/runner/backends/backticks.rb

Overview

A backend that uses ticks to do its bidding.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Fake

#ran?, #unsafe?, unsafe_execution?

Constructor Details

#initializeBackticks

Initialize the fake backend.


21
22
23
# File 'lib/command/runner/backends/backticks.rb', line 21

def initialize
  super
end

Class Method Details

.available?(_ = false) ⇒ Boolean

Returns whether or not this backend is avialable on this platform.

Returns:

  • (Boolean)

12
13
14
# File 'lib/command/runner/backends/backticks.rb', line 12

def self.available?(_ = false)
  true
end

.unsafe?Boolean

Returns:

  • (Boolean)

16
17
18
# File 'lib/command/runner/backends/backticks.rb', line 16

def self.unsafe?
  true
end

Instance Method Details

#call(command, arguments, env = {}, options = {}) {|Message| ... } ⇒ Message, Object

Run the given command and arguments, in the given environment.

Parameters:

  • command (String)

    the command to run.

  • arguments (String)

    the arguments to pass to the command.

  • env (Hash) (defaults to: {})

    the enviornment to run the command under.

  • options (Hash) (defaults to: {})

    the options to run the command under.

Yields:

  • (Message)

    when the command finishes.

Returns:

  • (Message, Object)

    message if no block is given, the result of the block call otherwise.

Raises:

  • (Errno::ENOENT)

    if the command doesn't exist.


37
38
39
40
41
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
# File 'lib/command/runner/backends/backticks.rb', line 37

def call(command, arguments, env = {}, options = {}, &block)
  super
  output = ""
  start_time = nil
  end_time = nil

  if options[:input]
    raise ArgumentError, "Cannot specify options[:input] - using incorrect backend"
  end

  with_modified_env(env) do
    start_time = Time.now
    output << `#{command} #{arguments.join(' ')}`
    end_time = Time.now
  end

  if $?.exitstatus == 127
    raise NoCommandError
  end

  message = Message.new :process_id => $?.pid,
              :exit_code => $?.exitstatus,
              :finished => true,
              :time => (end_time - start_time).abs,
              :env => env,
              :options => {},
              :stdout => output,
              :line => [command, arguments].flatten.join(' '),
              :executed => true,
              :status => $?

  if block_given?
    block.call(message)
  else
    message
  end
end