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

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

Overview

publish.to: render or copy the entry's stored data to each fixed repo path.

Instance Attribute Summary

Attributes inherited from Mode

#entry

Instance Method Summary collapse

Methods inherited from Mode

#keyless?, #validate!

Constructor Details

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

Returns a new instance of ToPaths.



63
64
65
66
# File 'lib/textus/protocol/manifest/entry/publish.rb', line 63

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

Instance Method Details

#publish(pctx, prefix: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/textus/protocol/manifest/entry/publish.rb', line 68

def publish(pctx, prefix: nil)
  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(key: entry.key)
  content = nil

  targets.each do |t|
    if t.renders?
      content ||= Textus::Protocol::Format.for(entry.format).parse(File.read(data_path), path: data_path)["content"]
      publish_bytes(render_bytes(t, content, pctx), entry.key, t, pctx, data_path, envelope)
    elsif strip_meta?(entry)
      content ||= Textus::Protocol::Format.for(entry.format).parse(File.read(data_path), path: data_path)["content"]
      bytes = Textus::Protocol::Format.for(entry.format).serialize(meta: {}, body: "", content: content)
      publish_bytes(bytes, entry.key, t, pctx, data_path, envelope)
    else
      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