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

Class Method Summary collapse

Class Method Details

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

Build a command.

See the Definition class.



266
267
268
269
270
# File 'lib/sys_cmd.rb', line 266

def self.command(command, options = {}, &block)
  definition = Definition.new(command, options)
  definition.instance_eval &block if block
  Command.new definition.command
end

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



299
300
301
302
303
304
305
306
# File 'lib/sys_cmd.rb', line 299

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)



387
388
389
# File 'lib/sys_cmd.rb', line 387

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.



393
394
395
# File 'lib/sys_cmd.rb', line 393

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

.line_separator(options = {}) ⇒ Object



308
309
310
311
312
313
314
315
# File 'lib/sys_cmd.rb', line 308

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.



278
279
280
281
282
283
284
# File 'lib/sys_cmd.rb', line 278

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


381
382
383
384
# File 'lib/sys_cmd.rb', line 381

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



317
318
319
320
321
322
323
324
# File 'lib/sys_cmd.rb', line 317

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

.os_type(options = {}) ⇒ Object



295
296
297
# File 'lib/sys_cmd.rb', line 295

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

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

Build and run a command



273
274
275
# File 'lib/sys_cmd.rb', line 273

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