|
10 | 10 | require 'rugged' |
11 | 11 | require 'redcarpet' |
12 | 12 | require 'redcarpet/compat' |
| 13 | +require 'parallel' |
13 | 14 | require 'thread' |
14 | 15 |
|
15 | 16 | # Markdown expects the old redcarpet compat API, so let's tell it what |
@@ -135,68 +136,34 @@ def generate_docs(options) |
135 | 136 | versions = vers |
136 | 137 | end |
137 | 138 |
|
138 | | - nversions = versions.size |
139 | | - output = Queue.new |
140 | | - pipes = {} |
141 | | - versions.each do |version| |
142 | | - # We don't need to worry about joining since this process is |
143 | | - # going to die immediately |
144 | | - read, write = IO.pipe |
145 | | - pid = Process.fork do |
146 | | - read.close |
147 | | - |
148 | | - data = generate_doc_for(version) |
149 | | - examples = format_examples!(data, version) |
150 | | - |
151 | | - Marshal.dump([version, data, examples], write) |
152 | | - write.close |
153 | | - end |
154 | | - |
155 | | - pipes[pid] = read |
156 | | - write.close |
157 | | - end |
158 | | - |
159 | | - print "Generating documentation [0/#{nversions}]\r" |
160 | 139 | head_data = nil |
161 | | - |
162 | | - # This may seem odd, but we need to keep reading from the pipe or |
163 | | - # the buffer will fill and they'll block and never exit. Therefore |
164 | | - # we can't rely on Process.wait to tell us when the work is |
165 | | - # done. Instead read from all the pipes concurrently and send the |
166 | | - # ruby objects through the queue. |
167 | | - Thread.abort_on_exception = true |
168 | | - pipes.each do |pid, read| |
169 | | - Thread.new do |
170 | | - result = read.read |
171 | | - output << Marshal.load(result) |
172 | | - end |
173 | | - end |
174 | | - |
175 | | - for i in 1..nversions |
176 | | - version, data, examples = output.pop |
177 | | - |
| 140 | + nversions = versions.count |
| 141 | + Parallel.each_with_index(versions, finish: -> (version, index, result) do |
| 142 | + version, data, examples = result |
178 | 143 | # There's still some work we need to do serially |
179 | 144 | tally_sigs!(version, data) |
180 | 145 | force_utf8(data) |
181 | 146 | sha = @repo.write(data.to_json, :blob) |
182 | 147 |
|
183 | | - print "Generating documentation [#{i}/#{nversions}]\r" |
| 148 | + puts "Adding documentation for #{version} [#{index}/#{nversions}]" |
184 | 149 |
|
185 | 150 | # Store it so we can show it at the end |
186 | | - if version == 'HEAD' |
187 | | - head_data = data |
188 | | - end |
| 151 | + head_data = data if version == 'HEAD' |
189 | 152 |
|
190 | 153 | output_index.add(:path => "#{version}.json", :oid => sha, :mode => 0100644) |
191 | 154 | examples.each do |path, id| |
192 | 155 | output_index.add(:path => path, :oid => id, :mode => 0100644) |
193 | 156 | end |
| 157 | + end) do |version, index| |
| 158 | + puts "Generating documentation for #{version} [#{index}/#{nversions}]" |
| 159 | + data = generate_doc_for(version) |
| 160 | + examples = format_examples!(data, version) |
| 161 | + [version, data, examples] |
| 162 | + end |
194 | 163 |
|
195 | | - if head_data |
196 | | - puts '' |
197 | | - show_warnings(data) |
198 | | - end |
199 | | - |
| 164 | + if head_data |
| 165 | + puts '' |
| 166 | + show_warnings(head_data) |
200 | 167 | end |
201 | 168 |
|
202 | 169 | # We tally the signatures in the order they finished, which is |
|
0 commit comments