Class: ImageOptim::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/image_optim/runner.rb,
lib/image_optim/runner/glob_helpers.rb,
lib/image_optim/runner/option_parser.rb

Overview

Handling optimization using image_optim binary

Defined Under Namespace

Modules: GlobHelpers Classes: BenchmarkResults, OptionParser, Results

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Runner

Returns a new instance of Runner.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/image_optim/runner.rb', line 85

def initialize(options)
  options = HashHelpers.deep_symbolise_keys(options)
  @recursive = options.delete(:recursive)
  @progress = options.delete(:show_progress) != false
  @exclude_dir_globs, @exclude_file_globs = %w[dir file].map do |type|
    glob = options.delete(:"exclude_#{type}_glob") || '.*'
    GlobHelpers.expand_braces(glob)
  end

  # --benchmark
  @benchmark = options.delete(:benchmark)
  if @benchmark
    unless options[:threads].nil?
      warning '--benchmark ignores --threads'
      options[:threads] = 1 # for consistency
    end
    if options[:timeout]
      warning '--benchmark ignores --timeout'
    end
  end

  @image_optim = ImageOptim.new(options)
end

Instance Method Details

#run!(args) ⇒ Object

rubocop:disable Naming/PredicateMethod



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/image_optim/runner.rb', line 109

def run!(args) # rubocop:disable Naming/PredicateMethod
  to_optimize = find_to_optimize(args)
  unless to_optimize.empty?
    if @benchmark
      benchmark_results = BenchmarkResults.new
      benchmark_images(to_optimize).each do |_original, rows| # rubocop:disable Style/HashEachMethods
        benchmark_results.add(rows)
      end
      benchmark_results.print
    else
      results = Results.new

      optimize_images!(to_optimize).each do |original, optimized|
        results.add(original, optimized)
      end

      results.print
    end
  end

  !@warnings
end