164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# File 'lib/asciibuild/extensions.rb', line 164
def process parent, reader, attrs
lang = get_lang attrs[2]
doctitle = parent.document.attributes['doctitle']
if not attrs['title']
attrs['title'] = lang
end
attrs['original_title'] = attrs['title']
if not include_section?(parent, attrs)
if parent.document.attributes["error"]
puts "Section \"#{parent.title}\" skipped due to previous error."
attrs['title'] = 'icon:pause-circle[role=red] ' + attrs['title'] + " (previous error)"
else
sections = parent.document.attributes["sections"].split(/,[ ]*/)
puts "Section \"#{parent.title}\" skipped. Does not match #{sections}."
attrs['title'] = 'icon:pause-circle[role=yellow] ' + attrs['title']
end
lang = get_lang attrs[2]
return create_open_block parent, ["[source,#{lang}]", "----"] + reader.lines + ["----"], attrs
end
body = Mustache.render(reader.read, parent.document.attributes)
lines = []
stderr_lines = []
cmd = case attrs[2]
when 'Dockerfile'
if not attrs['image']
raise 'Missing image name. Add attribute of image={name} to the [asciibuild,Dockerfile] style.'
end
fname = attrs['file'] ||= 'Dockerfile'
write_file attrs, 'Dockerfile', body
"docker build -t #{attrs['image']} #{attrs['build_opts']} -f #{fname} ."
when 'erlang'
write_file attrs, 'escript.erl', body
"escript #{name} #{attrs['escript_opts']}"
when 'pyspark'
"pyspark #{attrs['spark_opts']}"
when 'spark-shell'
"spark-shell #{attrs['spark_opts']}"
when 'bash'
"bash -exs #{attrs['bash_opts']}"
else
attrs[2]
end
if attrs['container']
name = if attrs['container'] == 'true' then doctitle else attrs['container'] end
container_id = parent.document.attributes["#{name} container"]
cmd = "docker exec -i #{container_id} #{attrs['exec_opts']} #{cmd}"
end
lines = ["[source,#{lang}]", "----", body, "", "----"]
puts ""
puts "#{doctitle} > #{parent.title} > #{attrs['title']}"
puts (">" * 80)
before_start cmd, parent, attrs, lines, stderr_lines
lines << ".#{cmd}" << "----"
puts body
puts "> #{cmd}"
puts ""
status = 0
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
stdin << body << "\n"
stdin.close
while line=stdout.gets do
puts line
lines << line.chomp
end
while line=stderr.gets do
puts line
stderr_lines << line.chomp
end
status = wait_thr.value
end
lines << "----"
puts ("<" * 80)
if not stderr_lines.size == 0
lines << ".STDERR" << "----"
stderr_lines.each do |l| lines << l end
lines << "----"
end
if status != 0
lines << "IMPORTANT: #{cmd} failed with #{status}"
parent.document.attributes["error"] = true
attrs['title'] = 'icon:exclamation-circle[role=red] ' + attrs['title']
else
attrs['title'] = 'icon:check-circle[role=green] ' + attrs['title']
end
after_end cmd, parent, lines, attrs
end
|