summaryrefslogtreecommitdiff
path: root/scripts/docs_screenshots.rb
blob: a734540eb69ba06d6587e61669e21d33f7f6072e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env ruby

# frozen_string_literal: true

require 'png_quantizator'
require 'open3'
require 'parallel'
require_relative '../tooling/lib/tooling/image'

generator = ARGV[0]
milestone = ARGV[1]

unless generator
  warn('Error: missing generator, please supply one')
  abort
end

unless milestone
  warn('Error: missing milestone, please supply one')
  abort
end

def rename_image(file, milestone)
  path = File.dirname(file)
  basename = File.basename(file, ".*")
  final_name = File.join(path, "#{basename}_v#{milestone}.png")
  FileUtils.mv(file, final_name)
end

system('spring', 'rspec', generator)

files = []

Open3.popen3("git diff --name-only -- '*.png'") do |stdin, stdout, stderr, thread|
  files.concat stdout.read.chomp.split("\n")
end

Open3.popen3("git status --porcelain -- '*.png'") do |stdin, stdout, stderr, thread|
  files.concat stdout.read.chomp.split("?? ")
end

files.reject!(&:empty?)

if files.empty?
  puts "No file generated, did you select the right screenshot generator?"
else
  puts "Compressing newly generated screenshots"

  Parallel.each(files) do |file|
    file_path = File.join(Dir.pwd, file.to_s.strip)
    was_uncompressed, savings = Tooling::Image.compress_image(file_path)
    rename_image(file_path, milestone)

    if was_uncompressed
      puts "#{file} was reduced by #{savings} bytes."
    else
      puts "Skipping already compressed file: #{file}."
    end
  end
end