lib/mini_magick/tool.rb in mini_magick-4.4.0 vs lib/mini_magick/tool.rb in mini_magick-4.5.0

- old
+ new

@@ -47,17 +47,19 @@ attr_reader :name, :args # @param whiny [Boolean] Whether to raise errors on exit codes different # than 0. # @example - # MiniMagick::Tool::Identify.new(false) do |identify| + # MiniMagick::Tool::Identify.new(whiny: false) do |identify| # identify.help # returns exit status 1, which would otherwise throw an error # end - def initialize(name, whiny = MiniMagick.whiny) + 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 - @whiny = whiny @args = [] + @whiny = options.is_a?(Hash) ? options.fetch(:whiny, MiniMagick.whiny) : options end ## # Executes the command that has been built up. # @@ -65,20 +67,34 @@ # mogrify = MiniMagick::Tool::Mogrify.new # mogrify.resize("500x500") # mogrify << "path/to/image.jpg" # mogrify.call # executes `mogrify -resize 500x500 path/to/image.jpg` # - # @param whiny [Boolean] Whether you want an error to be raised when - # ImageMagick returns an exit code of 1. You may want this because - # some ImageMagick's commands (`identify -help`) return exit code 1, - # even though no error happened. + # @example + # mogrify = MiniMagick::Tool::Mogrify.new + # # build the command + # mogrify.call do |stdout, stderr, status| + # # ... + # end # - # @return [String] Output of the command + # @yield [Array] Optionally yields stdout, stderr, and exit status # - def call(whiny = @whiny, options = {}) + # @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) + + options[:whiny] = whiny + options[:stderr] = false if block_given? + shell = MiniMagick::Shell.new - shell.run(command, options.merge(whiny: whiny)).strip + stdout, stderr, status = shell.run(command, options) + yield stdout, stderr, status if block_given? + + stdout.strip end ## # The currently built-up command. # @@ -170,10 +186,23 @@ yield self self << ")" end ## + # Adds ImageMagick's pseudo-filename `-` for standard input. + # + # @example + # identify = MiniMagick::Tool::Identify.new + # identify.stdin + # identify.call(stdin: image_content) + # # executes `identify -` with the given standard input + # + def stdin + self << "-" + end + + ## # Define creator operator methods # # mogrify = MiniMagick::Tool.new("mogrify") # mogrify.canvas("khaki") # mogrify.command.join(" ") #=> "mogrify canvas:khaki" @@ -214,12 +243,12 @@ true end def self.option_methods @option_methods ||= ( - tool = new + tool = new(whiny: false) tool << "-help" - help_page = tool.call(false, stderr: false) + 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]