Class: Upkeep::HerbSupport::TemplateManifest

Inherits:
Object
  • Object
show all
Defined in:
lib/upkeep/herb/template_manifest.rb

Defined Under Namespace

Classes: Visitor

Constant Summary collapse

DEFAULT_PARSE_OPTIONS =
{
  strict: true,
  track_whitespace: true,
  render_nodes: true,
  action_view_helpers: true,
  transform_conditionals: true
}.freeze
EMPTY_ROOT_SHAPE =
{
  significant_children: 0,
  root_elements: 0,
  single_root: false,
  multi_root: false
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, parse:, root_shape:, frontend_tag_plan:, render_nodes:, helper_lowered_elements:) ⇒ TemplateManifest

Returns a new instance of TemplateManifest.



110
111
112
113
114
115
116
117
# File 'lib/upkeep/herb/template_manifest.rb', line 110

def initialize(path:, parse:, root_shape:, frontend_tag_plan:, render_nodes:, helper_lowered_elements:)
  @path = path
  @parse = parse
  @root_shape = root_shape
  @frontend_tag_plan = frontend_tag_plan
  @render_nodes = render_nodes
  @helper_lowered_elements = helper_lowered_elements
end

Instance Attribute Details

#frontend_tag_planObject (readonly)

Returns the value of attribute frontend_tag_plan.



24
25
26
# File 'lib/upkeep/herb/template_manifest.rb', line 24

def frontend_tag_plan
  @frontend_tag_plan
end

#helper_lowered_elementsObject (readonly)

Returns the value of attribute helper_lowered_elements.



24
25
26
# File 'lib/upkeep/herb/template_manifest.rb', line 24

def helper_lowered_elements
  @helper_lowered_elements
end

#parseObject (readonly)

Returns the value of attribute parse.



24
25
26
# File 'lib/upkeep/herb/template_manifest.rb', line 24

def parse
  @parse
end

#pathObject (readonly)

Returns the value of attribute path.



24
25
26
# File 'lib/upkeep/herb/template_manifest.rb', line 24

def path
  @path
end

#render_nodesObject (readonly)

Returns the value of attribute render_nodes.



24
25
26
# File 'lib/upkeep/herb/template_manifest.rb', line 24

def render_nodes
  @render_nodes
end

#root_shapeObject (readonly)

Returns the value of attribute root_shape.



24
25
26
# File 'lib/upkeep/herb/template_manifest.rb', line 24

def root_shape
  @root_shape
end

Class Method Details

.build(path:, source:, parse_options: DEFAULT_PARSE_OPTIONS) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/upkeep/herb/template_manifest.rb', line 26

def self.build(path:, source:, parse_options: DEFAULT_PARSE_OPTIONS)
  parse_result = ::Herb.parse(source, **parse_options)
  parse = parse_status(parse_result)
  visitor = Visitor.new(path: path, source: source)
  parse_result.value&.accept(visitor) if parse.fetch(:ok)

  new(
    path: path,
    parse: parse,
    root_shape: visitor.root_shape,
    frontend_tag_plan: visitor.frontend_tag_plan,
    render_nodes: visitor.render_nodes,
    helper_lowered_elements: visitor.helper_lowered_elements
  )
rescue StandardError => error
  new(
    path: path,
    parse: {
      ok: false,
      exception: error.class.name,
      message: error.message
    },
    root_shape: {},
    frontend_tag_plan: [],
    render_nodes: [],
    helper_lowered_elements: []
  )
end

.summary(manifests) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/upkeep/herb/template_manifest.rb', line 55

def self.summary(manifests)
  partials = manifests.select(&:partial?)

  {
    templates_scanned: manifests.size,
    strict_parse_failures: manifests.count { |manifest| !manifest.parse.fetch(:ok) },
    render_nodes: manifests.sum { |manifest| manifest.render_nodes.size },
    helper_lowered_elements: manifests.sum { |manifest| manifest.helper_lowered_elements.size },
    frontend_tag_targets: manifests.sum { |manifest| manifest.frontend_tag_plan.size },
    page_root_tags: manifests.sum { |manifest| manifest.frontend_tag_plan.count { |tag| tag.fetch(:kind) == "page_root" } },
    fragment_root_tags: manifests.sum { |manifest| manifest.frontend_tag_plan.count { |tag| tag.fetch(:kind) == "fragment_root" } },
    render_site_tags: manifests.sum { |manifest| manifest.frontend_tag_plan.count { |tag| tag.fetch(:kind) == "render_site" } },
    partials: partials.size,
    single_root_partials: partials.count { |manifest| manifest.root_shape.fetch(:single_root, false) },
    multi_root_partials: partials.count { |manifest| manifest.root_shape.fetch(:multi_root, false) }
  }
end

Instance Method Details

#fingerprintObject



130
131
132
# File 'lib/upkeep/herb/template_manifest.rb', line 130

def fingerprint
  @fingerprint ||= Digest::SHA256.hexdigest(to_h.inspect)[0, 16]
end

#partial?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/upkeep/herb/template_manifest.rb', line 134

def partial?
  File.basename(path).start_with?("_")
end

#to_hObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/upkeep/herb/template_manifest.rb', line 119

def to_h
  {
    path: path,
    parse: parse,
    root_shape: root_shape,
    frontend_tag_plan: frontend_tag_plan,
    render_nodes: render_nodes,
    helper_lowered_elements: helper_lowered_elements
  }
end