Class: Commands::Command

Inherits:
Object show all
Defined in:
lib/commands/command.rb

Direct Known Subclasses

Factory, Pipe, Seq

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_name, *args) ⇒ Command

Construction methods.



24
25
26
27
28
29
# File 'lib/commands/command.rb', line 24

def initialize ( command_name, *args )
  @command = command_name
  @args = args.dup
  @open_mode = :w
  @input, @output, @error = nil, nil, nil
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



17
18
19
# File 'lib/commands/command.rb', line 17

def args
  @args
end

#commandObject

Returns the value of attribute command.



12
13
14
# File 'lib/commands/command.rb', line 12

def command
  @command
end

#dirObject

Returns the value of attribute dir.



13
14
15
# File 'lib/commands/command.rb', line 13

def dir
  @dir
end

#errorObject

Returns the value of attribute error.



16
17
18
# File 'lib/commands/command.rb', line 16

def error
  @error
end

#inputObject

Returns the value of attribute input.



14
15
16
# File 'lib/commands/command.rb', line 14

def input
  @input
end

#open_modeObject

Returns the value of attribute open_mode.



18
19
20
# File 'lib/commands/command.rb', line 18

def open_mode
  @open_mode
end

#outputObject

Returns the value of attribute output.



15
16
17
# File 'lib/commands/command.rb', line 15

def output
  @output
end

Class Method Details

.[](*args) ⇒ Object

Command[‘wc -l’].sh



34
35
36
# File 'lib/commands/command.rb', line 34

def self.[] ( *args )
  new(*args)
end

Instance Method Details

#+(rhs) ⇒ Object

Chain two commands (like a ‘;’ in shell), and return a new one.



137
138
139
140
141
142
143
144
# File 'lib/commands/command.rb', line 137

def + ( rhs )
  case rhs
  when Command
    Seq.new(self, rhs)
  else
    self[rhs]
  end
end

#<(arg) ⇒ Object

Return a new command with the given command input.



148
149
150
151
152
# File 'lib/commands/command.rb', line 148

def < ( arg )
  cmd = dup
  cmd.input = arg
  cmd
end

#<<(arg) ⇒ Object

Add an argument to a command. cmd = Command.new(‘ls’) cmd << ‘-la’ cmd.sh



183
184
185
186
# File 'lib/commands/command.rb', line 183

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

#>(arg) ⇒ Object

Return a new command with the given command output.



165
166
167
168
# File 'lib/commands/command.rb', line 165

def > ( arg )
  out, err = (arg.is_a?(Array))? arg : [arg, nil]
  add_outputs(out, err)
end

#>>(arg) ⇒ Object

Like > but open the output in append mode.



172
173
174
175
176
# File 'lib/commands/command.rb', line 172

def >> ( arg )
  out, err = (arg.is_a?(Array))? arg : [arg, nil]
  @open_mode = :a
  add_outputs(out, err)
end

#[](*args) ⇒ Object

Supply return a new commands with these arguments added.



129
130
131
132
133
# File 'lib/commands/command.rb', line 129

def [] ( *args )
  cmd = dup
  cmd.args += args.flatten
  cmd
end

#arg_stringObject

Misc



194
195
196
# File 'lib/commands/command.rb', line 194

def arg_string
  @args.map { |arg| arg.to_s.dump }.join(' ')
end

#execObject

Use the ExecRunner.



54
55
56
57
58
59
# File 'lib/commands/command.rb', line 54

def exec
  unless defined? @@exec_runner
    @@exec_runner = Runners::Exec.new
  end
  run(@@exec_runner)
end

#expandObject

Use Kernel#‘ and return the resulting string. FIXME make me a runner



87
88
89
# File 'lib/commands/command.rb', line 87

def expand
  `#{to_sh}`
end

#instanciate_argsObject



235
236
237
238
239
# File 'lib/commands/command.rb', line 235

def instanciate_args
  copy = dup
  copy.instanciate_args!
  copy
end

#instanciate_args!Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/commands/command.rb', line 220

def instanciate_args!
  if defined? @input and @input and @args.include? '%i'
    @args.map! { |a| (a == '%i')? @input : a }
    @input = nil
  end
  if defined? @output and @output and @args.include? '%o'
    @args.map! { |a| (a == '%o')? @output : a }
    @output = nil
  end
  if defined? @error and @error and @args.include? '%e'
    @args.map! { |a| (a == '%e')? @error : a }
    @error = nil
  end
end

#popenObject Also known as: fork



92
93
94
95
96
97
# File 'lib/commands/command.rb', line 92

def popen
  unless defined? @@fork_runner
    @@fork_runner = Runners::Fork.new
  end
  run(@@fork_runner)
end

#rubyObject

FIXME design me! FIXME make me a runner



103
104
# File 'lib/commands/command.rb', line 103

def ruby
end

#run(runner = @runner) ⇒ Object

Use a Commands::Runners::Runner and return a Commands::Datas::Data.



108
109
110
111
112
113
# File 'lib/commands/command.rb', line 108

def run ( runner=@runner )
  unless runner.respond_to? :run
    raise ArgumentError, "need a runner not: #{runner.inspect}"
  end
  runner.run(self)
end

#shObject

Use Kernel#system() but with a string to have the shell expansion. FIXME make me a runner



73
74
75
# File 'lib/commands/command.rb', line 73

def sh
  Kernel.system(to_sh)
end

#sh!Object

Use Kernel#exec() but with a string to have the shell expansion. FIXME make me a runner



80
81
82
# File 'lib/commands/command.rb', line 80

def sh!
  Kernel.exec(to_sh)
end

#sh_argsObject



241
242
243
244
245
246
247
# File 'lib/commands/command.rb', line 241

def sh_args
  args = ''
  args += " < #{@input.to_s.dump}" if @input
  args += " > #{@output.to_s.dump}" if @output
  args += " 2> #{@error.to_s.dump}" if @error
  args
end

#sysObject

FIXME test me



63
64
65
66
67
68
# File 'lib/commands/command.rb', line 63

def sys
  unless defined? @@sys_runner
    @@sys_runner = Runners::System.new.make_verbose.raise_on_failures
  end
  run(@@sys_runner)
end

#systemObject

Simulate Kernel#system() to run the command (No shell expansion).



45
46
47
48
49
50
# File 'lib/commands/command.rb', line 45

def system
  unless defined? @@system_runner
    @@system_runner = Runners::System.new
  end
  run(@@system_runner)
end

#to_aObject



250
251
252
# File 'lib/commands/command.rb', line 250

def to_a
  [@command.dup, *@args.map { |arg| arg.to_s }]
end

#to_cmdObject



254
255
256
# File 'lib/commands/command.rb', line 254

def to_cmd
  self
end

#to_sObject

Conversion methods



207
208
209
210
211
212
# File 'lib/commands/command.rb', line 207

def to_s
  str = @command.dump
  arg_str = arg_string
  str += ' ' + arg_str unless arg_str.empty?
  str
end

#to_shObject



215
216
217
218
# File 'lib/commands/command.rb', line 215

def to_sh
  cmd = instanciate_args
  "#{cmd.to_s}#{cmd.sh_args}"
end

#whereis(path = ) ⇒ Object



198
199
200
# File 'lib/commands/command.rb', line 198

def whereis ( path=ENV['PATH'] )
  # FIXME
end

#|(rhs) ⇒ Object

Pipe two commands.



123
124
125
# File 'lib/commands/command.rb', line 123

def | ( rhs )
  Pipe.new(self, rhs)
end