Module: RProgram::System
- Defined in:
- lib/rprogram/system.rb
Overview
Class Method Summary collapse
-
.arch ⇒ String
Determines the native architecture.
-
.find_program(name) ⇒ Pathname?
Finds the full-path of the program with the matching name.
-
.find_program_by_names(*names) ⇒ Pathname?
Finds the program matching one of the matching names.
-
.jruby? ⇒ Boolean
Determines if the current Ruby VM is JRuby.
-
.paths ⇒ Array<Pathname>
The directories to search for programs.
-
.platform ⇒ String
Determines the native platform.
-
.ruby_1_8? ⇒ Boolean
Determines if the current Ruby VM is from the 1.8.x family.
-
.run(*arguments) ⇒ Boolean
Runs a program.
-
.sudo(*arguments) ⇒ Boolean
Runs a program under sudo.
-
.sudo? ⇒ Boolean
Determines whether
sudo
is available on the system. -
.sudo_path ⇒ Pathname?
The path to the
sudo
program. -
.sudo_path=(path) ⇒ Pathanme
Sets the path to the
sudo
program. -
.windows? ⇒ Boolean
Determines if the platform is Windows.
Class Method Details
.arch ⇒ String
Determines the native architecture.
27 28 29 |
# File 'lib/rprogram/system.rb', line 27 def self.arch @arch end |
.find_program(name) ⇒ Pathname?
Finds the full-path of the program with the matching name.
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rprogram/system.rb', line 110 def self.find_program(name) # add the `.exe` suffix to the name, if running on Windows name = "#{name}.exe" if windows? paths.each do |dir| full_path = dir.join(name). return full_path if full_path.file? end return nil end |
.find_program_by_names(*names) ⇒ Pathname?
Finds the program matching one of the matching names.
136 137 138 139 140 141 142 143 144 |
# File 'lib/rprogram/system.rb', line 136 def self.find_program_by_names(*names) names.each do |name| if (path = find_program(name)) return path end end return nil end |
.jruby? ⇒ Boolean
Determines if the current Ruby VM is JRuby.
81 82 83 |
# File 'lib/rprogram/system.rb', line 81 def self.jruby? const_defined?(:RUBY_ENGINE) && const_get(:RUBY_ENGINE) == 'jruby' end |
.paths ⇒ Array<Pathname>
The directories to search for programs.
91 92 93 94 95 |
# File 'lib/rprogram/system.rb', line 91 def self.paths @paths ||= ENV['PATH'].split(File::PATH_SEPARATOR).map do |dir| Pathname.new(dir) end end |
.platform ⇒ String
Determines the native platform.
41 42 43 |
# File 'lib/rprogram/system.rb', line 41 def self.platform @platform end |
.ruby_1_8? ⇒ Boolean
Determines if the current Ruby VM is from the 1.8.x family.
69 70 71 |
# File 'lib/rprogram/system.rb', line 69 def self.ruby_1_8? RUBY_VERSION.start_with?('1.8.') end |
.run(path, *arguments) ⇒ Boolean .run(path, *arguments, options) ⇒ Boolean
Runs a program.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/rprogram/system.rb', line 187 def self.run(*arguments) # extra tailing options and ENV variables from arguments case arguments.last when Hash = arguments.pop env = (.delete(:env) || {}) popen = .delete(:popen) else = {} env = {} end # all arguments must be Strings arguments = arguments.map(&:to_s) # print debugging information if RProgram.debug command = '' env.each do |name,value| command << "#{name}=#{value} " end command << arguments.join(' ') command << " #{.inspect}" unless .empty? warn ">>> #{command}" end # passing ENV variables or exec options is not supported before 1.9.1 if (!.empty? && ruby_1_8?) raise("cannot pass exec options to Kernel.system in #{RUBY_VERSION}") end if popen # IO.popen does not accept multiple arguments on Ruby 1.8.x. if ruby_1_8? raise("cannot use :popen on #{RUBY_VERSION}, please use 1.9.x") end # :popen can only be used on Unix, or on Windows with JRuby if (windows? && !jruby?) raise("cannot use :popen on Windows, unless under JRuby") end end # re-add ENV variables and exec options arguments.unshift(env) unless env.empty? arguments.push() unless .empty? if popen then IO.popen(arguments,popen) else Kernel.system(*arguments) end end |
.run(path, *arguments) ⇒ Boolean .run(path, *arguments, options) ⇒ Boolean
Runs a program under sudo.
315 316 317 318 319 320 321 |
# File 'lib/rprogram/system.rb', line 315 def self.sudo(*arguments) unless sudo? raise(ProgramNotFound,'could not find the "sudo" program') end return run(sudo_path,*arguments) end |
.sudo? ⇒ Boolean
Determines whether sudo
is available on the system.
277 278 279 |
# File 'lib/rprogram/system.rb', line 277 def self.sudo? !sudo_path.nil? end |
.sudo_path ⇒ Pathname?
The path to the sudo
program.
250 251 252 |
# File 'lib/rprogram/system.rb', line 250 def self.sudo_path @sudo ||= find_program('sudo') end |
.sudo_path=(path) ⇒ Pathanme
Sets the path to the sudo
program.
265 266 267 |
# File 'lib/rprogram/system.rb', line 265 def self.sudo_path=(path) @sudo = Pathname.new(path) end |
.windows? ⇒ Boolean
Determines if the platform is Windows.
53 54 55 56 57 58 59 |
# File 'lib/rprogram/system.rb', line 53 def self.windows? if @platform @platform.include?('mingw') || @platform.include?('mswin') else false end end |