Class: Rubish::Command

Inherits:
UnixExecutable show all
Defined in:
lib/rubish/command.rb

Direct Known Subclasses

CommandBuilder

Constant Summary

Constants inherited from UnixExecutable

UnixExecutable::EIO

Instance Attribute Summary collapse

Attributes inherited from Executable

#working_directory

Instance Method Summary collapse

Methods inherited from UnixExecutable

#exec, #exec!

Methods inherited from Executable

#awk, #cd, #each, #each!, #err, #err=, #exec, #exec!, #first, #head, #i, #i=, #io, #last, #map, #map!, #o, #o=, #sed, #tail

Constructor Details

#initialize(cmd, args) ⇒ Command

Returns a new instance of Command.



6
7
8
9
10
# File 'lib/rubish/command.rb', line 6

def initialize(cmd,args)
  @quoted = false
  @args = args
  @cmd = cmd.to_s
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



4
5
6
# File 'lib/rubish/command.rb', line 4

def args
  @args
end

#cmdObject (readonly)

Returns the value of attribute cmd.



4
5
6
# File 'lib/rubish/command.rb', line 4

def cmd
  @cmd
end

#quotedObject (readonly)

if true, arguments for exec are not shell expanded.



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

def quoted
  @quoted
end

Instance Method Details

#%(arg) ⇒ Object



68
69
70
71
# File 'lib/rubish/command.rb', line 68

def %(arg)
  @args = [arg]
  self
end

#+(arg) ⇒ Object



63
64
65
66
# File 'lib/rubish/command.rb', line 63

def +(arg)
  @args << arg
  self
end

#exec_with(i, o, e) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/rubish/command.rb', line 12

def exec_with(i,o,e)
  normalize_args!
  unless pid = Kernel.fork
    # child
    system_exec(i,o,e)
  else
    return [pid]
  end
end

#normalize_args!(args = @args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rubish/command.rb', line 73

def normalize_args!(args=@args)
  args.map! do |arg|
    case arg
    when Symbol
      "-#{arg.to_s[0..-1]}"
    when Array
      # recursively flatten args
      normalize_args!(arg)
    when String
      arg
    else
      raise "argument to command should be one of Symbol, String, Array "
    end
  end
  args.flatten!
  args
end

#qObject



53
54
55
56
# File 'lib/rubish/command.rb', line 53

def q
  @quoted = true
  self
end

#q!Object



58
59
60
61
# File 'lib/rubish/command.rb', line 58

def q!
  @quoted = false
  self
end

#system_exec(i, o, e) ⇒ Object

this method should be called after fork



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubish/command.rb', line 23

def system_exec(i,o,e)
  begin
    # dup2 the given i,o,e to stdin,stdout,stderr
    # close all other file
    Rubish.set_stdioe(i,o,e)
    # exec the command
    if self.quoted
      # use arguments as is
      Kernel.exec self.cmd, *args
    else
      # want shell expansion of arguments
      Kernel.exec "#{self.cmd} #{args.join " "}"
    end
  rescue
    # with just want to kill the child
    # process. When something goes wrong with
    # exec. No cleanup necessary.
    #
    # There's a weird problem with
    # Process.exit(non_zero) raising SystemExit,
    # and that exception somehow reaches the
    # parent process.
    Process.exit!(1)
  end
end

#to_sObject



49
50
51
# File 'lib/rubish/command.rb', line 49

def to_s
  self.inspect
end