Class: Shr::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/shr/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, options = []) ⇒ Command

Returns a new instance of Command.



9
10
11
12
13
# File 'lib/shr/command.rb', line 9

def initialize(command, options=[])
  @command = command.to_s
  Option.indicator = '/' if OS.windows?
  @options = Option.translate(options).join(' ')
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



14
15
16
# File 'lib/shr/command.rb', line 14

def command
  @command
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/shr/command.rb', line 25

def exist?
  Which::exist? @command.chomp('!')
end

#release?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/shr/command.rb', line 29

def release?
  @command.end_with? '!'
end

#to_procObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/shr/command.rb', line 33

def to_proc
  Proc.new do |environment, command_out|
    if release?
      Open3.pipeline(self.to_s)
      [nil, nil]
    else
      io_r, io_w = IO.pipe
      options = { :out => io_w }
      options[:in] = command_out if command_out
      options.merge!(environment)

      pid = spawn(self.to_s, options)
      watcher = Process.detach(pid)
      io_w.close

      [io_r, watcher]
    end
  end
end

#to_sObject



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

def to_s
  [command, @options].join(' ')
end