Module: PmdTester::LiquidRenderer

Includes:
PmdTester
Included in:
LiquidProjectRenderer, SummaryReportBuilder
Defined in:
lib/pmdtester/builders/liquid_renderer.rb

Overview

A module to include in classes that use a Liquid template to generate content.

Constant Summary

Constants included from PmdTester

BASE, PATCH, PR_NUM_ENV_VAR, VERSION

Instance Method Summary collapse

Methods included from PmdTester

#logger, logger

Instance Method Details

#copy_resource(dir, to_root) ⇒ Object



35
36
37
38
39
# File 'lib/pmdtester/builders/liquid_renderer.rb', line 35

def copy_resource(dir, to_root)
  src = ResourceLocator.resource(dir)
  dest = "#{to_root}/#{dir}"
  FileUtils.copy_entry(src, dest)
end

#render_and_write(template_path, target_file, env) ⇒ Object



20
21
22
# File 'lib/pmdtester/builders/liquid_renderer.rb', line 20

def render_and_write(template_path, target_file, env)
  write_file(target_file, render_liquid(template_path, env))
end

#render_liquid(template_path, env) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/pmdtester/builders/liquid_renderer.rb', line 12

def render_liquid(template_path, env)
  to_render = File.read(ResourceLocator.resource(template_path))
  includes = Liquid::LocalFileSystem.new(ResourceLocator.resource('_includes'), '%s.html')
  Liquid::Template.file_system = includes
  template = Liquid::Template.parse(to_render, error_mode: :strict)
  template.render!(env, { strict_variables: true })
end

#write_file(target_file, contents) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/pmdtester/builders/liquid_renderer.rb', line 24

def write_file(target_file, contents)
  dir = File.dirname(target_file)
  FileUtils.mkdir_p(dir) unless File.directory?(dir)

  index = File.new(target_file, 'w')
  index&.puts contents # may be nil when stubbing
  logger&.info "Written #{target_file}"
ensure
  index&.close
end