Commit d454a549 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'add-pngquant-args' into 'master'

Convert pngquant tasks into shell script, support file arguments

See merge request gitlab-org/gitlab!69987
parents 2bbf199f ee294453
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'rails'
require 'png_quantizator'
require 'parallel'
require 'rainbow/ext/string'
require_relative '../tooling/lib/tooling/images'
return if Rails.env.production?
def usage
puts <<~EOF
Usage: #{$0} [compress|lint] [PATH..]
compress Compress all documentation PNG images using pngquant.
lint Checks that all documentation PNG images have been compressed with pngquant.
PATH One or more files or directories. If empty, `doc/**/*.png` is used.
EOF
end
command = ARGV.shift
paths = ARGV.presence || ['doc']
files = paths.flat_map do |path|
File.directory?(path) ? Dir.glob(File.join(path, '/**/*.png')) : path
end
case command
when 'compress'
puts "Compressing #{files.size} PNG files"
Parallel.each(files) do |file|
was_uncompressed, savings = Tooling::Image.compress_image(file)
if was_uncompressed
puts "#{file} was reduced by #{savings} bytes"
end
end
when 'lint'
puts "Checking #{files.size} PNG files"
uncompressed_files = Parallel.map(files) do |file|
is_uncompressed, _ = Tooling::Image.compress_image(file, true)
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/pngquant compress" and commit the result.'
)
abort
end
else
usage
end
......@@ -1148,7 +1148,7 @@ known tool is [`pngquant`](https://pngquant.org/), which is cross-platform and
open source. Install it by visiting the official website and following the
instructions for your OS.
GitLab has a [Rake task](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/tasks/pngquant.rake)
GitLab has a [Ruby script](https://gitlab.com/gitlab-org/gitlab/-/blob/master/bin/pngquant)
that you can use to automate the process. In the root directory of your local
copy of `https://gitlab.com/gitlab-org/gitlab`, run in a terminal:
......@@ -1156,19 +1156,26 @@ copy of `https://gitlab.com/gitlab-org/gitlab`, run in a terminal:
been compressed:
```shell
bundle exec rake pngquant:lint
bin/pngquant lint
```
- Compress all documentation PNG images using `pngquant`:
```shell
bundle exec rake pngquant:compress
bin/pngquant compress
```
The only caveat is that the task runs on all images under `doc/`, not only the
ones you might have included in a merge request. In that case, you can run the
compress task and only commit the images that are relevant to your merge
request.
- Compress specific files:
```shell
bin/pngquant compress doc/user/img/award_emoji_select.png doc/user/img/markdown_logo.png
```
- Compress all PNG files in a specific directory:
```shell
bin/pngquant compress doc/user/img
```
## Videos
......
# frozen_string_literal: true
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
namespace :pngquant do
# Returns an array of all images eligible for compression
def doc_images
Dir.glob('doc/**/*.png', File::FNM_CASEFOLD)
end
desc 'GitLab | Pngquant | Compress all documentation PNG images using pngquant'
task :compress do
files = doc_images
puts "Compressing #{files.size} PNG files in doc/**"
Parallel.each(files) do |file|
was_uncompressed, savings = Tooling::Image.compress_image(file)
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
files = doc_images
puts "Checking #{files.size} PNG files in doc/**"
uncompressed_files = Parallel.map(files) do |file|
is_uncompressed, _ = Tooling::Image.compress_image(file, true)
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