Sha256: 056749cc93f05cb85f7902d23831fbe0e720885e9104d192c3f9ae85f7f92722

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

module Hem
  module Lib
    module Local
      class Command
        attr_accessor :opts, :command

        def initialize command, opts = {}
          @command = command
          @opts = {
              :auto_echo => false,
              :pwd => opts[:pwd] || Hem.project_path,
              :append => ''
          }.merge(opts)
        end

        def pipe cmd, pipe_opts = {}
          pipe_opts = pipe_opts.merge({ :on => :host })
          cmd = "echo #{cmd.shellescape}" if @opts[:auto_echo]

          case pipe_opts[:on]
            when :host
              @pipe = cmd
            else
              raise "Unknown pipe source: #{pipe_opts[:on]}"
          end
          return self
        end

        def << cmd
          pipe cmd, :on => :host
        end

        def < cmd
          pipe cmd, :on => :host
        end

        def run
          return if @command.nil?
          Hem::Helper.shell @command, @opts
        end

        def to_s
          pwd_set_command = "cd #{@opts[:pwd].shellescape} && exec /bin/bash"

          command = [
              pwd_set_command,
              @command.empty? ? nil : @command.shellescape
          ].compact.join(" -c ") + "#{opts[:append].shellescape}"

          [
              @pipe,
              "(#{command})"
          ].compact.join(" | ")
        end

        def to_str
          to_s
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hem-1.2.4 lib/hem/lib/local/command.rb
hem-1.2.3 lib/hem/lib/local/command.rb
hem-1.2.2 lib/hem/lib/local/command.rb
hem-1.2.1 lib/hem/lib/local/command.rb