Module: Rails::Hyperdrive::CanonicalSkillRender

Defined in:
lib/rails/hyperdrive/canonical_skill_render.rb

Overview

Renders each SKILL.md.erb template in a companion gem repo to the static SKILL.md it pairs with, using the canonical fail-open binding. Companion-repo dev tooling: problems raise.

Defined Under Namespace

Classes: Rendered

Constant Summary collapse

Error =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.render_all(gemspec: nil, dir: Dir.pwd) ⇒ Object

Raises:



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
# File 'lib/rails/hyperdrive/canonical_skill_render.rb', line 31

def render_all(gemspec: nil, dir: Dir.pwd)
  spec_path = resolve_gemspec_path(gemspec, dir)
  spec = Gem::Specification.load(spec_path)
  raise Error, "could not load gemspec #{spec_path}" unless spec

  gem_root = File.dirname(File.expand_path(spec_path))
  skills_root = File.join(gem_root, (spec, "rails_hyperdrive_skills_dir"))
  templates_root = File.join(gem_root, (spec, "rails_hyperdrive_skill_templates_dir"))

  Dir.glob(File.join(templates_root, "**", "SKILL.md.erb")).sort.map do |template|
    rel = File.dirname(template).delete_prefix(templates_root).delete_prefix("/")
    dest_dir = File.join(skills_root, rel)
    # A SKILL.md written beside the SKILL.md.erb would take precedence
    # over it at discovery time, silently demoting the skill to its
    # static face.
    if File.expand_path(dest_dir) == File.expand_path(File.dirname(template))
      raise Error, "#{template}: content dir equals template dir; set " \
                   "spec.metadata[\"rails_hyperdrive_skills_dir\"] to a separate root"
    end

    body = render_template(template)
    validate_output!(body, template)
    Rendered.new(template: template, dest: File.join(dest_dir, "SKILL.md"), body: body)
  end
end

.stale(gemspec: nil, dir: Dir.pwd) ⇒ Object



25
26
27
28
29
# File 'lib/rails/hyperdrive/canonical_skill_render.rb', line 25

def stale(gemspec: nil, dir: Dir.pwd)
  render_all(gemspec: gemspec, dir: dir).reject do |r|
    File.file?(r.dest) && File.read(r.dest) == r.body
  end
end

.write(gemspec: nil, dir: Dir.pwd) ⇒ Object



18
19
20
21
22
23
# File 'lib/rails/hyperdrive/canonical_skill_render.rb', line 18

def write(gemspec: nil, dir: Dir.pwd)
  render_all(gemspec: gemspec, dir: dir).each do |r|
    FileUtils.mkdir_p(File.dirname(r.dest))
    File.write(r.dest, r.body)
  end
end