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.0.0"

Class Method Summary collapse

Class Method Details

.command(command, options = {}, &block) ⇒ Object

Build a command.

See the Definition class.



323
324
325
326
327
328
329
330
331
332
333
# File 'lib/sys_cmd.rb', line 323

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 = {}) ⇒ Object



362
363
364
365
366
367
368
369
# File 'lib/sys_cmd.rb', line 362

def self.escape(text, options = {})
  case os_type(options)
  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)



477
478
479
# File 'lib/sys_cmd.rb', line 477

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.



483
484
485
# File 'lib/sys_cmd.rb', line 483

def self.execute(options = {})
  yield if Shell.new.applicable?(options)
end

.line_separator(options = {}) ⇒ Object



391
392
393
394
395
396
397
398
# File 'lib/sys_cmd.rb', line 391

def self.line_separator(options = {})
  case os_type(options)
  when :windows
    '^\n'
  else
    '\\\n'
  end
end

.local_os_typeObject

Get the type of OS of the host.



341
342
343
344
345
346
347
# File 'lib/sys_cmd.rb', line 341

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


471
472
473
474
# File 'lib/sys_cmd.rb', line 471

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



400
401
402
403
404
405
406
407
# File 'lib/sys_cmd.rb', line 400

def self.option_switch(options = {})
  case os_type(options)
  when :windows
    '/'
  else
    '-'
  end
end

.os_type(options = {}) ⇒ Object



358
359
360
# File 'lib/sys_cmd.rb', line 358

def self.os_type(options = {})
  options[:os] || local_os_type
end

.run(command, options = {}, &block) ⇒ Object

Build and run a command



336
337
338
# File 'lib/sys_cmd.rb', line 336

def self.run(command, options = {}, &block)
  command(command, options, &block).run
end

.split(text, options = {}) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/sys_cmd.rb', line 371

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