Module: MiniMagick::Utilities

Defined in:
lib/mini_magick/utilities.rb

Class Method Summary collapse

Class Method Details

.escape(value) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/mini_magick/utilities.rb', line 27

def escape(value)
  if windows?
    windows_escape(value)
  else
    shell_escape(value)
  end
end

.path(path) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mini_magick/utilities.rb', line 49

def path(path)
  if windows?
    # For Windows, if a path contains space char, you need to quote it,
    # otherwise you SHOULD NOT quote it. If you quote a path that does
    # not contains space, it will not work.
    pathname = Pathname.new(path).to_s
    path.include?(' ') ? pathname.inspect : pathname
  else
    path
  end
end

.shell_escape(value) ⇒ Object



35
36
37
# File 'lib/mini_magick/utilities.rb', line 35

def shell_escape(value)
  Shellwords.escape(value)
end

.which(cmd) ⇒ Object

Cross-platform way of finding an executable in the $PATH.

which('ruby') #=> /usr/bin/ruby


11
12
13
14
15
16
17
18
19
20
# File 'lib/mini_magick/utilities.rb', line 11

def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable? exe
    end
  end
  nil
end

.windows?Boolean

Finds out if the host OS is windows

Returns:

  • (Boolean)


23
24
25
# File 'lib/mini_magick/utilities.rb', line 23

def windows?
  RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
end

.windows_escape(value) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/mini_magick/utilities.rb', line 39

def windows_escape(value)
  # For Windows, ^ is the escape char, equivalent to \ in Unix.
  escaped = value.gsub(/\^/, '^^').gsub(/>/, '^>')
  if escaped !~ /^".+"$/ && escaped.include?("'")
    escaped.inspect
  else
    escaped
  end
end