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:, recovery:) ⇒ TemplateManifest

Returns a new instance of TemplateManifest.



164
165
166
167
168
169
170
171
172
# File 'lib/upkeep/herb/template_manifest.rb', line 164

def initialize(path:, parse:, root_shape:, frontend_tag_plan:, render_nodes:, helper_lowered_elements:, recovery:)
  @path = path
  @parse = parse
  @root_shape = root_shape
  @frontend_tag_plan = frontend_tag_plan
  @render_nodes = render_nodes
  @helper_lowered_elements = helper_lowered_elements
  @recovery = recovery
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

#recoveryObject (readonly)

Returns the value of attribute recovery.



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

def recovery
  @recovery
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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, mode: parse_mode(parse_options))

  if parse.fetch(:ok)
    visitor = Visitor.new(path: path, source: source)
    parse_result.value&.accept(visitor)

    return 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,
      recovery: nil
    )
  end

  parse, recovery = recovery_for(path: path, source: source, parse: parse, parse_options: parse_options)

  new(
    path: path,
    parse: parse,
    root_shape: EMPTY_ROOT_SHAPE,
    frontend_tag_plan: [],
    render_nodes: [],
    helper_lowered_elements: [],
    recovery: recovery
  )
rescue StandardError => error
  new(
    path: path,
    parse: {
      ok: false,
      mode: parse_mode(parse_options),
      exception: error.class.name,
      message: error.message,
      recovered: false
    },
    root_shape: EMPTY_ROOT_SHAPE,
    frontend_tag_plan: [],
    render_nodes: [],
    helper_lowered_elements: [],
    recovery: nil
  )
end

.summary(manifests) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/upkeep/herb/template_manifest.rb', line 74

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

  {
    templates_scanned: manifests.size,
    strict_parse_failures: manifests.count { |manifest| !manifest.parse.fetch(:ok) },
    recoverable_parse_failures: manifests.count(&:recovered?),
    recovered_render_nodes: manifests.sum { |manifest| manifest.recovery_render_nodes.size },
    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



186
187
188
# File 'lib/upkeep/herb/template_manifest.rb', line 186

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

#partial?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/upkeep/herb/template_manifest.rb', line 190

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

#recovered?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/upkeep/herb/template_manifest.rb', line 194

def recovered?
  parse.fetch(:recovered, false)
end

#recovery_frontend_tag_planObject



198
199
200
# File 'lib/upkeep/herb/template_manifest.rb', line 198

def recovery_frontend_tag_plan
  Array(recovery&.fetch(:frontend_tag_plan, []))
end

#recovery_render_nodesObject



202
203
204
# File 'lib/upkeep/herb/template_manifest.rb', line 202

def recovery_render_nodes
  Array(recovery&.fetch(:render_nodes, []))
end

#to_hObject



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/upkeep/herb/template_manifest.rb', line 174

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,
    recovery: recovery
  }.compact
end