Sha256: 0362bcec97b74c68511f8b25bcb75a725a169435ab220f2ee5d0c6a8729e6dea

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

module GitWrapper
  module Commands

    module Shell

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

      private

      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

      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

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
git_wrapper-1.1.2 lib/git_wrapper/commands/shell.rb
git_wrapper-1.1.1 lib/git_wrapper/commands/shell.rb
git_wrapper-1.1.0 lib/git_wrapper/commands/shell.rb