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

Modules: POSIX, Windows Classes: IO, 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

formatter, included, level, level=, levels, #logger, #logger_name, outputter, 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



56
57
58
59
60
# File 'lib/r10k/util/subprocess.rb', line 56

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.



35
36
37
# File 'lib/r10k/util/subprocess.rb', line 35

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.



46
47
48
# File 'lib/r10k/util/subprocess.rb', line 46

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.



51
52
53
# File 'lib/r10k/util/subprocess.rb', line 51

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)


41
42
43
# File 'lib/r10k/util/subprocess.rb', line 41

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:



23
24
25
26
27
28
29
# File 'lib/r10k/util/subprocess.rb', line 23

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

Instance Method Details

#executeR10K::Util::Subprocess::Result

Execute the given command and return the result of evaluation.

Returns:

Raises:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/r10k/util/subprocess.rb', line 68

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

  logmsg = "Execute: #{@argv.join(' ')}"
  logmsg << "(cwd: #{@cwd})" if @cwd
  logger.debug1 logmsg

  subprocess.run

  result = subprocess.result

  logger.debug2 "[#{result.cmd}] STDOUT: #{result.stdout.chomp}" unless result.stdout.empty?
  logger.debug2 "[#{result.cmd}] STDERR: #{result.stderr.chomp}" unless result.stderr.empty?

  if @raise_on_fail and subprocess.crashed?
    raise SubprocessError.new(:result => result)
  end

  result
end