Class: SystemCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/system_command.rb,
lib/system_command/version.rb

Overview

Allows you to run external process with syntax a little bit more polished than the ruby built-ins.

Turn an external process into a class:

git = SystemCommand.new('git')
git.run('status')

Constant Summary collapse

Error =
Class.new(StandardError)
VERSION =
'2.0.2'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, *options) ⇒ SystemCommand

Returns a new instance of SystemCommand.



82
83
84
85
# File 'lib/system_command.rb', line 82

def initialize(path, *options)
  @path = path
  @options = options
end

Class Attribute Details

.verbose=(value) ⇒ Object (writeonly)

Sets the attribute verbose

Parameters:

  • value

    the value to set the attribute verbose to.



21
22
23
# File 'lib/system_command.rb', line 21

def verbose=(value)
  @verbose = value
end

Class Method Details

.each_line(chomp: false) ⇒ Object

Raises:

  • (LocalJumpError)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/system_command.rb', line 52

def each_line(*, chomp: false, **, &)
  argv0, *argv = coerce_argv(*)
  raise LocalJumpError, 'no block given' unless block_given?

  puts_command argv0, argv
  IO.popen([argv0, *argv], **) do |io|
    io.each_line(chomp:, &)
  end
  raise SystemCommand::Error, "#{argv0} failed (#{Process.last_status})" unless Process.last_status.success?

  nil
end

.getsObject



40
41
42
43
44
45
46
47
48
# File 'lib/system_command.rb', line 40

def gets(*, **)
  argv0, *argv = coerce_argv(*)

  puts_command argv0, argv
  result = IO.popen([argv0, *argv], **, &:read)
  raise SystemCommand::Error, "#{argv0} failed (#{Process.last_status})" unless Process.last_status.success?

  result
end

.lines(chomp: false) ⇒ Object



50
# File 'lib/system_command.rb', line 50

def lines(*, chomp: false, **) = gets(*, **).lines(chomp:)

.runObject



30
31
32
33
34
35
36
37
38
# File 'lib/system_command.rb', line 30

def run(*, **)
  argv0, *argv = coerce_argv(*)

  puts_command argv0, argv
  system([argv0, argv0], *argv, **)
  raise SystemCommand::Error, "#{argv0} failed (#{Process.last_status})" unless Process.last_status.success?

  nil
end

.run?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/system_command.rb', line 23

def run?(*, **)
  argv0, *argv = coerce_argv(*)

  puts_command argv0, argv
  system([argv0, argv0], *argv, **)
end

.verbose?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/system_command.rb', line 17

def verbose?
  @verbose ||= false
end

Instance Method Details

#each_lineObject



91
# File 'lib/system_command.rb', line 91

def each_line(...) = SystemCommand.each_line(@path, *@options, ...)

#getsObject



89
# File 'lib/system_command.rb', line 89

def gets(...) = SystemCommand.gets(@path, *@options, ...)

#linesObject



90
# File 'lib/system_command.rb', line 90

def lines(...) = SystemCommand.lines(@path, *@options, ...)

#runObject



88
# File 'lib/system_command.rb', line 88

def run(...) = SystemCommand.run(@path, *@options, ...)

#run?Boolean

Returns:

  • (Boolean)


87
# File 'lib/system_command.rb', line 87

def run?(...) = SystemCommand.run?(@path, *@options, ...)