sunny-crush 0.1 → 0.1.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.
- data/bin/crush +6 -22
- data/lib/crush.rb +41 -12
- metadata +1 -1
data/bin/crush
CHANGED
|
@@ -1,33 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env ruby -W0
|
|
2
2
|
|
|
3
|
-
require 'fileutils'
|
|
4
3
|
require File.dirname(__FILE__) + '/../lib/crush'
|
|
5
4
|
|
|
6
|
-
include FileUtils
|
|
7
|
-
include Crush
|
|
8
|
-
|
|
9
|
-
# handle args
|
|
10
5
|
dir = ARGV[0]
|
|
11
|
-
abort "Usage: #{File.basename($0)} dir" if dir.nil?
|
|
6
|
+
abort "Usage: #{File.basename($0)} dir" if dir.nil? or !File.directory?(dir)
|
|
12
7
|
|
|
13
|
-
|
|
14
|
-
files = []
|
|
15
|
-
cd(dir) do
|
|
16
|
-
search = File.join('**', '*')
|
|
17
|
-
files = Dir[search]
|
|
18
|
-
files.reject! { |f| File.directory?(f) }
|
|
19
|
-
end
|
|
20
|
-
files.map! { |f| File.expand_path(File.join(dir, f)) }
|
|
8
|
+
files = Crush.find_files(dir)
|
|
21
9
|
abort "No files found in #{dir}" if files.empty?
|
|
22
10
|
|
|
23
|
-
|
|
11
|
+
counter = 0
|
|
24
12
|
files.each do |file|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
pngcrush(file) or optipng(file)
|
|
28
|
-
when /\.jpe?g$/
|
|
29
|
-
jpegtran(file)
|
|
30
|
-
end
|
|
13
|
+
ok = Crush.crush(file)
|
|
14
|
+
counter += 1 if ok
|
|
31
15
|
end
|
|
32
|
-
|
|
16
|
+
puts "Crushed #{counter} files!"
|
|
33
17
|
|
data/lib/crush.rb
CHANGED
|
@@ -1,25 +1,54 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
|
+
# See http://yuiblog.com/blog/2008/11/14/imageopt-3/
|
|
2
3
|
|
|
3
4
|
require 'tempfile'
|
|
4
5
|
require 'fileutils'
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
def
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
class Crush
|
|
8
|
+
def self.crush(file, verbose = false)
|
|
9
|
+
case file
|
|
10
|
+
when /^\._|^\.DS_Store$/
|
|
11
|
+
FileUtils::rm(file)
|
|
12
|
+
when /\.png$/
|
|
13
|
+
pngcrush(file) or optipng(file)
|
|
14
|
+
when /\.jpe?g$/
|
|
15
|
+
jpegtran(file)
|
|
16
|
+
else
|
|
17
|
+
false
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.find_files(dir)
|
|
22
|
+
files = []
|
|
23
|
+
FileUtils::cd(dir) do
|
|
24
|
+
search = File.join('**', '*')
|
|
25
|
+
files = Dir[search]
|
|
26
|
+
end
|
|
27
|
+
files.map! { |f| File.expand_path(File.join(dir, f)) }
|
|
28
|
+
files.reject! { |f| File.directory?(f) }
|
|
12
29
|
end
|
|
13
30
|
|
|
14
|
-
def
|
|
15
|
-
|
|
31
|
+
def self.exec(cmd)
|
|
32
|
+
#puts cmd
|
|
33
|
+
`#{cmd}`
|
|
16
34
|
$?.exitstatus == 0
|
|
17
35
|
end
|
|
18
36
|
|
|
19
|
-
def
|
|
37
|
+
def self.pngcrush(old_file)
|
|
38
|
+
tmp_file = Tempfile.new('crush_pngcrush').path
|
|
39
|
+
ok = exec "pngcrush -rem alla -reduce -brute '#{old_file}' '#{tmp_file}'"
|
|
40
|
+
FileUtils::cp tmp_file, old_file if ok
|
|
41
|
+
ok
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.optipng(old_file)
|
|
45
|
+
exec "optipng '#{old_file}'"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def self.jpegtran(old_file)
|
|
20
49
|
tmp_file = Tempfile.new('crush_jpegtran').path
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
50
|
+
ok = exec "jpegtran -copy none -optimize '#{old_file}' > '#{tmp_file}'"
|
|
51
|
+
FileUtils::cp tmp_file, old_file if ok
|
|
52
|
+
ok
|
|
24
53
|
end
|
|
25
54
|
end
|