Class: TheLocal::Generators::ProviderGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/the_local/provider_generator.rb

Overview

‘bin/rails g the_local:provider <gem_name>` — scaffolds the provider side of the_local into a gem: a Reference loader, a knowledge guide, and a Companion that registers the common command interface (info / install / worker) via TheLocal.register behind a soft `require “the_local”` guard.

The companion app side is ‘the_local:install`; this is its mirror for the gems that contribute locals. See PROVIDERS.md.

Constant Summary collapse

GEMFILE_LINE =
%(gem "the_local", github: "DYB-Development/the_local")
RAKEFILE_REQUIRE =
%(require "the_local/rake")

Instance Method Summary collapse

Instance Method Details

#add_to_gemfileObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/generators/the_local/provider_generator.rb', line 47

def add_to_gemfile
  return if gem_name == "the_local"

  gemfile = File.join(destination_root, "Gemfile")
  return unless File.exist?(gemfile)
  return if File.read(gemfile).include?(GEMFILE_LINE)

  append_to_file "Gemfile",
                 "\n# Optional companion: #{gem_name} registers its locals with the_local " \
                 "when present.\n# Registration is guarded, so #{gem_name} works standalone.\n#{GEMFILE_LINE}\n"
end

#build_agent_filesObject

Render the committed .md files now, so they land in the diff for review. Loading the companion registers this gem’s locals; reset first so only they are built, not anything else the process may have registered.



81
82
83
84
85
86
87
88
# File 'lib/generators/the_local/provider_generator.rb', line 81

def build_agent_files
  companion = File.join(destination_root, "lib", lib_path, "the_local.rb")
  return unless File.exist?(companion)

  TheLocal.reset!
  load companion
  TheLocal::Builder.new(registry: TheLocal.registry).call
end

#create_companionObject



43
44
45
# File 'lib/generators/the_local/provider_generator.rb', line 43

def create_companion
  template "the_local.rb.tt", "lib/#{lib_path}/the_local.rb"
end

#create_guideObject



39
40
41
# File 'lib/generators/the_local/provider_generator.rb', line 39

def create_guide
  template "guide.md.tt", "lib/#{lib_path}/reference/guide.md"
end

#create_referenceObject



35
36
37
# File 'lib/generators/the_local/provider_generator.rb', line 35

def create_reference
  template "reference.rb.tt", "lib/#{lib_path}/reference.rb"
end

#hook_build_task_into_rakefileObject



69
70
71
72
73
74
75
76
# File 'lib/generators/the_local/provider_generator.rb', line 69

def hook_build_task_into_rakefile
  return unless File.exist?(File.join(destination_root, "Rakefile"))
  return if File.read(File.join(destination_root, "Rakefile")).include?(RAKEFILE_REQUIRE)

  append_to_file "Rakefile",
                 "\n# Render #{gem_name}'s committed the_local agent files: `rake the_local:build`.\n" \
                 "require \"#{lib_path}\"\n#{RAKEFILE_REQUIRE}\n"
end

#relocate_to_gem_rootObject



31
32
33
# File 'lib/generators/the_local/provider_generator.rb', line 31

def relocate_to_gem_root
  self.destination_root = gem_root
end

#require_from_entrypointObject



59
60
61
62
63
64
65
66
67
# File 'lib/generators/the_local/provider_generator.rb', line 59

def require_from_entrypoint
  entrypoint = File.join("lib", "#{lib_path}.rb")
  return unless File.exist?(File.join(destination_root, entrypoint))
  return if File.read(File.join(destination_root, entrypoint)).include?(require_line)

  append_to_file entrypoint,
                 "\n# Register #{gem_name}'s locals when the_local is present (no-op otherwise).\n" \
                 "#{require_line}\n"
end