class Asciibuild::Extensions::EvalBlock

Public Instance Methods

after_end(cmd, parent, lines, attrs) click to toggle source
# File lib/asciibuild/extensions.rb, line 197
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
      cid = rout.chomp
      wait_for_container cid
      parent.document.attributes["#{name} container"] = cid
    else
      Asciibuild::Extensions.failed = true
    end
    lines << "----" << "> #{docker_run}" << rout << rerr << "----"
  end
  create_open_block parent, lines, attrs
end
before_start(cmd, parent, attrs, lines, stderr_lines) click to toggle source
# File lib/asciibuild/extensions.rb, line 194
def before_start(cmd, parent, attrs, lines, stderr_lines)
end
process(parent, reader, attrs) click to toggle source
# File lib/asciibuild/extensions.rb, line 220
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} #{attrs['compose_opts']} #{dc_cmd}"
  when 'erlang'
    fname = write_file attrs, 'escript.erl', body
    "escript #{fname} #{attrs['escript_opts']}"
  when 'elixir'
    fname = write_file attrs, 'elixir.exs', body
    "elixir -r Logger -pa ebin -pa 'deps/*/ebin' #{attrs['elixir_opts']} #{fname}"
  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}"
    Asciibuild::Extensions.failed = 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