class RVM::Shell::SingleShotWrapper

Implementation of the abstract wrapper class that opens a new instance of bash when a command is run, only keeping it around for the lifetime of the command. Possibly inefficient but for the moment simplest and hence default implementation.

Attributes

current[RW]

Public Instance Methods

run_command(command) click to toggle source

Runs a given command in the current shell. Defaults the command to true if empty.

# File lib/rvm/shell/single_shot_wrapper.rb, line 15
def run_command(command)
  command = "true" if command.to_s.strip.empty?
  with_shell_instance do
    stdin.puts wrapped_command(command)
    stdin.close
    out, err = stdout.read, stderr.read
    out, status, _ = raw_stdout_to_parts(out)
    return status, out, err
  end
end
run_command_silently(command) click to toggle source

Runs a command, ensuring no output is collected.

# File lib/rvm/shell/single_shot_wrapper.rb, line 27
def run_command_silently(command)
  with_shell_instance do
    stdin.puts silent_command(command)
  end
end

Protected Instance Methods

stderr() click to toggle source
# File lib/rvm/shell/single_shot_wrapper.rb, line 52
def stderr; @current[2]; end
stdin() click to toggle source

Direct access to each of the named descriptors

# File lib/rvm/shell/single_shot_wrapper.rb, line 50
def stdin;  @current[0]; end
stdout() click to toggle source
# File lib/rvm/shell/single_shot_wrapper.rb, line 51
def stdout; @current[1]; end
with_shell_instance() { || ... } click to toggle source

yields stdio, stderr and stdin for a shell instance. If there isn't a current shell instance, it will create a new one. In said scenario, it will also cleanup once it is done.

# File lib/rvm/shell/single_shot_wrapper.rb, line 38
def with_shell_instance(&blk)
  no_current = @current.nil?
  if no_current
    @current = Open3.popen3(self.shell_executable)
    invoke_setup!
  end
  yield
ensure
  @current = nil if no_current
end