From 7a785b64e20340388f3c853ec16f1221a6928ff0 Mon Sep 17 00:00:00 2001 From: Dima Shevtsov Date: Tue, 21 May 2019 16:13:31 -0500 Subject: [PATCH] Add rake task for image optimization --- Gemfile | 7 +++++++ Gemfile.lock | 24 +++++++++++++++++++++++- Rakefile | 12 ++++++++++++ bin/image_optim | 29 +++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100755 bin/image_optim diff --git a/Gemfile b/Gemfile index af37ff5b9bf..a20264883bc 100644 --- a/Gemfile +++ b/Gemfile @@ -12,6 +12,11 @@ group :test do gem 'launchy' end +group :optimiztion do + gem 'image_optim' + gem 'image_optim_pack' +end + group :jekyll_plugins do gem 'jekyll-algolia', '~> 1.0' gem 'jekyll-redirect-from' @@ -20,3 +25,5 @@ group :jekyll_plugins do gem 'jekyll-relative-links' gem 'jekyll-optional-front-matter' end + +gem "pry", "~> 0.12.2" diff --git a/Gemfile.lock b/Gemfile.lock index 4952fa490ae..bd8736e1810 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -21,6 +21,7 @@ GEM algoliasearch (1.26.0) httpclient (~> 2.8, >= 2.8.3) json (>= 1.5.1) + coderay (1.1.2) colorator (1.1.0) colorize (0.8.1) concurrent-ruby (1.1.5) @@ -30,9 +31,11 @@ GEM ethon (0.12.0) ffi (>= 1.3.0) eventmachine (1.2.7) + exifr (1.3.6) ffi (1.10.0) filesize (0.2.0) forwardable-extended (2.6.0) + fspath (3.1.0) html-proofer (3.9.3) activesupport (>= 4.2, < 6.0) addressable (~> 2.3) @@ -46,6 +49,17 @@ GEM httpclient (2.8.3) i18n (0.9.5) concurrent-ruby (~> 1.0) + image_optim (0.26.3) + exifr (~> 1.2, >= 1.2.2) + fspath (~> 3.0) + image_size (>= 1.5, < 3) + in_threads (~> 1.3) + progress (~> 3.0, >= 3.0.1) + image_optim_pack (0.5.2.20190428) + fspath (>= 2.1, < 4) + image_optim (~> 0.19) + image_size (2.0.1) + in_threads (1.5.1) jekyll (3.8.5) addressable (~> 2.4) colorator (~> 1.0) @@ -92,6 +106,7 @@ GEM rb-inotify (~> 0.9, >= 0.9.7) ruby_dep (~> 1.2) mercenary (0.3.6) + method_source (0.9.2) mini_portile2 (2.3.0) minitest (5.11.3) nokogiri (1.8.5) @@ -99,7 +114,11 @@ GEM parallel (1.17.0) pathutil (0.16.2) forwardable-extended (~> 2.6) + progress (3.5.0) progressbar (1.10.0) + pry (0.12.2) + coderay (~> 1.1.0) + method_source (~> 0.9.0) public_suffix (3.0.3) rb-fsevent (0.10.3) rb-inotify (0.10.0) @@ -126,6 +145,8 @@ PLATFORMS DEPENDENCIES devdocs! html-proofer + image_optim + image_optim_pack jekyll jekyll-algolia (~> 1.0) jekyll-optional-front-matter @@ -134,6 +155,7 @@ DEPENDENCIES jekyll-sitemap jekyll-titles-from-headings launchy + pry (~> 0.12.2) BUNDLED WITH - 1.17.2 + 1.17.3 diff --git a/Rakefile b/Rakefile index 2ba8f224524..8ef15b0fe41 100644 --- a/Rakefile +++ b/Rakefile @@ -60,3 +60,15 @@ end desc 'Pull docs from external repositories' task init: %w[multirepo:init] + +desc 'Optimize images in modified files, or by path (rake image_optim path=path/to/dir/or/file).' +task :image_optim do + path = ENV['path'] + unless path + staged_files = `git ls-files --modified --others --exclude-standard`.split("\n") + # staged_md_files = staged_files.select { |file| File.extname(file) == '.md' } + abort 'Didn\'t find any modified files.'.blue if staged_files.empty? + path = staged_files.join(' ') + end + system "bin/image_optim --no-pngout --no-svgo --recursive #{path}" +end diff --git a/bin/image_optim b/bin/image_optim new file mode 100755 index 00000000000..a2624d763ac --- /dev/null +++ b/bin/image_optim @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'image_optim' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +require "pathname" +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", + Pathname.new(__FILE__).realpath) + +bundle_binstub = File.expand_path("../bundle", __FILE__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("image_optim", "image_optim")