asset-image-opt 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/asset-image-opt.rb +39 -6
- data/lib/asset-image-opt/version.rb +1 -1
- metadata +1 -1
data/lib/asset-image-opt.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "asset-image-opt/version"
|
2
2
|
require 'piet'
|
3
|
+
require 'thread'
|
3
4
|
|
4
5
|
class AssetImageOpt
|
5
6
|
EXTENSIONS = %w{png jpeg jpg}
|
@@ -12,17 +13,48 @@ class AssetImageOpt
|
|
12
13
|
@initial_size = get_files_size
|
13
14
|
end
|
14
15
|
|
16
|
+
def threads_count
|
17
|
+
case RbConfig::CONFIG['host_os']
|
18
|
+
when /darwin9/
|
19
|
+
`hwprefs cpu_count`.to_i
|
20
|
+
when /darwin/
|
21
|
+
((`which hwprefs` != '') ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
|
22
|
+
when /linux/
|
23
|
+
`cat /proc/cpuinfo | grep processor | wc -l`.to_i
|
24
|
+
when /freebsd/
|
25
|
+
`sysctl -n hw.ncpu`.to_i
|
26
|
+
when /mswin|mingw/
|
27
|
+
require 'win32ole'
|
28
|
+
wmi = WIN32OLE.connect("winmgmts://")
|
29
|
+
cpu = wmi.ExecQuery("select NumberOfCores from Win32_Processor") # TODO count hyper-threaded in this
|
30
|
+
cpu.to_enum.first.NumberOfCores
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def optimize_files
|
35
|
+
queue = Queue.new
|
36
|
+
@files.each{|e| queue << e }
|
37
|
+
|
38
|
+
threads = []
|
39
|
+
threads_count.times do
|
40
|
+
threads << Thread.new do
|
41
|
+
while (e = queue.pop(true) rescue nil)
|
42
|
+
optimize_file(e)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
threads.each {|t| t.join }
|
48
|
+
puts ""
|
49
|
+
@optimized_size = get_files_size
|
50
|
+
end
|
51
|
+
|
15
52
|
def optimize
|
16
53
|
if @files.empty?
|
17
54
|
puts "No files to optimize"
|
18
55
|
else
|
19
56
|
puts "Optimizing images"
|
20
|
-
|
21
|
-
optimize_file(file)
|
22
|
-
print '.'
|
23
|
-
end
|
24
|
-
@optimized_size = get_files_size
|
25
|
-
puts ""
|
57
|
+
optimize_files
|
26
58
|
end
|
27
59
|
end
|
28
60
|
|
@@ -40,6 +72,7 @@ class AssetImageOpt
|
|
40
72
|
|
41
73
|
def optimize_file(file)
|
42
74
|
Piet.optimize(file)
|
75
|
+
print '.'
|
43
76
|
end
|
44
77
|
|
45
78
|
def get_files_size
|