module SysCmd
Constants
- OS_TYPE_SYNONIMS
- VERSION
Public Class Methods
command(command, options = {}, &block)
click to toggle source
Build a command.
See the Definition
class.
# File lib/sys_cmd.rb, line 334 def self.command(command, options = {}, &block) definition = Definition.new(command, options) if block if block.arity == 1 block.call definition else definition.instance_eval &block end end Command.new definition end
escape(text, options = {})
click to toggle source
# File lib/sys_cmd.rb, line 373 def self.escape(text, options = {}) case os_type(options) when :windows '"' + text.gsub('"', '""') + '"' else Shellwords.shellescape(text) end end
except_on(*os_types) { || ... }
click to toggle source
Execute a block of code except on some system(s)
# 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 = {}) { || ... }
click to toggle source
Execute a block of code if the options :only_on
, :except_on
are satisfied in the host system.
# File lib/sys_cmd.rb, line 516 def self.execute(options = {}) yield if Shell.new.applicable?(options) end
here_doc(text, options = {})
click to toggle source
# File lib/sys_cmd.rb, line 424 def self.here_doc(text, options = {}) case os_type(options) when :windows # only valid for PowerShell " @'\n#{text}\n'@\n" else delimiter = options[:delimiter] || 'EOF' " << #{delimiter}\n#{text}\n#{delimiter}\n" end end
line_separator(options = {})
click to toggle source
# File lib/sys_cmd.rb, line 406 def self.line_separator(options = {}) case os_type(options) when :windows '^\n' else '\\\n' end end
local_os_type()
click to toggle source
Get the type of OS of the host.
# File lib/sys_cmd.rb, line 352 def self.local_os_type if OS.windows? :windows else :unix end end
only_on(*os_types) { || ... }
click to toggle source
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
# 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 = {})
click to toggle source
# File lib/sys_cmd.rb, line 415 def self.option_switch(options = {}) case os_type(options) when :windows '/' else '-' end end
os_type(options = {})
click to toggle source
# File lib/sys_cmd.rb, line 369 def self.os_type(options = {}) options[:os] || local_os_type end
requires_escaping?(text, options = {})
click to toggle source
# File lib/sys_cmd.rb, line 382 def self.requires_escaping?(text, options = {}) /[\s\"\']/ =~ text end
run(command, options = {}, &block)
click to toggle source
Build and run a command
# File lib/sys_cmd.rb, line 347 def self.run(command, options = {}, &block) command(command, options, &block).run end
split(text, options = {})
click to toggle source
# File lib/sys_cmd.rb, line 386 def self.split(text, options = {}) case os_type(options) 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