Class: Hem::Lib::Local::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/hem/lib/local/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, opts = {}) ⇒ Command

Returns a new instance of Command.



7
8
9
10
11
12
13
14
# File 'lib/hem/lib/local/command.rb', line 7

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

Instance Attribute Details

#commandObject

Returns the value of attribute command.



5
6
7
# File 'lib/hem/lib/local/command.rb', line 5

def command
  @command
end

#optsObject

Returns the value of attribute opts.



5
6
7
# File 'lib/hem/lib/local/command.rb', line 5

def opts
  @opts
end

Instance Method Details

#<(cmd) ⇒ Object



33
34
35
# File 'lib/hem/lib/local/command.rb', line 33

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

#<<(cmd) ⇒ Object



29
30
31
# File 'lib/hem/lib/local/command.rb', line 29

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

#pipe(cmd, pipe_opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hem/lib/local/command.rb', line 16

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

#runObject



37
38
39
40
# File 'lib/hem/lib/local/command.rb', line 37

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

#to_sObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hem/lib/local/command.rb', line 42

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

#to_strObject



56
57
58
# File 'lib/hem/lib/local/command.rb', line 56

def to_str
  to_s
end