Class: R10K::Util::Subprocess Private

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/r10k/util/subprocess.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The subprocess namespace implements an interface similar to childprocess. The interface has been simplified to make it easier to use and does not depend on native code.

Defined Under Namespace

Classes: Result, Runner, SubprocessError

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level

Constructor Details

#initialize(argv) ⇒ Subprocess

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepare the subprocess invocation.

Parameters:

  • argv (Array<String>)

    The argument vector to execute



54
55
56
57
58
# File 'lib/r10k/util/subprocess.rb', line 54

def initialize(argv)
  @argv = argv

  @raise_on_fail = false
end

Instance Attribute Details

#argvObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



33
34
35
# File 'lib/r10k/util/subprocess.rb', line 33

def argv
  @argv
end

#cwdString

Returns The directory to be used as the cwd when executing the command.

Returns:

  • (String)

    The directory to be used as the cwd when executing the command.



44
45
46
# File 'lib/r10k/util/subprocess.rb', line 44

def cwd
  @cwd
end

#logger=(value) ⇒ Object (writeonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
# File 'lib/r10k/util/subprocess.rb', line 49

def logger=(value)
  @logger = value
end

#raise_on_failtrue, false

Determine whether #execute raises an error when the command exits with a non-zero exit status.

Returns:

  • (true, false)


39
40
41
# File 'lib/r10k/util/subprocess.rb', line 39

def raise_on_fail
  @raise_on_fail
end

Class Method Details

.runnerClass < R10K::Util::Subprocess::Runner]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Class < R10K::Util::Subprocess::Runner].

Returns:



19
20
21
22
23
24
25
26
27
# File 'lib/r10k/util/subprocess.rb', line 19

def self.runner
  if R10K::Util::Platform.windows?
    R10K::Util::Subprocess::Runner::Windows
  elsif R10K::Util::Platform.jruby?
    R10K::Util::Subprocess::Runner::JRuby
  else
    R10K::Util::Subprocess::Runner::POSIX
  end
end

Instance Method Details

#executeR10K::Util::Subprocess::Result

Execute the given command and return the result of evaluation.

Returns:

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/r10k/util/subprocess.rb', line 66

def execute
  subprocess = self.class.runner.new(@argv)
  subprocess.cwd = @cwd if @cwd

  logmsg = _("Starting process: %{args}") % {args: @argv.inspect}
  logmsg << "(cwd: #{@cwd})" if @cwd
  logger.debug2(logmsg)

  result = subprocess.run
  logger.debug2(_("Finished process:\n%{result}") % {result: result.format})

  if @raise_on_fail && result.failed?
    raise SubprocessError.new(_("Command exited with non-zero exit code"), :result => result)
  end

  result
end