geordi 0.15.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/optimize-png +52 -15
  2. data/lib/geordi/version.rb +1 -1
  3. metadata +2 -2
data/bin/optimize-png CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ require "ruby-debug"
3
+ require 'fileutils'
2
4
 
3
5
  # pngcrush -rem allb -reduce -brute original.png optimized.png
4
6
  # pngcrush -d target-dir/ *.png
@@ -17,6 +19,11 @@
17
19
  # An article explaining why removing gamma correction
18
20
  # http://hsivonen.iki.fi/png-gamma/
19
21
 
22
+ # Rails: file activesupport/lib/active_support/core_ext/string/starts_ends_with.rb, line 27
23
+ def ends_with?(string, suffix)
24
+ string[-suffix.length, suffix.length] == suffix
25
+ end
26
+
20
27
  def indented_puts(str="")
21
28
  puts " #{str}"
22
29
  end
@@ -27,15 +34,15 @@ def print_manual
27
34
  indented_puts
28
35
  indented_puts script_name
29
36
  indented_puts "=" *script_name.size
30
- indented_puts "- May reduce PNG file size lossless"
31
37
  indented_puts "- Removes color profiles: cHRM, sRGB, gAMA, ICC, etc."
32
38
  indented_puts "- Eliminates unused colors and reduce bit-depth (If possible)"
39
+ indented_puts "- May reduce PNG file size lossless"
33
40
  indented_puts
34
41
  indented_puts "Batch optimize all *.png in current directory:"
35
- indented_puts " #{script_name} output_directory"
42
+ indented_puts " #{script_name} directory"
36
43
  indented_puts
37
44
  indented_puts "Optimize single file:"
38
- indented_puts " #{script_name} input.png output.png"
45
+ indented_puts " #{script_name} input.png"
39
46
  indented_puts
40
47
  end
41
48
 
@@ -47,29 +54,59 @@ def optimization_default_args
47
54
  args
48
55
  end
49
56
 
50
- def batch_optimize(script_args)
51
- output_directory = script_args[0]
52
- match_pattern = "*.*" # be aware of case sensitivty: *.png wouldn't match IMAGE.PNG # pngcrush does nothing with other files than png
53
- exec "pngcrush -d #{output_directory} #{optimization_default_args} #{match_pattern}"
57
+ def optimize_file(input_file, output_file)
58
+ system "pngcrush #{optimization_default_args} '#{input_file}' '#{output_file}'"
54
59
  end
55
60
 
56
- def single_file_optimize(script_args)
57
- input_file = script_args[0]
58
- output_file = script_args[1]
59
- exec "pngcrush #{optimization_default_args} #{input_file} #{output_file}"
61
+ def unused_tempfile_path(original)
62
+ dirname = File.dirname(original)
63
+ basename = File.basename(original)
64
+ count = 0
65
+ begin
66
+ tmp_name = "#{dirname}/#{basename}_temp_#{count += 1}.png"
67
+ end while File.exists?(tmp_name)
68
+ tmp_name
69
+ end
70
+
71
+ def optimize_inplace(input_file)
72
+ temp_file = unused_tempfile_path(input_file)
73
+ result = optimize_file(input_file, temp_file)
74
+ if result.nil?
75
+ puts "Error was #{$?}"
76
+ else
77
+ FileUtils.rm(input_file)
78
+ FileUtils.mv("#{temp_file}", "#{input_file}")
79
+ end
80
+ end
81
+
82
+ def batch_optimize_inplace(path)
83
+ # Dir[".png"] works case sensitive, so to catch all funky .png extensions we have to go the following way:
84
+ png_relative_paths = []
85
+ Dir["#{path}/*.*"].each do |file_name|
86
+ png_relative_paths << file_name if ends_with?(File.basename(file_name.downcase), ".png")
87
+ end
88
+ png_relative_paths.each do |png_relative_path|
89
+ optimize_inplace(png_relative_path)
90
+ end
60
91
  end
61
92
 
62
93
  def main
63
94
  if ARGV.size == 1
64
- batch_optimize(ARGV)
65
- elsif ARGV.size == 2
66
- single_file_optimize(ARGV)
95
+ path = ARGV[0]
96
+ if File.directory?(path)
97
+ batch_optimize_inplace(path)
98
+ elsif File.file?(path)
99
+ optimize_inplace(path)
100
+ end
101
+ print_manual
102
+ indented_puts "*** Error: '#{path}' is neither a directory nor a file ***"
103
+ indented_puts
67
104
  else
68
105
  print_manual
69
106
  end
70
107
  end
71
108
 
72
- main
109
+ main()
73
110
 
74
111
 
75
112
 
@@ -1,3 +1,3 @@
1
1
  module Geordi
2
- VERSION = '0.15.0'
2
+ VERSION = '0.15.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geordi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.0
4
+ version: 0.15.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-19 00:00:00.000000000 Z
12
+ date: 2013-04-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Collection of command line tools we use in our daily work with Ruby,
15
15
  Rails and Linux at makandra.