# Generating html file from results using haml
require "haml"
require "tilt/haml"
require "pathname"
require 'fileutils'
module GoldenRose
module Generators
class HtmlFormat
def initialize(results, build_logs, output_path = nil, force_save = false)
@details = results
@build_logs = build_logs
@output_path = output_path || ""
@force_save = force_save || false
end
def output
prepare_output_paths
make_output_html("index.html", "index.haml")
generate_test_logs if @details
end
def render_partial(path)
full_path = "#{GoldenRose.root}/lib/golden_rose/templates/#{path}"
render_haml_partial(full_path)
end
def render_asset(file_name, type: :js)
asset_path =
case type.to_sym
when :js then "#{GoldenRose.root}/lib/golden_rose/templates/assets/javascript/"
when :css then "#{GoldenRose.root}/lib/golden_rose/templates/assets/styles/"
end
render_haml_partial(asset_path + file_name)
end
def render_subactivities(subactivities)
return unless subactivities
full_path = "#{GoldenRose.root}/lib/golden_rose/templates/_test_logs.haml"
partial = Tilt::HamlTemplate.new(full_path)
return unless subactivities.children
partial.render(self, activity_summaries: subactivities.children)
end
def generate_test_logs
return unless @details.testable_summaries
@details.testable_summaries.each do |testable_summary|
testable_summary_name = testable_summary.name
next unless testable_summary.items
testable_summary.items.each do |item|
next unless item[:subtests]
item[:subtests].each do |subtest|
next unless subtest[:activity_summaries]
locals = {
testable_summary_name: testable_summary_name, item: item, subtest: subtest,
activity_summaries: subtest[:activity_summaries].children
}
make_output_html("test_logs/#{subtest[:uuid]}.html", "test_logs/index.haml", locals)
end
end
end
end
private
def render_haml_partial(path)
partial = Tilt::HamlTemplate.new(path)
partial.render(self)
end
def prepare_output_paths
FileUtils.remove_dir(@output_path, true) if @force_save
FileUtils.remove_dir(@output_path + "/test_logs", true)
FileUtils.mkdir_p @output_path + "/test_logs"
end
def make_output_html(relative_file_path, relative_template, *locals)
file_path = Pathname(@output_path) + relative_file_path
file = File.new(file_path, "w+")
template_path = File.join(GoldenRose::root, "/lib/golden_rose/templates/#{relative_template}")
template = Tilt::HamlTemplate.new(template_path)
file.puts template.render(self, *locals)
file.close
end
end
end
end