Class: RightGit::Shell::Default

Inherits:
Object
  • Object
show all
Includes:
Interface, RightSupport::Ruby::EasySingleton
Defined in:
lib/right_git/shell/default.rb

Overview

Default shell singleton implementation.

Instance Method Summary collapse

Instance Method Details

#clear_env_vars(names) { ... } ⇒ TrueClass

Clears (set-to-nil) the given list of environment variables while executing the given block.

Parameters:

  • names (Array)

    of variables to clear

Yields:

  • called with environment cleared

Returns:

  • (TrueClass)

    always true



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/right_git/shell/default.rb', line 165

def clear_env_vars(names, &block)
  save_vars = {}
  names.each do |k|
    k = k.to_s
    save_vars[k] = ENV[k]
    ENV[k] = nil
  end
  begin
    yield
  ensure
    names.each do |k|
      k = k.to_s
      ENV[k] = save_vars[k]
    end
  end
  true
end

#configure_executioner(executioner, options) ⇒ Proc

Encapsulates the given executioner with child-process-modifying behavior based on options. Builds the executioner as a series of callbacks.

Parameters:

  • executioner (Proc)

    to configure

  • options (Hash)

    for execution

Returns:

  • (Proc)

    configured executioner



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/right_git/shell/default.rb', line 102

def configure_executioner(executioner, options)
  # set specific environment variables, if requested.
  sev = options[:set_env_vars]
  if (sev && !sev.empty?)
    executioner = lambda do |e|
      lambda { set_env_vars(sev) { e.call } }
    end.call(executioner)
  end

  # clear specific environment variables, if requested.
  cev = options[:clear_env_vars]
  if (cev && !cev.empty?)
    executioner = lambda do |e|
      lambda { clear_env_vars(cev) { e.call } }
    end.call(executioner)
  end

  # working directory.
  if directory = options[:directory]
    executioner = lambda do |e, d|
      lambda { ::Dir.chdir(d) { e.call } }
    end.call(executioner, directory)
  end
  executioner
end

#default_loggerObject

Delegates to the RightGit class logger.



38
39
40
# File 'lib/right_git/shell/default.rb', line 38

def default_logger
  ::RightGit::Git::Repository.logger
end

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

Implements execute interface.



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
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/right_git/shell/default.rb', line 43

def execute(cmd, options = {})
  options = {
    :directory        => nil,
    :outstream        => nil,
    :raise_on_failure => true,
    :set_env_vars     => nil,
    :clear_env_vars   => nil,
    :logger => default_logger
  }.merge(options)
  outstream = options[:outstream]

  logger = options[:logger]

  # build execution block.
  exitstatus = nil
  executioner = lambda do
    logger.info("+ #{cmd}")
    ::IO.popen("#{cmd} 2>&1", 'r') do |output|
      output.sync = true
      done = false
      while !done
        begin
          data = output.readline
          if outstream
            outstream << data
          else
            logger.info(data.strip)
          end
        rescue ::EOFError
          done = true
        end
      end
    end
    exitstatus = $?.exitstatus
    if (!$?.success? && options[:raise_on_failure])
      raise ShellError, "Execution failed with exitstatus #{exitstatus}"
    end
  end

  # configure and invoke.
  configure_executioner(executioner, options).call

  return exitstatus
end

#output_for(cmd, options = {}) ⇒ Object

Implements output_for interface.



89
90
91
92
93
# File 'lib/right_git/shell/default.rb', line 89

def output_for(cmd, options = {})
  output = StringIO.new
  execute(cmd, options.merge(:outstream => output))
  output.string
end

#set_env_vars(variables) { ... } ⇒ TrueClass

Sets the given list of environment variables while executing the given block.

Parameters

Yield

Return

Parameters:

  • variables (Hash)

    to set

Yields:

  • called with environment set

Returns:

  • (TrueClass)

    always true



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/right_git/shell/default.rb', line 139

def set_env_vars(variables)
  save_vars = {}
  variables.each do |k, v|
    k = k.to_s
    save_vars[k] = ENV[k]
    ENV[k] = v.nil? ? v : v.to_s
  end
  begin
    yield
  ensure
    variables.each_key do |k|
      k = k.to_s
      ENV[k] = save_vars[k]
    end
  end
  true
end