Module: SysCmd
- Defined in:
- lib/sys_cmd.rb,
lib/sys_cmd/version.rb
Defined Under Namespace
Classes: Command, Definition, Shell
Constant Summary collapse
- OS_TYPE_SYNONIMS =
{ unix: :unix, windows: :windows, bash: :unix, linux: :unix, osx: :unix, cmd: :windows }
- VERSION =
"0.2.0"
Class Method Summary collapse
-
.command(command, options = {}, &block) ⇒ Object
Build a command.
- .escape(text, options = {}) ⇒ Object
-
.except_on(*os_types) ⇒ Object
Execute a block of code except on some system(s).
-
.execute(options = {}) ⇒ Object
Execute a block of code if the options
:only_on,:except_onare satisfied in the host system. - .line_separator(options = {}) ⇒ Object
-
.local_os_type ⇒ Object
Get the type of OS of the host.
-
.only_on(*os_types) ⇒ Object
Execute a block of code only on some systems.
- .option_switch(options = {}) ⇒ Object
- .os_type(options = {}) ⇒ Object
-
.run(command, options = {}, &block) ⇒ Object
Build and run a command.
- .split(text, options = {}) ⇒ Object
Class Method Details
.command(command, options = {}, &block) ⇒ Object
Build a command.
See the Definition class.
289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/sys_cmd.rb', line 289 def self.command(command, = {}, &block) definition = Definition.new(command, ) if block if block.arity == 1 block.call definition else definition.instance_eval &block end end Command.new definition end |
.escape(text, options = {}) ⇒ Object
328 329 330 331 332 333 334 335 |
# File 'lib/sys_cmd.rb', line 328 def self.escape(text, = {}) case os_type() when :windows '"' + text.gsub('"', '""') + '"' else Shellwords.shellescape(text) end end |
.except_on(*os_types) ⇒ Object
Execute a block of code except on some system(s)
440 441 442 |
# File 'lib/sys_cmd.rb', line 440 def self.except_on(*os_types) yield if Shell.new.applicable?(except_on: os_types) end |
.execute(options = {}) ⇒ Object
Execute a block of code if the options :only_on, :except_on are satisfied in the host system.
446 447 448 |
# File 'lib/sys_cmd.rb', line 446 def self.execute( = {}) yield if Shell.new.applicable?() end |
.line_separator(options = {}) ⇒ Object
357 358 359 360 361 362 363 364 |
# File 'lib/sys_cmd.rb', line 357 def self.line_separator( = {}) case os_type() when :windows '^\n' else '\\\n' end end |
.local_os_type ⇒ Object
Get the type of OS of the host.
307 308 309 310 311 312 313 |
# File 'lib/sys_cmd.rb', line 307 def self.local_os_type if OS.windows? :windows else :unix end end |
.only_on(*os_types) ⇒ Object
Execute a block of code only on some systems
cmd = nil
SysCmd.only_on :unix do
cmd = CmdSys.command('ls')
end
SysCmd.only_on :windows do
cmd = CmdSys.command('dir')
end
cmd.run
files = cmd.output
434 435 436 437 |
# File 'lib/sys_cmd.rb', line 434 def self.only_on(*os_types) return if os_types.empty? yield if Shell.new.applicable?(only_on: os_types) end |
.option_switch(options = {}) ⇒ Object
366 367 368 369 370 371 372 373 |
# File 'lib/sys_cmd.rb', line 366 def self.option_switch( = {}) case os_type() when :windows '/' else '-' end end |
.os_type(options = {}) ⇒ Object
324 325 326 |
# File 'lib/sys_cmd.rb', line 324 def self.os_type( = {}) [:os] || local_os_type end |
.run(command, options = {}, &block) ⇒ Object
Build and run a command
302 303 304 |
# File 'lib/sys_cmd.rb', line 302 def self.run(command, = {}, &block) command(command, , &block).run end |
.split(text, options = {}) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/sys_cmd.rb', line 337 def self.split(text, = {}) case os_type() when :windows words = [] field = '' line.scan(/\G\s*(?>([^\s\^\'\"]+)|'([^\']*)'|"((?:[^\"\^]|\\.)*)"|(\^.?)|(\S))(\s|\z)?/m) do |word, sq, dq, esc, garbage, sep| raise ArgumentError, "Unmatched double quote: #{line.inspect}" if garbage field << (word || sq || (dq || esc).gsub(/\^(.)/, '\\1')) if sep words << field field = '' end end words else Shellwords.shellsplit(text) end end |