Class: RightDevelop::Utility::Shell

Inherits:
RightGit::Shell::Default
  • Object
show all
Defined in:
lib/right_develop/utility/shell.rb

Overview

extends default shell easy-singleton from right_git gem.

Defined Under Namespace

Classes: NullLoggerSingleton

Instance Method Summary collapse

Instance Method Details

#configure_executioner(executioner, options) ⇒ Object

Overrides ::RightGit::Shell::Default#configure_executioner



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/right_develop/utility/shell.rb', line 119

def configure_executioner(executioner, options)
  # super configure early to ensure that any custom env vars are set after
  # restoring the pre-bundler env.
  executioner = super(executioner, options)

  # clean all bundler env vars, if requested.
  if options[:clean_bundler_env]
    executioner = wrap_executioner_with_clean_env(executioner)
  end
  executioner
end

#default_loggerLogger

Returns RightSupport::Log::Mixin.default_logger.

Returns:

  • (Logger)

    RightSupport::Log::Mixin.default_logger



82
83
84
# File 'lib/right_develop/utility/shell.rb', line 82

def default_logger
  RightSupport::Log::Mixin.default_logger
end

#execute(cmd, options = {}) ⇒ Integer

Overrides ::RightGit::Shell::Default#execute

Parameters:

  • cmd (String)

    the shell command to run

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

    for execution

Options Hash (options):

  • :directory (String)

    to use as working directory during command execution or nil

  • :logger (Logger)

    logger for shell execution (default = STDOUT)

  • :outstream (IO)

    output stream to receive STDOUT and STDERR from command (default = none)

  • :raise_on_failure (TrueClass|FalseClass)

    if true, wil raise a RuntimeError if the command does not end successfully (default), false to ignore errors

  • :set_env_vars (Hash)

    environment variables to set during execution (default = none set)

  • :clear_env_vars (Hash)

    environment variables to clear during execution (default = none cleared but see :clean_bundler_env)

  • :clean_bundler_env (TrueClass|FalseClass)

    true to clear all bundler environment variables during execution (default), false to inherit bundler env from parent

  • :sudo (TrueClass|FalseClass)

    if true, will wrap command in sudo if needed, false to run as current user (default)

  • :timeout (Numeric)

    to kill spawned process when time (in seconds) expires

Returns:

  • (Integer)

    exitstatus of the command

Raises:

  • (ShellError)

    on failure only if :raise_on_failure is true



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/right_develop/utility/shell.rb', line 103

def execute(cmd, options = {})
  options = {
    :clean_bundler_env => true,
    :sudo              => false
  }.merge(options)

  if options[:sudo]
    fail "Not available in Windows" if is_windows?
    cmd = "sudo #{cmd}" unless ::Process.euid == 0
  end

  # super execute.
  super(cmd, options)
end

#is_windows?TrueClass|FalseClass

Returns true if running on Windows platform.

Returns:

  • (TrueClass|FalseClass)

    true if running on Windows platform



70
71
72
# File 'lib/right_develop/utility/shell.rb', line 70

def is_windows?
  return !!(RUBY_PLATFORM =~ /mswin|win32|dos|mingw|cygwin/)
end

#null_loggerLogger

Creates a null logger.

Returns:

  • (Logger)

    the null logger



77
78
79
# File 'lib/right_develop/utility/shell.rb', line 77

def null_logger
  NullLoggerSingleton.instance
end

#setup_clean_envObject

bundle exec sets GEM_HOME and GEM_PATH (in Windows?) and these need to be wacked in order to have a pristing rubygems environment since bundler won’t clean them. also, if you ‘bundle exec rake …’ and then put arguments to the right of the task name, then these args won’t appear in Bundler::ORIGINAL_ENV. example: “bundle exec rake build:all DEBUG=true …”



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/right_develop/utility/shell.rb', line 49

def setup_clean_env
  # user may be running gem binary directly without bundler.
  if defined?(::Bundler)
    # a little revisionist history music...
    ::ENV.each do |key, value|
      if key.start_with?('GEM_') || key.start_with?('BUNDLER_')
        ::Bundler::ORIGINAL_ENV[key] = nil
      elsif Bundler::ORIGINAL_ENV[key].nil?
        ::Bundler::ORIGINAL_ENV[key] = value
      end
    end
    ::Bundler.with_clean_env do
      # now the ENV is clean and not missing any right-hand args so replace
      # the ORIGINAL_ENV.
      ::Bundler::ORIGINAL_ENV.replace(ENV)
    end
  end
  true
end

#wrap_executioner_with_clean_env(executioner) ⇒ Proc

Encapsulates executioner with bundler-defeating logic, but only if bundler has been loaded by current process.

Parameters:

  • executioner (Proc)

    to conditionally wrap

Returns:

  • (Proc)

    wrapped or original executioner



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/right_develop/utility/shell.rb', line 137

def wrap_executioner_with_clean_env(executioner)
  # only if bundler is loaded.
  if defined?(::Bundler)
    # double-lambda, single call freezes the inner call made to previous
    # definition of executioner.
    executioner = lambda do |e|
      lambda { ::Bundler.with_clean_env { e.call } }
    end.call(executioner)
  end
  executioner
end