Class: ObjectiveCommand::Commands::Command

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

Direct Known Subclasses

Factory, Pipe, Seq

Constant Summary collapse

@@default_runners =

Running methods.

{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_name, *args) ⇒ Command

Construction methods.



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

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

Instance Attribute Details

#argsObject

Returns the value of attribute args.



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

def args
  @args
end

#commandObject

Returns the value of attribute command.



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

def command
  @command
end

#dirObject

Returns the value of attribute dir.



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

def dir
  @dir
end

#errorObject

Returns the value of attribute error.



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

def error
  @error
end

#inputObject

Returns the value of attribute input.



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

def input
  @input
end

#open_modeObject

Returns the value of attribute open_mode.



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

def open_mode
  @open_mode
end

#outputObject

Returns the value of attribute output.



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

def output
  @output
end

Instance Method Details

#+(rhs) ⇒ Object

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



132
133
134
135
136
137
138
139
# File 'lib/objective_command/commands/command.rb', line 132

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.



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

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



178
179
180
181
# File 'lib/objective_command/commands/command.rb', line 178

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

#>(arg) ⇒ Object

Return a new command with the given command output.



160
161
162
163
# File 'lib/objective_command/commands/command.rb', line 160

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.



167
168
169
170
171
# File 'lib/objective_command/commands/command.rb', line 167

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.



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

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

#arg_stringObject

Misc



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

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

#exec(*options) ⇒ Object

Use the ExecRunner.



54
55
56
# File 'lib/objective_command/commands/command.rb', line 54

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

#expandObject

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



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

def expand
  `#{to_sh}`
end

#fork(*options) ⇒ Object



91
92
93
# File 'lib/objective_command/commands/command.rb', line 91

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

#freezeObject



259
260
261
262
263
264
# File 'lib/objective_command/commands/command.rb', line 259

def freeze
  super
  for i in [@command, @dir, @input, @output, @error, @args] do
    i.freeze
  end
end

#inspect_for_unified_matchersObject



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

def inspect_for_unified_matchers
  "#{to_s.inspect}.to_ocmd"
end

#instanciate_argsObject



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

def instanciate_args
  copy = dup
  copy.instanciate_args!
  copy
end

#instanciate_args!Object



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

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



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

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

#rubyObject

FIXME design me! FIXME make me a runner



98
99
# File 'lib/objective_command/commands/command.rb', line 98

def ruby
end

#run(runner = @runner) ⇒ Object

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



103
104
105
106
107
108
# File 'lib/objective_command/commands/command.rb', line 103

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



37
38
39
40
41
42
43
44
# File 'lib/objective_command/commands/command.rb', line 37

def run_with_options ( runner_class, options )
  if options.empty?
    runner = @@default_runners[runner_class] ||= runner_class.new
  else
    runner = runner_class.new.apply_options options
  end
  run(runner)
end

#shObject

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



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

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



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

def sh!
  Kernel.exec(to_sh)
end

#sh_argsObject



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

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

#sys(*options) ⇒ Object

FIXME test me



60
61
62
# File 'lib/objective_command/commands/command.rb', line 60

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).



48
49
50
# File 'lib/objective_command/commands/command.rb', line 48

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

#to_aObject



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

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

#to_ocmdObject



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

def to_ocmd
  self
end

#to_sObject

Conversion methods



202
203
204
205
206
207
# File 'lib/objective_command/commands/command.rb', line 202

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

#to_shObject



210
211
212
213
# File 'lib/objective_command/commands/command.rb', line 210

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

#whereis(path = ) ⇒ Object



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

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

#|(rhs) ⇒ Object

Pipe two commands.



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

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