Class: Textus::Manifest::Entry::Publish::ToPaths

Inherits:
Mode
  • Object
show all
Defined in:
lib/textus/manifest/entry/publish/to_paths.rb

Overview

publish.to: render or copy the entry’s stored data to each fixed repo path. The behaviour of any entry that declares ‘publish: [{ to: … }, …]`. ADR 0094: iterates publish_targets (to-targets), rendering through a template when the target declares one, or copying verbatim otherwise.

Instance Attribute Summary

Attributes inherited from Mode

#entry

Instance Method Summary collapse

Methods inherited from Mode

#keyless?, #validate!

Constructor Details

#initialize(entry, publisher: Textus::Ports::Publisher.new) ⇒ ToPaths

Returns a new instance of ToPaths.



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

def initialize(entry, publisher: Textus::Ports::Publisher.new)
  super(entry)
  @publisher = publisher
end

Instance Method Details

#publish(pctx, prefix: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument,Metrics/AbcSize



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/textus/manifest/entry/publish/to_paths.rb', line 17

def publish(pctx, prefix: nil) # rubocop:disable Lint/UnusedMethodArgument,Metrics/AbcSize
  targets = entry.publish_targets.select(&:to_target?)
  return nil if targets.empty?

  data_path = pctx.manifest.resolver.resolve(entry.key).path
  envelope  = pctx.reader.call(entry.key)
  renderer  = Textus::Produce::Render.new(template_loader: ->(n) { pctx.read_template(n) })
  content = nil # parsed lazily; the data's `content` (always _meta-free)

  targets.each do |t|
    if t.renders?
      content ||= Textus::Entry.for_format(entry.format).parse(File.read(data_path), path: data_path)["content"]
      publish_bytes(render_bytes(t, content, renderer, pctx), entry.key, t, pctx, data_path, envelope)
    elsif strip_meta?(entry)
      content ||= Textus::Entry.for_format(entry.format).parse(File.read(data_path), path: data_path)["content"]
      bytes = Textus::Entry.for_format(entry.format).serialize(meta: {}, body: "", content: content)
      publish_bytes(bytes, entry.key, t, pctx, data_path, envelope)
    else
      # opaque / command / non-structured — publish the stored file as-is
      target_abs = File.join(pctx.repo_root, t.to)
      @publisher.publish(source: data_path, target: target_abs, store_root: pctx.root)
      pctx.emit(:entry_published, key: entry.key, envelope: envelope, source: data_path, target: target_abs)
    end
  end

  { kind: :built, value: { "key" => entry.key, "path" => data_path, "published_to" => targets.map(&:to) } }
end