Skip to content

Commit

Permalink
Merge branch 'preresolve-bins'
Browse files Browse the repository at this point in the history
Conflicts:
	lib/image_optim.rb
  • Loading branch information
toy committed Sep 4, 2014
2 parents 36de1b5 + e31106a commit d67f4f1
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Style/CaseIndentation:
IndentWhenRelativeTo: end

Style/ClassLength:
Max: 120
Max: 150

Style/CyclomaticComplexity:
Max: 10
Expand Down
12 changes: 5 additions & 7 deletions lib/image_optim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,16 @@ def env_path
# Create hash with format mapped to list of workers sorted by run order
def create_workers_by_format(&options_proc)
by_format = {}
Worker.klasses.each do |klass|
next unless (options = options_proc[klass])
worker = klass.new(self, options)
workers = Worker.create_all(self, &options_proc)
Worker.resolve_all!(workers)
sorted = workers.sort_by.with_index{ |worker, i| [worker.run_order, i] }
sorted.each do |worker|
worker.image_formats.each do |format|
by_format[format] ||= []
by_format[format] << worker
end
end
by_format.each do |_format, workers|
sorted = workers.sort_by.with_index{ |worker, i| [worker.run_order, i] }
workers.replace(sorted)
end
by_format
end

# Run method for each item in list
Expand Down
13 changes: 13 additions & 0 deletions lib/image_optim/bin_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ def env_path
[dir, ENV['PATH'], VENDOR_PATH].compact.join(':')
end

# Collect resolving errors when running block over items of enumerable
def self.collect_errors(enumerable)
errors = []
enumerable.each do |item|
begin
yield item
rescue Error => e
errors << e
end
end
errors
end

private

def resolving(name)
Expand Down
41 changes: 39 additions & 2 deletions lib/image_optim/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ def option(name, default, type, description = nil, &proc)
option_definitions <<
OptionDefinition.new(name, default, type, description, &proc)
end

# Initialize all workers using options from calling options_proc with
# klass
def create_all(image_optim, &options_proc)
Worker.klasses.map do |klass|
next unless (options = options_proc[klass])
klass.new(image_optim, options)
end.compact
end

# Resolve all bins of all workers failing with one joint exception
def resolve_all!(workers)
errors = BinResolver.collect_errors(workers) do |worker|
worker.resolve_used_bins!
end
return if errors.empty?
fail BinResolver::Error, ['Bin resolving errors:', *errors].join("\n")
end
end

# Configure (raises on extra options)
Expand Down Expand Up @@ -80,6 +98,20 @@ def run_order
0
end

# List of bins used by worker
def used_bins
[self.class.bin_sym]
end

# Resolve used bins, raise exception mergin all messages
def resolve_used_bins!
errors = BinResolver.collect_errors(used_bins) do |bin|
@image_optim.resolve_bin!(bin)
end
return if errors.empty?
fail BinResolver::Error, wrap_resolver_error_message(errors.join(', '))
end

# Check if operation resulted in optimized file
def optimized?(src, dst)
dst.size? && dst.size < src.size
Expand All @@ -99,9 +131,14 @@ def assert_no_unknown_options!(options)
def resolve_bin!(bin)
@image_optim.resolve_bin!(bin)
rescue BinResolver::Error => e
raise e, wrap_resolver_error_message(e.message), e.backtrace
end

def wrap_resolver_error_message(message)
name = self.class.bin_sym
raise e, "#{name} worker: #{e.message}; please provide proper binary or "\
"disable this worker (`:#{name} => false`)", e.backtrace
"#{name} worker: #{message}; please provide proper binary or "\
"disable this worker (--no-#{name} argument or "\
"`:#{name} => false` through options)"
end

# Run command setting priority and hiding output
Expand Down
4 changes: 4 additions & 0 deletions lib/image_optim/worker/jhead.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def run_order
-10
end

def used_bins
[:jhead, :jpegtran]
end

def optimize(src, dst)
if (2..8).include?(EXIFR::JPEG.new(src.to_s).orientation.to_i)
src.copy(dst)
Expand Down
4 changes: 4 additions & 0 deletions lib/image_optim/worker/jpegtran.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Jpegtran < Worker
option(:jpegrescan, false, 'Use jpegtran through jpegrescan, '\
'ignore progressive option'){ |v| !!v }

def used_bins
jpegrescan ? [:jpegtran, :jpegrescan] : [:jpegtran]
end

def optimize(src, dst)
if jpegrescan
args = %W[#{src} #{dst}]
Expand Down
6 changes: 5 additions & 1 deletion spec/image_optim/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@
end

describe 'for_worker' do
Abc = Class.new(ImageOptim::Worker) do
Abc = Class.new do
def self.bin_sym
:abc
end

def image_formats
[]
end
Expand Down
7 changes: 3 additions & 4 deletions spec/image_optim/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
Worker = ImageOptim::Worker

describe 'optimize' do
it 'should raise NotImplementedError unless overriden' do
class Abc < ImageOptim::Worker; end

it 'should raise NotImplementedError' do
image_optim = ImageOptim.new
worker = Worker.new(image_optim, {})

expect do
Abc.new(image_optim, {}).optimize(double, double)
worker.optimize(double, double)
end.to raise_error NotImplementedError
end
end
Expand Down

0 comments on commit d67f4f1

Please sign in to comment.