Class: Commands::Command
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
- 
  
    
      #args  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute args. 
- 
  
    
      #command  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute command. 
- 
  
    
      #dir  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute dir. 
- 
  
    
      #error  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute error. 
- 
  
    
      #input  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute input. 
- 
  
    
      #open_mode  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute open_mode. 
- 
  
    
      #output  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute output. 
Class Method Summary collapse
- 
  
    
      .[](*args)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Command[‘wc -l’].sh. 
Instance Method Summary collapse
- 
  
    
      #+(rhs)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Chain two commands (like a ‘;’ in shell), and return a new one. 
- 
  
    
      #<(arg)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Return a new command with the given command input. 
- 
  
    
      #<<(arg)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Add an argument to a command. 
- 
  
    
      #>(arg)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Return a new command with the given command output. 
- 
  
    
      #>>(arg)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Like > but open the output in append mode. 
- 
  
    
      #[](*args)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Supply return a new commands with these arguments added. 
- 
  
    
      #arg_string  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Misc. 
- 
  
    
      #exec(*options)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Use the ExecRunner. 
- 
  
    
      #expand  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Use Kernel#‘ and return the resulting string. 
- #fork(*options) ⇒ Object
- 
  
    
      #initialize(command_name, *args)  ⇒ Command 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Construction methods. 
- #instanciate_args ⇒ Object
- #instanciate_args! ⇒ Object
- #popen(*options) ⇒ Object
- 
  
    
      #ruby  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    FIXME design me! FIXME make me a runner. 
- 
  
    
      #run(runner = @runner)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Use a Commands::Runners::Runner and return a Commands::Datas::Data. 
- #run_with_options(runner_class, options) ⇒ Object
- 
  
    
      #sh  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Use Kernel#system() but with a string to have the shell expansion. 
- 
  
    
      #sh!  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Use Kernel#exec() but with a string to have the shell expansion. 
- #sh_args ⇒ Object
- 
  
    
      #sys(*options)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    FIXME test me. 
- 
  
    
      #system(*options)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Simulate Kernel#system() to run the command (No shell expansion). 
- #to_a ⇒ Object
- #to_cmd ⇒ Object
- 
  
    
      #to_s  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Conversion methods. 
- #to_sh ⇒ Object
- #whereis(path = ) ⇒ Object
- 
  
    
      #|(rhs)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Pipe two commands. 
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
#args ⇒ Object
Returns the value of attribute args.
| 15 16 17 | # File 'lib/commands/command.rb', line 15 def args @args end | 
#command ⇒ Object
Returns the value of attribute command.
| 10 11 12 | # File 'lib/commands/command.rb', line 10 def command @command end | 
#dir ⇒ Object
Returns the value of attribute dir.
| 11 12 13 | # File 'lib/commands/command.rb', line 11 def dir @dir end | 
#error ⇒ Object
Returns the value of attribute error.
| 14 15 16 | # File 'lib/commands/command.rb', line 14 def error @error end | 
#input ⇒ Object
Returns the value of attribute input.
| 12 13 14 | # File 'lib/commands/command.rb', line 12 def input @input end | 
#open_mode ⇒ Object
Returns the value of attribute open_mode.
| 16 17 18 | # File 'lib/commands/command.rb', line 16 def open_mode @open_mode end | 
#output ⇒ Object
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_string ⇒ Object
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 ( * ) (Runners::Exec, .flatten) end | 
#expand ⇒ Object
Use Kernel#‘ and return the resulting string. FIXME make me a runner
| 101 102 103 | # File 'lib/commands/command.rb', line 101 def `#{to_sh}` end | 
#fork(*options) ⇒ Object
| 111 112 113 | # File 'lib/commands/command.rb', line 111 def fork ( * ) (Runners::Fork, .flatten) end | 
#instanciate_args ⇒ Object
| 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 ( * ) (Runners::Popen, .flatten) end | 
#ruby ⇒ Object
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 ( runner_class, ) if .empty? runner = @@default_runners[runner_class] ||= runner_class.new else runner = runner_class.new .each do |k| runner.send(@@running_options[k]) end end run(runner) end | 
#sh ⇒ Object
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_args ⇒ Object
| 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 ( * ) (Runners::System, [:v, :r] + .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 ( * ) (Runners::System, .flatten) end | 
#to_a ⇒ Object
| 265 266 267 | # File 'lib/commands/command.rb', line 265 def to_a [@command.dup, *@args.map { |arg| arg.to_s }] end | 
#to_cmd ⇒ Object
| 269 270 271 | # File 'lib/commands/command.rb', line 269 def to_cmd self end | 
#to_s ⇒ Object
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_sh ⇒ Object
| 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 |