Class: MiniMagick::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_magick/shell.rb

Overview

Sends commands to the shell (more precisely, it sends commands directly to the operating system).

Instance Method Summary collapse

Instance Method Details

#execute(command) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mini_magick/shell.rb', line 28

def execute(command)
  stdout, stderr, status =
    MiniMagick.logger.debug(command.join(" ")) do
      Timeout.timeout(MiniMagick.timeout) do
        send("execute_#{MiniMagick.shell_api.gsub("-", "_")}", *command)
      end
    end

  [stdout, stderr, status.exitstatus]
rescue Errno::ENOENT, IOError
  ["", "executable not found: \"#{command.first}\"", 127]
end

#execute_open3(*command) ⇒ Object



41
42
43
44
# File 'lib/mini_magick/shell.rb', line 41

def execute_open3(*command)
  require "open3"
  Open3.capture3(*command)
end

#execute_posix_spawn(*command) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/mini_magick/shell.rb', line 46

def execute_posix_spawn(*command)
  require "posix-spawn"
  pid, stdin, stdout, stderr = POSIX::Spawn.popen4(*command)
  Process.waitpid(pid)

  [stdout.read, stderr.read, $?]
end

#run(command, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mini_magick/shell.rb', line 13

def run(command, options = {})
  stdout, stderr, code = execute(command)

  case code
  when 1
    fail MiniMagick::Error, "`#{command.join(" ")}` failed with error:\n#{stderr}"
  when 127
    fail MiniMagick::Error, stderr
  end if options.fetch(:whiny, true)

  $stderr.print(stderr) unless options[:stderr] == false

  stdout
end