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 =
"1.1.3"
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_on
are satisfied in the host system. - .here_doc(text, options = {}) ⇒ Object
- .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
- .requires_escaping?(text, options = {}) ⇒ Boolean
-
.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.
334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/sys_cmd.rb', line 334 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
373 374 375 376 377 378 379 380 |
# File 'lib/sys_cmd.rb', line 373 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)
510 511 512 |
# File 'lib/sys_cmd.rb', line 510 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.
516 517 518 |
# File 'lib/sys_cmd.rb', line 516 def self.execute( = {}) yield if Shell.new.applicable?() end |
.here_doc(text, options = {}) ⇒ Object
424 425 426 427 428 429 430 431 432 433 |
# File 'lib/sys_cmd.rb', line 424 def self.here_doc(text, = {}) case os_type() when :windows # only valid for PowerShell " @'\n#{text}\n'@\n" else delimiter = [:delimiter] || 'EOF' " << #{delimiter}\n#{text}\n#{delimiter}\n" end end |
.line_separator(options = {}) ⇒ Object
406 407 408 409 410 411 412 413 |
# File 'lib/sys_cmd.rb', line 406 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.
352 353 354 355 356 357 358 |
# File 'lib/sys_cmd.rb', line 352 def self.local_os_type if OS.windows? :windows else :unix end end |
.only_on(*os_types) ⇒ Object
504 505 506 507 |
# File 'lib/sys_cmd.rb', line 504 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
415 416 417 418 419 420 421 422 |
# File 'lib/sys_cmd.rb', line 415 def self.option_switch( = {}) case os_type() when :windows '/' else '-' end end |
.os_type(options = {}) ⇒ Object
369 370 371 |
# File 'lib/sys_cmd.rb', line 369 def self.os_type( = {}) [:os] || local_os_type end |
.requires_escaping?(text, options = {}) ⇒ Boolean
382 383 384 |
# File 'lib/sys_cmd.rb', line 382 def self.requires_escaping?(text, = {}) /[\s\"\']/ =~ text end |
.run(command, options = {}, &block) ⇒ Object
Build and run a command
347 348 349 |
# File 'lib/sys_cmd.rb', line 347 def self.run(command, = {}, &block) command(command, , &block).run end |
.split(text, options = {}) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/sys_cmd.rb', line 386 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 |