Commit 720f7648 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'nfriend-add-pngquant-pipeline-check' into 'master'

Add `pngquant:compress` and `pngquant:lint` rake tasks

See merge request gitlab-org/gitlab!20738
parents b5d8f44b e3ea5a0a
...@@ -386,6 +386,10 @@ group :development, :test do ...@@ -386,6 +386,10 @@ group :development, :test do
gem 'simple_po_parser', '~> 1.1.2', require: false gem 'simple_po_parser', '~> 1.1.2', require: false
gem 'timecop', '~> 0.8.0' gem 'timecop', '~> 0.8.0'
gem 'png_quantizator', '~> 0.2.1', require: false
gem 'parallel', '~> 1.17.0', require: false
end end
# Gems required in omnibus-gitlab pipeline # Gems required in omnibus-gitlab pipeline
......
...@@ -734,6 +734,7 @@ GEM ...@@ -734,6 +734,7 @@ GEM
peek (1.1.0) peek (1.1.0)
railties (>= 4.0.0) railties (>= 4.0.0)
pg (1.1.4) pg (1.1.4)
png_quantizator (0.2.1)
po_to_json (1.0.1) po_to_json (1.0.1)
json (>= 1.6.0) json (>= 1.6.0)
premailer (1.11.1) premailer (1.11.1)
...@@ -1286,8 +1287,10 @@ DEPENDENCIES ...@@ -1286,8 +1287,10 @@ DEPENDENCIES
omniauth_crowd (~> 2.2.0) omniauth_crowd (~> 2.2.0)
omniauth_openid_connect (~> 0.3.3) omniauth_openid_connect (~> 0.3.3)
org-ruby (~> 0.9.12) org-ruby (~> 0.9.12)
parallel (~> 1.17.0)
peek (~> 1.1) peek (~> 1.1)
pg (~> 1.1) pg (~> 1.1)
png_quantizator (~> 0.2.1)
premailer-rails (~> 1.10.3) premailer-rails (~> 1.10.3)
prometheus-client-mmap (~> 0.9.10) prometheus-client-mmap (~> 0.9.10)
pry-byebug (~> 3.5.1) pry-byebug (~> 3.5.1)
......
return if Rails.env.production?
require 'png_quantizator'
require 'parallel'
# The amount of variance (in bytes) allowed in
# file size when testing for compression size
TOLERANCE = 10
namespace :pngquant do
# Returns an array of all images eligible for compression
def doc_images
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)
if was_uncompressed
puts "#{file} was reduced by #{savings} bytes"
end
end
end
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)
if is_uncompressed
puts "Uncompressed file detected: ".color(:red) + file
file
end
end.compact
if uncompressed_files.empty?
puts "All documentation images are optimally compressed!".color(:green)
else
warn(
"The #{uncompressed_files.size} image(s) above have not been optimally compressed using pngquant.".color(:red),
'Please run "bin/rake pngquant:compress" and commit the result.'
)
abort
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment