sprockets-derailleur 0.0.3 → 0.0.5

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/README.md CHANGED
@@ -22,9 +22,9 @@ Require `sprockets-derailleur` in environment file:
22
22
 
23
23
  ## Usage
24
24
 
25
- Determine how many workers you want to use first. On OSX you can determine the number of physical CPUs this way:
25
+ Determine how many workers you want to use first. Determine the number of physical CPUs this way:
26
26
 
27
- processes = Integer `sysctl -n hw.physicalcpu 2>/dev/null` rescue 1
27
+ processes = SprocketsDerailleur::number_of_processors rescue 1
28
28
 
29
29
  Then initialize the manifest with the workers you just determined:
30
30
 
@@ -0,0 +1,118 @@
1
+ require "sprockets"
2
+
3
+ module Sprockets
4
+ class Manifest
5
+ attr_reader :workers
6
+
7
+ alias_method :old_initialize, :initialize
8
+ def initialize(environment, path, workers=1)
9
+ @workers = workers
10
+ old_initialize(environment, path)
11
+ end
12
+
13
+ alias_method :compile_with_workers, :compile
14
+ def compile(*args)
15
+ time = Benchmark.measure do
16
+ paths = environment.each_logical_path(*args).to_a +
17
+ args.flatten.select { |fn| Pathname.new(fn).absolute? if fn.is_a?(String)}
18
+
19
+ logger.warn "Initializing #{@workers} workers"
20
+
21
+ workers = []
22
+ @workers.times do
23
+ workers << worker(paths)
24
+ end
25
+
26
+ reads = workers.map{|worker| worker[:read]}
27
+ writes = workers.map{|worker| worker[:write]}
28
+
29
+ index = 0
30
+ finished = 0
31
+
32
+ loop do
33
+ break if finished >= paths.size
34
+
35
+ ready = IO.select(reads, writes)
36
+ ready[0].each do |readable|
37
+ data = Marshal.load(readable)
38
+ assets.merge! data["assets"]
39
+ files.merge! data["files"]
40
+ finished += 1
41
+ end
42
+
43
+ ready[1].each do |write|
44
+ break if index >= paths.size
45
+
46
+ Marshal.dump(index, write)
47
+ index += 1
48
+ end
49
+ end
50
+
51
+ logger.debug "Cleaning up workers"
52
+
53
+ workers.each do |worker|
54
+ worker[:read].close
55
+ worker[:write].close
56
+ end
57
+
58
+ workers.each do |worker|
59
+ Process.wait worker[:pid]
60
+ end
61
+
62
+ save
63
+ end
64
+
65
+ logger.warn "Completed compiling assets (#{(time.real * 100).round / 100.0}s)"
66
+ end
67
+
68
+ def worker(paths)
69
+ child_read, parent_write = IO.pipe
70
+ parent_read, child_write = IO.pipe
71
+
72
+ pid = fork do
73
+ begin
74
+ parent_write.close
75
+ parent_read.close
76
+
77
+ while !child_read.eof?
78
+ path = paths[Marshal.load(child_read)]
79
+
80
+ time = Benchmark.measure do
81
+ if asset = find_asset(path)
82
+ data = {'assets' => {}, 'files' => {}}
83
+
84
+ data['files'][asset.digest_path] = {
85
+ 'logical_path' => asset.logical_path,
86
+ 'mtime' => asset.mtime.iso8601,
87
+ 'size' => asset.bytesize,
88
+ 'digest' => asset.digest
89
+ }
90
+ data['assets'][asset.logical_path] = asset.digest_path
91
+
92
+ target = File.join(dir, asset.digest_path)
93
+
94
+ if File.exist?(target)
95
+ logger.debug "Skipping #{target}, already exists"
96
+ else
97
+ logger.debug "Writing #{target}"
98
+ asset.write_to target
99
+ end
100
+
101
+ Marshal.dump(data, child_write)
102
+ end
103
+ end
104
+ logger.warn "Compiled #{path} (#{(time.real * 1000).round}ms)"
105
+ end
106
+ ensure
107
+ child_read.close
108
+ child_write.close
109
+ end
110
+ end
111
+
112
+ child_read.close
113
+ child_write.close
114
+
115
+ {:read => parent_read, :write => parent_write, :pid => pid}
116
+ end
117
+ end
118
+ end
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Derailleur
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -1,119 +1,25 @@
1
1
  require "sprockets-derailleur/version"
2
- require "sprockets"
3
-
4
- module Sprockets
5
- class Manifest
6
- attr_reader :workers
7
-
8
- alias_method :old_initialize, :initialize
9
- def initialize(environment, path, workers=1)
10
- @workers = workers
11
- old_initialize(environment, path)
12
- end
13
-
14
- alias_method :compile_with_workers, :compile
15
- def compile(*args)
16
- time = Benchmark.measure do
17
- paths = environment.each_logical_path(*args).to_a +
18
- args.flatten.select { |fn| Pathname.new(fn).absolute? if fn.is_a?(String)}
19
-
20
- logger.warn "Initializing #{@workers} workers"
21
-
22
- workers = []
23
- @workers.times do
24
- workers << worker(paths)
25
- end
26
-
27
- reads = workers.map{|worker| worker[:read]}
28
- writes = workers.map{|worker| worker[:write]}
29
-
30
- index = 0
31
- finished = 0
32
-
33
- loop do
34
- break if finished >= paths.size
35
-
36
- ready = IO.select(reads, writes)
37
- ready[0].each do |readable|
38
- data = Marshal.load(readable)
39
- assets.merge! data["assets"]
40
- files.merge! data["files"]
41
- finished += 1
42
- end
43
-
44
- ready[1].each do |write|
45
- break if index >= paths.size
46
-
47
- Marshal.dump(index, write)
48
- index += 1
49
- end
50
- end
51
-
52
- logger.debug "Cleaning up workers"
53
-
54
- workers.each do |worker|
55
- worker[:read].close
56
- worker[:write].close
57
- end
58
-
59
- workers.each do |worker|
60
- Process.wait worker[:pid]
61
- end
62
-
63
- save
64
- end
65
-
66
- logger.warn "Completed compiling assets (#{(time.real * 100).round / 100.0}s)"
67
- end
68
-
69
- def worker(paths)
70
- child_read, parent_write = IO.pipe
71
- parent_read, child_write = IO.pipe
72
-
73
- pid = fork do
2
+ require "sprockets-derailleur/manifest"
3
+
4
+ module SprocketsDerailleur
5
+ def self.number_of_processors
6
+ if RUBY_PLATFORM =~ /linux/
7
+ return `cat /proc/cpuinfo | grep processor | wc -l`.to_i
8
+ elsif RUBY_PLATFORM =~ /darwin/
9
+ return `sysctl -n hw.physicalcpu`.to_i
10
+ elsif RUBY_PLATFORM =~ /win32/
11
+ # this works for windows 2000 or greater
12
+ require 'win32ole'
13
+ wmi = WIN32OLE.connect("winmgmts://")
14
+ wmi.ExecQuery("select * from Win32_ComputerSystem").each do |system|
74
15
  begin
75
- parent_write.close
76
- parent_read.close
77
-
78
- while !child_read.eof?
79
- path = paths[Marshal.load(child_read)]
80
-
81
- time = Benchmark.measure do
82
- if asset = find_asset(path)
83
- data = {'assets' => {}, 'files' => {}}
84
-
85
- data['files'][asset.digest_path] = {
86
- 'logical_path' => asset.logical_path,
87
- 'mtime' => asset.mtime.iso8601,
88
- 'size' => asset.bytesize,
89
- 'digest' => asset.digest
90
- }
91
- data['assets'][asset.logical_path] = asset.digest_path
92
-
93
- target = File.join(dir, asset.digest_path)
94
-
95
- if File.exist?(target)
96
- logger.debug "Skipping #{target}, already exists"
97
- else
98
- logger.debug "Writing #{target}"
99
- asset.write_to target
100
- end
101
-
102
- Marshal.dump(data, child_write)
103
- end
104
- end
105
- logger.warn "Compiled #{path} (#{(time.real * 1000).round}ms)"
106
- end
107
- ensure
108
- child_read.close
109
- child_write.close
16
+ processors = system.NumberOfLogicalProcessors
17
+ rescue
18
+ processors = 0
110
19
  end
20
+ return [system.NumberOfProcessors, processors].max
111
21
  end
112
-
113
- child_read.close
114
- child_write.close
115
-
116
- {:read => parent_read, :write => parent_write, :pid => pid}
117
22
  end
23
+ raise "can't determine 'number_of_processors' for '#{RUBY_PLATFORM}'"
118
24
  end
119
25
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprockets-derailleur
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Steel Fu
@@ -49,6 +49,7 @@ files:
49
49
  - README.md
50
50
  - Rakefile
51
51
  - lib/sprockets-derailleur.rb
52
+ - lib/sprockets-derailleur/manifest.rb
52
53
  - lib/sprockets-derailleur/version.rb
53
54
  - sprockets-derailleur.gemspec
54
55
  homepage: https://github.com/steel/sprockets-derailleur