summaryrefslogtreecommitdiff
path: root/lib/tasks/pngquant.rake
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tasks/pngquant.rake')
-rw-r--r--lib/tasks/pngquant.rake50
1 files changed, 3 insertions, 47 deletions
diff --git a/lib/tasks/pngquant.rake b/lib/tasks/pngquant.rake
index ceb4de55373..63bc1c7c16e 100644
--- a/lib/tasks/pngquant.rake
+++ b/lib/tasks/pngquant.rake
@@ -2,10 +2,10 @@ return if Rails.env.production?
require 'png_quantizator'
require 'parallel'
+require_relative '../../tooling/lib/tooling/images'
# The amount of variance (in bytes) allowed in
# file size when testing for compression size
-TOLERANCE = 10000
namespace :pngquant do
# Returns an array of all images eligible for compression
@@ -13,55 +13,13 @@ namespace :pngquant do
Dir.glob('doc/**/*.png', File::FNM_CASEFOLD)
end
- # Runs pngquant on an image and optionally
- # writes the result to disk
- def compress_image(file, overwrite_original)
- compressed_file = "#{file}.compressed"
- FileUtils.copy(file, compressed_file)
-
- pngquant_file = PngQuantizator::Image.new(compressed_file)
-
- # Run the image repeatedly through pngquant until
- # the change in file size is within TOLERANCE
- loop do
- before = File.size(compressed_file)
- pngquant_file.quantize!
- after = File.size(compressed_file)
- break if before - after <= TOLERANCE
- end
-
- savings = File.size(file) - File.size(compressed_file)
- is_uncompressed = savings > TOLERANCE
-
- if is_uncompressed && overwrite_original
- FileUtils.copy(compressed_file, file)
- end
-
- FileUtils.remove(compressed_file)
-
- [is_uncompressed, savings]
- end
-
- # Ensures pngquant is available and prints an error if not
- def check_executable
- unless system('pngquant --version', out: File::NULL)
- warn(
- 'Error: pngquant executable was not detected in the system.'.color(:red),
- 'Download pngquant at https://pngquant.org/ and place the executable in /usr/local/bin'.color(:green)
- )
- abort
- end
- end
-
desc 'GitLab | Pngquant | Compress all documentation PNG images using pngquant'
task :compress do
- check_executable
-
files = doc_images
puts "Compressing #{files.size} PNG files in doc/**"
Parallel.each(files) do |file|
- was_uncompressed, savings = compress_image(file, true)
+ was_uncompressed, savings = Tooling::Image.compress_image(file)
if was_uncompressed
puts "#{file} was reduced by #{savings} bytes"
@@ -71,13 +29,11 @@ namespace :pngquant do
desc 'GitLab | Pngquant | Checks that all documentation PNG images have been compressed with pngquant'
task :lint do
- check_executable
-
files = doc_images
puts "Checking #{files.size} PNG files in doc/**"
uncompressed_files = Parallel.map(files) do |file|
- is_uncompressed, _ = compress_image(file, false)
+ is_uncompressed, _ = Tooling::Image.compress_image(file, true)
if is_uncompressed
puts "Uncompressed file detected: ".color(:red) + file
file