class Autoproj::Jenkins::TemplateRenderingContext

@api private

A rendering context for ERB templates

It ensures that the templates are restricted to use the parameters that are provided to them

Public Class Methods

new(template_path, allow_unused: false, indent: 0, **parameters) click to toggle source
# File lib/autoproj/jenkins/render_template.rb, line 12
def initialize(template_path, allow_unused: false, indent: 0, **parameters)
    @allow_unused = allow_unused
    @indent = indent
    @template_path = template_path
    @parameters = parameters
    @used_parameters = ::Set.new
end

Public Instance Methods

__verify() click to toggle source
# File lib/autoproj/jenkins/render_template.rb, line 20
def __verify
    return if @allow_unused

    unused_parameters = @parameters.keys.to_set -
        @used_parameters
    if !unused_parameters.empty?
        ::Kernel.raise ::Autoproj::Jenkins::UnusedTemplateParameters, "#{unused_parameters.size} unused parameters: #{unused_parameters.map(&:to_s).sort.join(", ")}"
    end
end
escape_to_groovy(content) click to toggle source
# File lib/autoproj/jenkins/render_template.rb, line 48
def escape_to_groovy(content)
    content.
        gsub("\n", "\\n").
        gsub("\"", "\\\"")
end
method_missing(m) click to toggle source
# File lib/autoproj/jenkins/render_template.rb, line 54
def method_missing(m)
    if @parameters.has_key?(m)
        result = @parameters.fetch(m)
        @used_parameters << m
        result
    else
        ::Kernel.raise ::Autoproj::Jenkins::UnknownTemplateParameter, "#{m} is not a known template parameter"
    end
end
read_and_escape_file(path) click to toggle source
# File lib/autoproj/jenkins/render_template.rb, line 43
def read_and_escape_file(path)
    file_data = (::Autoproj::Jenkins.template_path + path).read
    escape_to_groovy(file_data)
end
render_template(path, escape: false, indent: 0, **parameters) click to toggle source
# File lib/autoproj/jenkins/render_template.rb, line 30
def render_template(path, escape: false, indent: 0, **parameters)
    result = ::Autoproj::Jenkins.render_template(path, template_path: @template_path, indent: indent, **parameters)
    @used_parameters.merge(parameters.keys)
    if escape
        result = escape_to_groovy(result)
    end

    indent_string = " " * indent
    # We assume that the ERB tag for render_template is indented
    # properly
    result.split("\n").join("\n#{indent_string}")
end