Class: Railstart::TemplateRunner
- Inherits:
-
Object
- Object
- Railstart::TemplateRunner
- Defined in:
- lib/railstart/template_runner.rb
Overview
Executes Rails application templates (including RailsBytes scripts) inside a generated application directory.
Wraps Rails' own AppGenerator so existing template DSL helpers such as
gem, initializer, route, etc. are available without reimplementing
them in Railstart.
Instance Method Summary collapse
-
#apply(source, variables: {}) ⇒ void
Apply a Rails template located at +source+.
- #assign_variables(generator, variables) ⇒ Object private
- #build_generator ⇒ Object private
- #default_generator_factory ⇒ Object private
- #generator_factory ⇒ Object private
-
#initialize(app_path:, generator_factory: nil, shell: Thor::Base.shell.new) ⇒ TemplateRunner
constructor
A new instance of TemplateRunner.
Constructor Details
#initialize(app_path:, generator_factory: nil, shell: Thor::Base.shell.new) ⇒ TemplateRunner
18 19 20 21 22 |
# File 'lib/railstart/template_runner.rb', line 18 def initialize(app_path:, generator_factory: nil, shell: Thor::Base.shell.new) @app_path = app_path @shell = shell @generator_factory = generator_factory end |
Instance Method Details
#apply(source, variables: {}) ⇒ void
This method returns an undefined value.
Apply a Rails template located at +source+.
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/railstart/template_runner.rb', line 31 def apply(source, variables: {}) raise TemplateError, "Template source must be provided" if source.to_s.strip.empty? generator = build_generator assign_variables(generator, variables) generator.apply(source) rescue TemplateError raise rescue LoadError => e raise TemplateError, "Rails must be installed to run template post-actions: #{e.}" rescue StandardError => e raise TemplateError, "Failed to apply template #{source}: #{e.}" end |
#assign_variables(generator, variables) ⇒ Object (private)
47 48 49 50 51 |
# File 'lib/railstart/template_runner.rb', line 47 def assign_variables(generator, variables) Array(variables).each do |key, value| generator.instance_variable_set(:"@#{key}", value) end end |
#build_generator ⇒ Object (private)
53 54 55 56 57 58 59 60 61 |
# File 'lib/railstart/template_runner.rb', line 53 def build_generator generator = generator_factory.call(@app_path) if generator.respond_to?(:destination_root=) generator.destination_root = @app_path elsif generator.respond_to?(:destination_root) generator.instance_variable_set(:@destination_root, @app_path) end generator end |
#default_generator_factory ⇒ Object (private)
67 68 69 70 71 72 73 74 75 |
# File 'lib/railstart/template_runner.rb', line 67 def default_generator_factory require "rails/generators" require "rails/generators/rails/app/app_generator" shell = @shell lambda do |_app_path| Rails::Generators::AppGenerator.new([], {}, shell: shell) end end |
#generator_factory ⇒ Object (private)
63 64 65 |
# File 'lib/railstart/template_runner.rb', line 63 def generator_factory @generator_factory ||= default_generator_factory end |