Class: Asciibuild::Extensions::EvalBlock

Inherits:
Asciidoctor::Extensions::BlockProcessor
  • Object
show all
Defined in:
lib/asciibuild/extensions.rb

Instance Method Summary collapse

Instance Method Details

#after_end(cmd, parent, lines, attrs) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/asciibuild/extensions.rb', line 178

def after_end(cmd, parent, lines, attrs)
  if attrs[2] == 'Dockerfile' and attrs['run'] and not attrs['run'] == 'false'
    doctitle = parent.document.attributes['doctitle']
    cmd = if attrs['run'] == 'true' then '' else attrs['run'] end
    name = if attrs['original_title'] then attrs['original_title'] else doctitle end
    docker_run = "docker run -d -i --label asciibuild.name=\"#{doctitle}\" #{attrs['run_opts']} #{attrs['image']} #{cmd}"

    puts docker_run

    rout, rerr, status = Open3.capture3(docker_run)
    puts rout, rerr
    if status == 0
      parent.document.attributes["#{name} container"] = rout.chomp
    else
      parent.document.attributes["error"] = true
    end
    lines << "----" << "> #{docker_run}" << rout << rerr << "----"
  end
  create_open_block parent, lines, attrs
end

#before_start(cmd, parent, attrs, lines, stderr_lines) ⇒ Object



175
176
# File 'lib/asciibuild/extensions.rb', line 175

def before_start(cmd, parent, attrs, lines, stderr_lines)
end

#process(parent, reader, attrs) ⇒ Object



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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/asciibuild/extensions.rb', line 199

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) or parent.document.attributes["enabled"] == "false" or attrs["enabled"] == "false"
    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)"
    elsif parent.document.attributes["enabled"] == "false" or attrs["enabled"] == "false"
      puts "Section \"#{parent.title}\" not enabled."
    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 = if not attrs['template'] == 'false'
    Mustache.render(reader.read, parent.document.attributes)
  else
    reader.read
  end

  lines = []
  stderr_lines = []

  cmd = case attrs[2]
  when 'bash'
    "bash -exs #{attrs['bash_opts']}"
  when 'Dockerfile'
    if not attrs['image']
      raise 'Missing image name. Add attribute of image={name} to the [asciibuild,Dockerfile] style.'
    end
    fname = write_file attrs, 'Dockerfile', body
    "docker build -t #{attrs['image']} #{attrs['build_opts']} -f #{fname} ."
  when 'docker-compose'
    dc_cmd = attrs['command'] ||= 'build'
    fname = write_file attrs, 'docker-compose.yml', body
    "docker-compose -f #{fname} #{compose_opts} #{dc_cmd}"
  when 'erlang'
    fname = write_file attrs, 'escript.erl', body
    "escript #{fname} #{attrs['escript_opts']}"
  when 'Makefile'
    "make -f - #{attrs['make_opts']} #{attrs['target']}"
  when 'pyspark'
    "pyspark #{attrs['spark_opts']}"
  when 'spark-shell'
    "spark-shell #{attrs['spark_opts']}"
  else
    opts = attrs["#{attrs[2]}_opts"]
    "#{attrs[2]} #{opts}"
  end
  # Check to see if we run inside a container
  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
      STDERR.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, normalize(parent, attrs, lines), attrs
end