Class: Textus::Manifest::Entry::Nested

Inherits:
Base show all
Defined in:
lib/textus/manifest/entry/nested.rb

Constant Summary collapse

PUBLISH_EACH_VARS =
Validators::PublishEach::KNOWN_VARS
PUBLISH_EACH_VAR_RE =
Validators::PublishEach::VAR_RE
KIND =
:nested

Constants inherited from Textus::Manifest::Entry

REGISTRY

Instance Attribute Summary collapse

Attributes inherited from Base

#format, #key, #manifest, #owner, #path, #publish_to, #raw, #schema, #zone

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#derived?, #events, #in_generator_zone?, #in_proposal_zone?, #inject_boot, #intake?, #leaf?, #template, #zone_writers

Constructor Details

#initialize(index_filename: nil, publish_each: nil, **rest) ⇒ Nested

Returns a new instance of Nested.



12
13
14
15
16
# File 'lib/textus/manifest/entry/nested.rb', line 12

def initialize(index_filename: nil, publish_each: nil, **rest)
  super(**rest)
  @index_filename = index_filename
  @publish_each = publish_each
end

Instance Attribute Details

#index_filenameObject (readonly)

Returns the value of attribute index_filename.



10
11
12
# File 'lib/textus/manifest/entry/nested.rb', line 10

def index_filename
  @index_filename
end

#publish_eachObject (readonly)

Returns the value of attribute publish_each.



10
11
12
# File 'lib/textus/manifest/entry/nested.rb', line 10

def publish_each
  @publish_each
end

Class Method Details

.from_raw(common, raw) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/textus/manifest/entry/nested.rb', line 66

def self.from_raw(common, raw)
  new(
    index_filename: raw["index_filename"],
    publish_each: raw["publish_each"],
    **common,
  )
end

Instance Method Details

#nested?Boolean

Returns:

  • (Boolean)


18
# File 'lib/textus/manifest/entry/nested.rb', line 18

def nested? = true

#publish_target_for(full_key) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/textus/manifest/entry/nested.rb', line 20

def publish_target_for(full_key)
  return nil if @publish_each.nil?

  entry_segs = @key.split(".")
  key_segs = full_key.split(".")
  raise UsageError.new("key '#{full_key}' is not under entry '#{@key}'") unless key_segs[0, entry_segs.length] == entry_segs

  remaining = key_segs[entry_segs.length..] || []
  leaf = remaining.join("/")
  basename = remaining.last || ""
  ext = Textus::Entry.for_format(@format).extensions.first.to_s.sub(/^\./, "")

  vars = { "leaf" => leaf, "basename" => basename, "key" => full_key, "ext" => ext }
  @publish_each.gsub(PUBLISH_EACH_VAR_RE) { vars.fetch(::Regexp.last_match(1)) }
end

#publish_via(pctx, prefix: nil) ⇒ Object



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
# File 'lib/textus/manifest/entry/nested.rb', line 36

def publish_via(pctx, prefix: nil)
  return nil if @publish_each.nil?

  leaves = []
  @manifest.resolver.enumerate(prefix: @key).each do |row|
    next unless row[:manifest_entry].equal?(self)
    next if prefix && !row[:key].start_with?(prefix) && row[:key] != prefix

    target_rel = publish_target_for(row[:key])
    target_abs = File.expand_path(File.join(pctx.repo_root, target_rel))
    unless target_abs.start_with?(File.expand_path(pctx.repo_root) + File::SEPARATOR)
      raise Textus::PublishError.new(
        "entry '#{@key}': publish_each target '#{target_rel}' for key '#{row[:key]}' escapes repo root",
      )
    end

    Textus::Infra::Publisher.publish(source: row[:path], target: target_abs, store_root: pctx.root)
    pctx.emit.call(:file_published,
                   key: row[:key],
                   envelope: pctx.reader.call(row[:key]),
                   source: row[:path],
                   target: target_abs)
    leaves << { "key" => row[:key], "source" => row[:path], "target" => target_abs }
  end

  { kind: :leaves, value: leaves }
end