Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add disallow overwrite option #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/middleman-webp/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ def convert_images(paths, &_after_conversion)
paths.each do |p|
begin
dst = destination_path(p)
next dont_convert(dst) if @options.disallow_overwrite && File.exist?(dst)
exec_convert_tool(p, dst)
yield File.new(p), File.new(dst.to_s)
rescue StandardError => e
@builder.trigger :error, "Converting #{p} failed", "#{e.to_s}\nBacktrace:\n\t#{e.backtrace.join("\n\t")}"
@builder.trigger :error, "Converting #{p} failed", "#{e.to_s}\n"\
"Backtrace:\n\t#{e.backtrace.join("\n\t")}"
end
end
end
Expand All @@ -58,6 +60,10 @@ def reject_file(file)
File.unlink(file)
end

def dont_convert(file)
@builder.trigger :identical, "#{file} already exists, skipped"
end

def print_summary
savings = @original_size - @converted_size
@builder.trigger :webp, '', 'Total conversion savings: '\
Expand Down
2 changes: 2 additions & 0 deletions lib/middleman-webp/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class WebPExtension < Extension
'their source')
option(:run_before_build, false, 'Run before build and save .webp files in'\
' source dir')
option(:disallow_overwrite, false, 'Prevent existing .webp files with' \
' matching filenames from being overwritten')

def initialize(app, options_hash = {}, &block)
super
Expand Down
3 changes: 2 additions & 1 deletion lib/middleman-webp/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Middleman
module WebP
class Options
attr_reader :ignore, :verbose, :append_extension, :allow_skip,
:run_before_build
:run_before_build, :disallow_overwrite

def initialize(options = {})
@ignore = options[:ignore] || []
Expand All @@ -23,6 +23,7 @@ def initialize(options = {})
@append_extension = options[:append_extension] || false
@allow_skip = !(false == options[:allow_skip])
@run_before_build = options[:run_before_build] || false
@disallow_overwrite = !(false == options[:disallow_overwrite])
end

# Internal: Generate command line args for cwebp or gif2webp command
Expand Down