Class: Patir::ShellCommand

Inherits:
Object
  • Object
show all
Includes:
Command
Defined in:
lib/patir/command.rb

Overview

This class wraps the codeforpeople.com/lib/ruby/systemu/ as a Command duck.

It allows for execution of any shell command.

Accepted keys are

:cmd should be the shell command to execute (required - ParameterException will be raised).

:working_directory for specifying the working directory (default is ‘.’) and :name for assigning a name to the command (default is “”).

Instance Attribute Summary

Attributes included from Command

#error, #exec_time, #name, #number, #output, #status, #strategy

Instance Method Summary collapse

Methods included from Command

#executed?, #reset, #run?, #success?

Constructor Details

#initialize(*args) ⇒ ShellCommand

The constructor will throw CommandError if :cmd is missing.

CommandError will also be thrown if :working_directory does not exist.

Raises:



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/patir/command.rb', line 116

def initialize *args
  params=args[0] if args
  raise ParameterException,"No ShellCommand parameters defined" unless params
  @name=params[:name]
  @working_directory=params[:working_directory]
  #initialize the working directory value. This actually means ./
  @working_directory||="."
  #we need a command line :)
  raise ParameterException,"No :command defined" unless params[:cmd]
  @command=params[:cmd]
  @status=:not_executed
end

Instance Method Details

#runObject

Executes the shell command and returns the status



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/patir/command.rb', line 130

def run
  start_time=Time.now
  super
  #create the working directory if it does not exist
  FileUtils::mkdir_p(@working_directory)  unless File.exists?(@working_directory)
  #create the actual command, run it, grab stderr and stdout and set output,error, status and execution time
  status, @output, @error = systemu(@command,:cwd=>@working_directory)
  #lets get the status how we want it
  if status.exited?
    if status.exitstatus==0
      @status=:success
    else
      @status=:error
    end
  else
    @status=:warning
  end
  #set the time it took us
  @exec_time=Time.now-start_time
  return @status
end

#to_sObject



152
153
154
# File 'lib/patir/command.rb', line 152

def to_s
  return "#{@name}: #{@command} in #{File.expand_path(@working_directory)}"
end