lib/mini_magick/tool.rb in mini_magick-4.13.2 vs lib/mini_magick/tool.rb in mini_magick-5.0.0
- old
+ new
@@ -1,18 +1,16 @@
require "mini_magick/shell"
module MiniMagick
##
- # Abstract class that wraps command-line tools. It shouldn't be used directly,
- # but through one of its subclasses (e.g. {MiniMagick::Tool::Mogrify}). Use
- # this class if you want to be closer to the metal and execute ImageMagick
- # commands directly, but still with a nice Ruby interface.
+ # Class that wraps command-line tools directly, as opposed MiniMagick::Image
+ # which is more high-level.
#
# @example
- # MiniMagick::Tool::Mogrify.new do |builder|
- # builder.resize "500x500"
- # builder << "path/to/image.jpg"
+ # MiniMagick.mogrify do |mogrify|
+ # mogrify.resize "500x500"
+ # mogrify << "path/to/image.jpg"
# end
#
class Tool
CREATION_OPERATORS = %w[xc canvas logo rose gradient radial-gradient plasma
@@ -21,12 +19,11 @@
##
# Aside from classic instantiation, it also accepts a block, and then
# executes the command in the end.
#
# @example
- # version = MiniMagick::Tool::Identify.new { |b| b.version }
- # puts version
+ # puts MiniMagick.identify(&:version)
#
# @return [MiniMagick::Tool, String] If no block is given, returns an
# instance of the tool, if block is given, returns the output of the
# command.
#
@@ -44,54 +41,50 @@
# @private
attr_reader :name, :args
# @param name [String]
# @param options [Hash]
- # @option options [Boolean] :whiny Whether to raise errors on non-zero
+ # @option options [Boolean] :errors Whether to raise errors on non-zero
# exit codes.
+ # @option options [Boolean] :warnings Whether to print warnings to stderrr.
+ # @option options [String] :stdin Content to send to standard input stream.
# @example
- # MiniMagick::Tool::Identify.new(whiny: false) do |identify|
+ # MiniMagick.identify(errors: false) do |identify|
# identify.help # returns exit status 1, which would otherwise throw an error
# end
- def initialize(name, options = {})
- warn "MiniMagick::Tool.new(false) is deprecated and will be removed in MiniMagick 5, use MiniMagick::Tool.new(whiny: false) instead." if !options.is_a?(Hash)
-
- @name = name
- @args = []
- @whiny = options.is_a?(Hash) ? options.fetch(:whiny, MiniMagick.whiny) : options
+ def initialize(name, **options)
+ @name = name
+ @args = []
+ @options = options
end
##
# Executes the command that has been built up.
#
# @example
- # mogrify = MiniMagick::Tool::Mogrify.new
+ # mogrify = MiniMagick.mogrify
# mogrify.resize("500x500")
# mogrify << "path/to/image.jpg"
# mogrify.call # executes `mogrify -resize 500x500 path/to/image.jpg`
#
# @example
- # mogrify = MiniMagick::Tool::Mogrify.new
+ # mogrify = MiniMagick.mogrify
# # build the command
# mogrify.call do |stdout, stderr, status|
# # ...
# end
#
# @yield [Array] Optionally yields stdout, stderr, and exit status
#
# @return [String] Returns the output of the command
#
- def call(*args)
- options = args[-1].is_a?(Hash) ? args.pop : {}
- warn "Passing whiny to MiniMagick::Tool#call is deprecated and will be removed in MiniMagick 5, use MiniMagick::Tool.new(whiny: false) instead." if args.any?
- whiny = args.fetch(0, @whiny)
+ def call(**options)
+ options = @options.merge(options)
+ options[:warnings] = false if block_given?
- options[:whiny] = whiny
- options[:stderr] = MiniMagick.warnings && !block_given?
-
shell = MiniMagick::Shell.new
- stdout, stderr, status = shell.run(command, options)
+ stdout, stderr, status = shell.run(command, **options)
yield stdout, stderr, status if block_given?
stdout.chomp("\n")
end
@@ -99,44 +92,39 @@
# The currently built-up command.
#
# @return [Array<String>]
#
# @example
- # mogrify = MiniMagick::Tool::Mogrify.new
+ # mogrify = MiniMagick.mogrify
# mogrify.resize "500x500"
# mogrify.contrast
# mogrify.command #=> ["mogrify", "-resize", "500x500", "-contrast"]
#
def command
[*executable, *args]
end
##
# The executable used for this tool. Respects
- # {MiniMagick::Configuration#cli}, {MiniMagick::Configuration#cli_path},
- # and {MiniMagick::Configuration#cli_prefix}.
+ # {MiniMagick::Configuration#cli_prefix}.
#
# @return [Array<String>]
#
# @example
- # MiniMagick.configure { |config| config.cli = :graphicsmagick }
- # identify = MiniMagick::Tool::Identify.new
- # identify.executable #=> ["gm", "identify"]
+ # identify = MiniMagick.identify
+ # identify.executable #=> ["magick", "identify"]
#
# @example
# MiniMagick.configure do |config|
- # config.cli = :graphicsmagick
# config.cli_prefix = ['firejail', '--force']
# end
- # identify = MiniMagick::Tool::Identify.new
- # identify.executable #=> ["firejail", "--force", "gm", "identify"]
+ # identify = MiniMagick.identify
+ # identify.executable #=> ["firejail", "--force", "magick", "identify"]
#
def executable
exe = [name]
exe.unshift "magick" if MiniMagick.imagemagick7? && name != "magick"
- exe.unshift "gm" if MiniMagick.graphicsmagick?
- exe.unshift File.join(MiniMagick.cli_path, exe.shift) if MiniMagick.cli_path
Array(MiniMagick.cli_prefix).reverse_each { |p| exe.unshift p } if MiniMagick.cli_prefix
exe
end
##
@@ -161,11 +149,11 @@
##
# Changes the last operator to its "plus" form.
#
# @example
- # MiniMagick::Tool::Mogrify.new do |mogrify|
+ # MiniMagick.mogrify do |mogrify|
# mogrify.antialias.+
# mogrify.distort.+("Perspective", "0,0,4,5 89,0,45,46")
# end
# # executes `mogrify +antialias +distort Perspective '0,0,4,5 89,0,45,46'`
#
@@ -179,11 +167,11 @@
##
# Create an ImageMagick stack in the command (surround.
#
# @example
- # MiniMagick::Tool::Magick.new do |convert|
+ # MiniMagick.convert do |convert|
# convert << "wand.gif"
# convert.stack do |stack|
# stack << "wand.gif"
# stack.rotate(30)
# end
@@ -206,11 +194,11 @@
##
# Adds ImageMagick's pseudo-filename `-` for standard input.
#
# @example
- # identify = MiniMagick::Tool::Identify.new
+ # identify = MiniMagick.identify
# identify.stdin
# identify.call(stdin: image_content)
# # executes `identify -` with the given standard input
#
def stdin
@@ -219,11 +207,11 @@
##
# Adds ImageMagick's pseudo-filename `-` for standard output.
#
# @example
- # content = MiniMagick::Tool::Magick.new do |convert|
+ # content = MiniMagick.convert do |convert|
# convert << "input.jpg"
# convert.auto_orient
# convert.stdout
# end
# # executes `convert input.jpg -auto-orient -` which returns file contents
@@ -271,37 +259,16 @@
self << option
self.merge!(args)
self
end
- def self.option_methods
- @option_methods ||= (
- tool = new(whiny: false)
- tool << "-help"
- help_page = tool.call(stderr: false)
-
- cli_options = help_page.scan(/^\s+-[a-z\-]+/).map(&:strip)
- if tool.name == "mogrify" && MiniMagick.graphicsmagick?
- # These options were undocumented before 2015-06-14 (see gm bug 302)
- cli_options |= %w[-box -convolve -gravity -linewidth -mattecolor -render -shave]
+ # deprecated tool subclasses
+ %w[animate compare composite conjure convert display identify import magick mogrify montage stream].each do |tool|
+ const_set(tool.capitalize, Class.new(self) {
+ define_method(:initialize) do |*args|
+ super(tool, *args)
end
-
- cli_options.map { |o| o[1..-1].tr('-','_') }
- )
+ })
+ deprecate_constant(tool.capitalize)
end
-
end
end
-
-require "mini_magick/tool/animate"
-require "mini_magick/tool/compare"
-require "mini_magick/tool/composite"
-require "mini_magick/tool/conjure"
-require "mini_magick/tool/convert"
-require "mini_magick/tool/display"
-require "mini_magick/tool/identify"
-require "mini_magick/tool/import"
-require "mini_magick/tool/magick"
-require "mini_magick/tool/mogrify"
-require "mini_magick/tool/mogrify_restricted"
-require "mini_magick/tool/montage"
-require "mini_magick/tool/stream"