Module RSCM::CommandLine
In: lib/rscm/command_line.rb

Methods

execute  

Classes and Modules

Class RSCM::CommandLine::ExecutionError
Class RSCM::CommandLine::OptionError

Constants

QUOTE_REPLACEMENT = (Platform.family == "mswin32") ? "\"" : "\\\""
LESS_THAN_REPLACEMENT = (Platform.family == "mswin32") ? "<" : "\\<"

Public Instance methods

Executes cmd. If the +:stdout+ and +:stderr+ options are specified, a line consisting of a prompt (including cmd) will be appended to the respective output streams will be appended to those files, followed by the output itself. Example:

  CommandLine.execute("echo hello world", {:stdout => "stdout.log", :stderr => "stderr.log"})

will result in the following being written to stdout.log:

  /Users/aslakhellesoy/scm/buildpatterns/repos/damagecontrol/trunk aslakhellesoy$ echo hello world
  hello world

-and to stderr.log:

  /Users/aslakhellesoy/scm/buildpatterns/repos/damagecontrol/trunk aslakhellesoy$ echo hello world

If a block is passed, the stdout io will be yielded to it (as with IO.popen). In this case the output will not be written to the stdout file (even if it‘s specified):

  /Users/aslakhellesoy/scm/buildpatterns/repos/damagecontrol/trunk aslakhellesoy$ echo hello world
  [output captured and therefore not logged]

If the exitstatus of the command is different from the value specified by the +:exitstatus+ option (which defaults to 0) then an ExecutionError is raised, its message containing the last 400 bytes of stderr (provided +:stderr+ was specified)

You can also specify the +:dir+ option, which will cause the command to be executed in that directory (default is current directory).

You can also specify a hash of environment variables in +:env+, which will add additional environment variables to the default environment.

Finally, you can specify several commands within one by separating them with ’&&’ (as you would in a shell). This will result in several lines to be appended to the log (as if you had executed the commands separately).

See the unit test for more examples.

[Source]

    # File lib/rscm/command_line.rb, line 57
57:     def execute(cmd, options={}, &proc)
58:       raise "Can't have newline in cmd" if cmd =~ /\n/
59:       options = {
60:         :dir => Dir.pwd,
61:         :env => {},
62:         :mode => 'r',
63:         :exitstatus => 0
64:       }.merge(options)
65:       
66:       options[:stdout] = File.expand_path(options[:stdout]) if options[:stdout]
67:       options[:stderr] = File.expand_path(options[:stderr]) if options[:stderr]
68: 
69:       if options[:dir].nil?
70:         e(cmd, options, &proc)
71:       else
72:         Dir.chdir(options[:dir]) do
73:           e(cmd, options, &proc)
74:         end
75:       end
76:     end

[Validate]