Class: Cassie::Support::SystemCommand
- Defined in:
- lib/cassie/support/system_command.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#binary ⇒ Object
readonly
Returns the value of attribute binary.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
-
.exist?(name) ⇒ Boolean
Indicates whether a binary exists in the current user’s PATH.
-
.which(name) ⇒ String?
Find the path to the executable file, using the current user’s PATH.
Instance Method Summary collapse
-
#completed? ⇒ Boolean
True if the command completed execution and success bits were set, regardless of the exit status code.
- #exist? ⇒ Boolean
-
#exitcode ⇒ Fixnum
Runs the command if it hasn’t been run yet.
- #failure_message ⇒ Object
-
#initialize(*cmd) ⇒ SystemCommand
constructor
Creates a new SystemCommand object that has not yet been executed.
-
#run ⇒ Boolean
Runs the command.
-
#run? ⇒ Boolean
False if the command hasn’t been run yet.
-
#succeed ⇒ Boolean
Runs the command, expecting an exit status of 0.
-
#success? ⇒ Boolean
True if command has been run, and exited with status of 0, otherwise returns false.
- #which ⇒ Object
Constructor Details
#initialize(command) ⇒ SystemCommand #initialize(binary, [args][]) ⇒ SystemCommand #initialize(binary, arg, ...) ⇒ SystemCommand
Creates a new SystemCommand object that has not yet been executed.
77 78 79 80 81 82 83 84 85 |
# File 'lib/cassie/support/system_command.rb', line 77 def initialize(*cmd) @args = [] cmd.flatten.each{|a| @args += a.to_s.split(" ")} @command = args.join(" ") @command = command + " 2>&1" unless command =~ / > / @binary = @args.shift end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
4 5 6 |
# File 'lib/cassie/support/system_command.rb', line 4 def args @args end |
#binary ⇒ Object (readonly)
Returns the value of attribute binary.
4 5 6 |
# File 'lib/cassie/support/system_command.rb', line 4 def binary @binary end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
4 5 6 |
# File 'lib/cassie/support/system_command.rb', line 4 def command @command end |
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
4 5 6 |
# File 'lib/cassie/support/system_command.rb', line 4 def duration @duration end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
4 5 6 |
# File 'lib/cassie/support/system_command.rb', line 4 def output @output end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
4 5 6 |
# File 'lib/cassie/support/system_command.rb', line 4 def status @status end |
Class Method Details
.exist?(name) ⇒ Boolean
Indicates whether a binary exists in the current user’s PATH
9 10 11 |
# File 'lib/cassie/support/system_command.rb', line 9 def self.exist?(name) !!which(name) end |
.which(name) ⇒ String?
Find the path to the executable file, using the current user’s PATH
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cassie/support/system_command.rb', line 16 def self.which(name) # windows support exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| exts.each { |ext| exe = File.join(path, "#{name}#{ext}") return exe if File.executable?(exe) && !File.directory?(exe) } end return nil end |
Instance Method Details
#completed? ⇒ Boolean
Returns true if the command completed execution and success bits were set, regardless of the exit status code. false if the command hasn’t been executed, failed to exit, or crashed.
138 139 140 141 |
# File 'lib/cassie/support/system_command.rb', line 138 def completed? return false unless run? status.exited? && @status.success? #status.success is NOT the exit code == 0! end |
#exist? ⇒ Boolean
87 88 89 |
# File 'lib/cassie/support/system_command.rb', line 87 def exist? self.class.exist?(binary) end |
#exitcode ⇒ Fixnum
Runs the command if it hasn’t been run yet.
125 126 127 |
# File 'lib/cassie/support/system_command.rb', line 125 def exitcode status.exitstatus end |
#failure_message ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/cassie/support/system_command.rb', line 143 def msg = "---------------------\n" msg << red(output) msg << "---------------------\n" msg << "Failed to execute `#{command}`:\n" msg << "\tPlease check the output above for any errors and make sure that `#{binary}` is installed in your PATH with proper permissions." msg end |
#run ⇒ Boolean
Runs the command
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cassie/support/system_command.rb', line 97 def run t1=Time.now IO.popen(command) do |io| @status=Process.waitpid2(io.pid)[1] @output=io.read.sub(/\n\z/, "") end @duration=Time.now-t1 completed? end |
#run? ⇒ Boolean
Returns false if the command hasn’t been run yet.
119 120 121 |
# File 'lib/cassie/support/system_command.rb', line 119 def run? !!@duration end |
#succeed ⇒ Boolean
Runs the command, expecting an exit status of 0
112 113 114 115 116 |
# File 'lib/cassie/support/system_command.rb', line 112 def succeed fail unless run && success? true end |
#success? ⇒ Boolean
Returns true if command has been run, and exited with status of 0, otherwise returns false.
131 132 133 134 |
# File 'lib/cassie/support/system_command.rb', line 131 def success? return false unless run? exitcode == 0 end |
#which ⇒ Object
91 92 93 |
# File 'lib/cassie/support/system_command.rb', line 91 def which self.class.which(binary) end |