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.
@@ -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
- @files.each do |file|
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
@@ -1,7 +1,7 @@
1
1
  module Asset
2
2
  module Image
3
3
  module Opt
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asset-image-opt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: