Class: Commands::Command

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

Direct Known Subclasses

Factory, Pipe, Seq

Constant Summary collapse

@@default_runners =

Running methods.

{}
@@running_options =
{
  :verbose => :make_verbose,
  :v       => :make_verbose,
  :raise   => :raise_on_failures,
  :r       => :raise_on_failures,
  :debug   => :make_debug,
  :d       => :make_debug,
  :mock    => :make_mock,
  :m       => :make_mock,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_name, *args) ⇒ Command

Construction methods.



22
23
24
25
26
27
# File 'lib/commands/command.rb', line 22

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.



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

def args
  @args
end

#commandObject

Returns the value of attribute command.



10
11
12
# File 'lib/commands/command.rb', line 10

def command
  @command
end

#dirObject

Returns the value of attribute dir.



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

def dir
  @dir
end

#errorObject

Returns the value of attribute error.



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

def error
  @error
end

#inputObject

Returns the value of attribute input.



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

def input
  @input
end

#open_modeObject

Returns the value of attribute open_mode.



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

def open_mode
  @open_mode
end

#outputObject

Returns the value of attribute output.



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

def output
  @output
end

Class Method Details

.[](*args) ⇒ Object

Command[‘wc -l’].sh



32
33
34
# File 'lib/commands/command.rb', line 32

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

Instance Method Details

#+(rhs) ⇒ Object

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



152
153
154
155
156
157
158
159
# File 'lib/commands/command.rb', line 152

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.



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

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



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

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

#>(arg) ⇒ Object

Return a new command with the given command output.



180
181
182
183
# File 'lib/commands/command.rb', line 180

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.



187
188
189
190
191
# File 'lib/commands/command.rb', line 187

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.



144
145
146
147
148
# File 'lib/commands/command.rb', line 144

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

#arg_stringObject

Misc



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

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

#exec(*options) ⇒ Object

Use the ExecRunner.



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

def exec ( *options )
  run_with_options(Runners::Exec, options.flatten)
end

#expandObject

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



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

def expand
  `#{to_sh}`
end

#fork(*options) ⇒ Object



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

def fork ( *options )
  run_with_options(Runners::Fork, options.flatten)
end

#instanciate_argsObject



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

def instanciate_args
  copy = dup
  copy.instanciate_args!
  copy
end

#instanciate_args!Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/commands/command.rb', line 235

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

#popen(*options) ⇒ Object



106
107
108
# File 'lib/commands/command.rb', line 106

def popen ( *options )
  run_with_options(Runners::Popen, options.flatten)
end

#rubyObject

FIXME design me! FIXME make me a runner



118
119
# File 'lib/commands/command.rb', line 118

def ruby
end

#run(runner = @runner) ⇒ Object

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



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

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

#run_with_options(runner_class, options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/commands/command.rb', line 54

def run_with_options ( runner_class, options )
  if options.empty?
    runner = @@default_runners[runner_class] ||= runner_class.new
  else
    runner = runner_class.new
    options.each do |k|
      runner.send(@@running_options[k])
    end
  end
  run(runner)
end

#shObject

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



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

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



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

def sh!
  Kernel.exec(to_sh)
end

#sh_argsObject



256
257
258
259
260
261
262
# File 'lib/commands/command.rb', line 256

def sh_args
  args = ''
  [ ['<', input], ['>', output], ['2>', error] ].each do |str, io|
    args += " #{str} #{io.to_s.dump}" if io and not (io.respond_to? :pipe? and io.pipe?)
  end
  args
end

#sys(*options) ⇒ Object

FIXME test me



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

def sys ( *options )
  run_with_options(Runners::System, [:v, :r] + options.flatten)
end

#system(*options) ⇒ Object

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



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

def system ( *options )
  run_with_options(Runners::System, options.flatten)
end

#to_aObject



265
266
267
# File 'lib/commands/command.rb', line 265

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

#to_cmdObject



269
270
271
# File 'lib/commands/command.rb', line 269

def to_cmd
  self
end

#to_sObject

Conversion methods



222
223
224
225
226
227
# File 'lib/commands/command.rb', line 222

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

#to_shObject



230
231
232
233
# File 'lib/commands/command.rb', line 230

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

#whereis(path = ) ⇒ Object



213
214
215
# File 'lib/commands/command.rb', line 213

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

#|(rhs) ⇒ Object

Pipe two commands.



138
139
140
# File 'lib/commands/command.rb', line 138

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