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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/asciibuild/extensions.rb', line 143

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



140
141
# File 'lib/asciibuild/extensions.rb', line 140

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

#process(parent, reader, attrs) ⇒ Object



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
  # 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
      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