From 710d43ee2eebeaead1bec53652b6028af6aa3f1d Mon Sep 17 00:00:00 2001 From: Kevin Bongart Date: Tue, 29 May 2018 23:21:07 -0400 Subject: [PATCH 1/4] Hash#each --- lib/middleman-imageoptim/options.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/middleman-imageoptim/options.rb b/lib/middleman-imageoptim/options.rb index c29995a..8ed484c 100644 --- a/lib/middleman-imageoptim/options.rb +++ b/lib/middleman-imageoptim/options.rb @@ -46,8 +46,8 @@ def symbolize_key(key) end def set_options(user_options) - OPTIONS.keys.each do |name| - instance_variable_set(:"@#{name}", user_options.fetch(name, OPTIONS[name])) + OPTIONS.each do |name, default_value| + instance_variable_set(:"@#{name}", user_options.fetch(name, default_value)) end end end From 1f1ae0cfb971c73d81e47a854743f42951bd7227 Mon Sep 17 00:00:00 2001 From: Kevin Bongart Date: Tue, 29 May 2018 23:21:28 -0400 Subject: [PATCH 2/4] Optional manifest_path config --- README.md | 1 + lib/middleman-imageoptim/manifest.rb | 8 +++++--- lib/middleman-imageoptim/optimizer.rb | 2 +- lib/middleman-imageoptim/options.rb | 7 +++++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f40eebd..dfc6219 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Below is the default configuration showing all available options; activate :imageoptim do |options| # Use a build manifest to prevent re-compressing images between builds options.manifest = true + options.manifest_path = 'build' # Silence problematic image_optim workers options.skip_missing_workers = true diff --git a/lib/middleman-imageoptim/manifest.rb b/lib/middleman-imageoptim/manifest.rb index 37c1bd5..d542675 100644 --- a/lib/middleman-imageoptim/manifest.rb +++ b/lib/middleman-imageoptim/manifest.rb @@ -3,14 +3,16 @@ module Imageoptim class Manifest MANIFEST_FILENAME = 'imageoptim.manifest.yml'.freeze - attr_reader :app + attr_reader :app, :path_option - def initialize(app) + def initialize(app, path_option = nil) @app = app + @path_option = path_option end def path - File.join(build_dir, MANIFEST_FILENAME) + base_path = path_option.present? ? path_option : build_dir + File.join(base_path, MANIFEST_FILENAME) end def build_dir diff --git a/lib/middleman-imageoptim/optimizer.rb b/lib/middleman-imageoptim/optimizer.rb index cda579d..25f1c96 100644 --- a/lib/middleman-imageoptim/optimizer.rb +++ b/lib/middleman-imageoptim/optimizer.rb @@ -100,7 +100,7 @@ def optimizer end def manifest - @manifest ||= Manifest.new(app) + @manifest ||= Manifest.new(app, options.manifest_path) end def update_bytes_saved(bytes) diff --git a/lib/middleman-imageoptim/options.rb b/lib/middleman-imageoptim/options.rb index 8ed484c..8818a47 100644 --- a/lib/middleman-imageoptim/options.rb +++ b/lib/middleman-imageoptim/options.rb @@ -6,8 +6,10 @@ class Options # Mapping of valid option names to default values EXTENSION_OPTIONS = [ :image_extensions, - :manifest + :manifest, + :manifest_path ].freeze + OPTIONS = { advpng: { level: 4 }, allow_lossy: false, @@ -15,8 +17,9 @@ class Options image_extensions: %w(.png .jpg .jpeg .gif .svg), jpegoptim: { strip: ['all'], max_quality: 100 }, jpegtran: { copy_chunks: false, progressive: true, jpegrescan: true }, - nice: true, manifest: true, + manifest_path: nil, + nice: true, optipng: { level: 6, interlace: false }, pack: true, pngcrush: { chunks: ['alla'], fix: false, brute: false }, From a2141866d1f5df56affa11a330969411e7916a9c Mon Sep 17 00:00:00 2001 From: Kevin Bongart Date: Wed, 30 May 2018 09:03:40 -0400 Subject: [PATCH 3/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dfc6219..3362f21 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Below is the default configuration showing all available options; activate :imageoptim do |options| # Use a build manifest to prevent re-compressing images between builds options.manifest = true - options.manifest_path = 'build' + options.manifest_path = './build/' # Silence problematic image_optim workers options.skip_missing_workers = true From c1e562bd5adeba04a348286fd1e79f49fa233be6 Mon Sep 17 00:00:00 2001 From: Kevin Bongart Date: Wed, 30 May 2018 09:42:08 -0400 Subject: [PATCH 4/4] Fix broken tests --- features/manifest.feature | 10 +++++++- features/support/step_definitions.rb | 8 +++--- .../basic-app/config-custom-manifest-path.rb | 3 +++ lib/middleman-imageoptim/manifest.rb | 25 +++++++++++++------ 4 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 fixtures/basic-app/config-custom-manifest-path.rb diff --git a/features/manifest.feature b/features/manifest.feature index e79cadc..b69805e 100644 --- a/features/manifest.feature +++ b/features/manifest.feature @@ -4,7 +4,15 @@ Feature: Manifest Given a successfully built app at "basic-app" with flags "--verbose" Then the following files should exist: | build/imageoptim.manifest.yml | - Then the manifest should have the right timestamp for "build/images/table.jpg" + Then the manifest at "build" should have the right timestamp for "build/images/table.jpg" + + Scenario: Save manifest at custom path + Given a fixture app "basic-app" + And app "basic-app" is using config "custom-manifest-path" + Given a successfully built app at "basic-app" with flags "--verbose" + Then the following files should exist: + | custom/manifest/path/imageoptim.manifest.yml | + Then the manifest at "custom/manifest/path" should have the right timestamp for "build/images/table.jpg" Scenario: Don't write a manifest file if it is disabled Given a fixture app "basic-app" diff --git a/features/support/step_definitions.rb b/features/support/step_definitions.rb index 487a264..d10aded 100644 --- a/features/support/step_definitions.rb +++ b/features/support/step_definitions.rb @@ -8,8 +8,8 @@ expect(File.size(File.join(current_dir, img))).to be < size.to_i end -Then(/^the manifest should have the right timestamp for "(.*?)"$/) do |file| - manifest = YAML.load(File.read(manifest_path)) +Then(/^the manifest at "(.*?)" should have the right timestamp for "(.*?)"$/) do |manifest_base_path, file| + manifest = YAML.load(File.read(manifest_path(manifest_base_path))) file_stamp = File.mtime(File.join(current_dir, file)) expect(manifest[file]).to eql(file_stamp) end @@ -40,6 +40,6 @@ expect(File.mtime(target)).not_to eql(@modification_times[target]) end -def manifest_path - File.join(current_dir, 'build', 'imageoptim.manifest.yml') +def manifest_path(manifest_base_path = 'build') + File.join(current_dir, manifest_base_path, 'imageoptim.manifest.yml') end diff --git a/fixtures/basic-app/config-custom-manifest-path.rb b/fixtures/basic-app/config-custom-manifest-path.rb new file mode 100644 index 0000000..d693275 --- /dev/null +++ b/fixtures/basic-app/config-custom-manifest-path.rb @@ -0,0 +1,3 @@ +activate :imageoptim do |options| + options.manifest_path = 'custom/manifest/path' +end diff --git a/lib/middleman-imageoptim/manifest.rb b/lib/middleman-imageoptim/manifest.rb index d542675..56b630d 100644 --- a/lib/middleman-imageoptim/manifest.rb +++ b/lib/middleman-imageoptim/manifest.rb @@ -11,8 +11,7 @@ def initialize(app, path_option = nil) end def path - base_path = path_option.present? ? path_option : build_dir - File.join(base_path, MANIFEST_FILENAME) + @path ||= File.join(base_path, MANIFEST_FILENAME) end def build_dir @@ -33,18 +32,28 @@ def resource(key) private + def base_path + if path_option.present? + path_option + else + build_dir + end + end + def resources - @resources ||= load(path) + @resources ||= begin + if File.exist?(path) + YAML.load_file(path) + else + {} + end + end end def dump(source) YAML.dump(source) end - def load(path) - YAML.load(File.read(path)) - end - def build(resources) resources.inject({}) do |new_manifest, resource| new_manifest[resource.to_s] = File.mtime(resource) @@ -53,6 +62,8 @@ def build(resources) end def write(manifest) + FileUtils.mkpath(base_path) unless File.exists?(path) + File.open(path, 'w') do |manifest_file| manifest_file.write(manifest) end