Module: GitWrapper::Commands::Shell

Defined in:
lib/git_wrapper/commands/shell.rb

Class Method Summary collapse

Class Method Details

.execute(command, options = {}) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/git_wrapper/commands/shell.rb', line 6

def self.execute(command, options={})
  if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
    jruby_execute(command, options)
  else
    ruby_execute(command, options)
  end
end

.jruby_execute(command, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/git_wrapper/commands/shell.rb', line 34

def self.jruby_execute(command, options={})
  location_folder = options[:chdir] || '.'

  prev_stdout = $stdout
  prev_stderr = $stderr

  $stdout = StringIO.new
  $stderr = StringIO.new

  begin
    Dir.chdir location_folder do
      system(command)
    end
    status = $?
    $stdout.rewind
    $stderr.rewind
    if block_given?
      yield($stdout.readlines.join.force_encoding('UTF-8'), $stderr.readlines.join.force_encoding('UTF-8'), status.pid)
      result = status
    else
      result = $stdout.readlines.join.force_encoding('UTF-8'), $stderr.readlines.join.force_encoding('UTF-8'), status
    end
  ensure
    $stdout = prev_stdout
    $stderr = prev_stderr
  end

  return result
end

.ruby_execute(command, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/git_wrapper/commands/shell.rb', line 14

def self.ruby_execute(command, options={})
  location_folder = options[:chdir] || '.'
  result = nil

  Open3.popen3(command, :chdir => location_folder) do |stdin, stdout, stderr, wait_thr|
    output = stdout.readlines.join
    error = stderr.readlines.join
    status = wait_thr.value

    if block_given?
      result = status
      yield(output, error, wait_thr.pid)
    else
      result = [output, error, status]
    end
  end

  return result
end