Sha256: 178891b846ab1b1b9887ee59c31edf09885b85e056626c097a23ec591aedeee1

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

require 'open3'
require 'tempfile'

module Hobo
  module Helper

    def bundle_shell *args, &block
      has_bundle = begin
        shell "bundle", "exec", "ruby -v"
        true
      rescue ::Hobo::ExternalCommandError
        false
      end

      if has_bundle
        args = [ 'bundle', 'exec' ] + args
      end

      shell *args, &block
    end

    def shell *args, &block
      opts = (args.size > 1 && args.last.is_a?(Hash)) ? args.pop : {}
      opts = {
        :capture => false,
        :indent => 0,
        :realtime => false,
        :env => {}
      }.merge! opts

      Hobo::Logging.logger.debug("helper.shell: Invoking '#{args.join(" ")}' with #{opts.to_s}")

      indent = " " * opts[:indent]
      ::Open3.popen3 opts[:env], *args do |stdin, out, err, external|
        buffer = ::Tempfile.new 'hobo_run_buf'
        buffer.sync = true
        threads = [external]

        ## Create a thread to read from each stream
        { :out => out, :err => err }.each do |key, stream|
          threads.push(::Thread.new do
            until (line = stream.gets).nil? do
              line = ::Hobo.ui.color(line.strip, :error) if key == :err
              buffer.write("#{line.strip}\n")
              Hobo::Logging.logger.debug("helper.shell: #{line.strip}")
              line = yield line if block
              puts indent + line if opts[:realtime] && !line.nil?
            end
          end)
        end

        threads.each do |t|
          t.join
        end

        buffer.fsync
        buffer.rewind

        raise ::Hobo::ExternalCommandError.new(args.join(" "), external.value.exitstatus, buffer) if external.value.exitstatus != 0

        return opts[:capture] ? buffer.read.strip : nil
      end
    end
  end
end

include Hobo::Helper

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hobo-inviqa-0.0.6 lib/hobo/helper/shell.rb