Class: Charming::Generators::AppGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/charming/generators/app_generator.rb

Overview

AppGenerator implements ‘charming new NAME`. Writes a complete Bundler-gem-style Charming app skeleton: Gemfile, Rakefile, gemspec, exe, lib root + application + version, config/routes.rb, app/state, app/controllers, app/views/layouts + home view, and a baseline spec/ tree. Optionally also creates the database files when `database:` is set.

Constant Summary collapse

BASE_FILE_TEMPLATES =

The list of [relative-path, template-path, executable-flag] triples to render for a non-database app.

[
  ["Gemfile", "app/Gemfile.template", false],
  ["Rakefile", "app/Rakefile.template", false],
  ["README.md", "app/README.md.template", false],
  ["%<name>s.gemspec", "app/gemspec.template", false],
  ["exe/%<name>s", "app/executable.template", true],
  ["lib/%<name>s.rb", "app/root_file.template", false],
  ["lib/%<name>s/application.rb", "app/application.template", false],
  ["lib/%<name>s/version.rb", "app/version.template", false],
  ["config/routes.rb", "app/routes.template", false],
  ["app/state/application_state.rb", "app/application_state.template", false],
  ["app/state/home_state.rb", "app/home_state.template", false],
  ["app/controllers/application_controller.rb", "app/application_controller.template", false],
  ["app/controllers/home_controller.rb", "app/home_controller.template", false],
  ["app/views/layouts/application_layout.rb", "app/layout.template", false],
  ["app/views/home/show_view.rb", "app/view.template", false],
  ["app/components/.keep", "app/keep.template", false],
  ["spec/spec_helper.rb", "app/spec_helper.template", false],
  ["spec/state/home_state_spec.rb", "app/spec_state.template", false],
  ["spec/controllers/home_controller_spec.rb", "app/spec_controller.template", false],
  ["spec/views/home/show_view_spec.rb", "app/spec_view.template", false]
].freeze
DATABASE_FILE_TEMPLATES =

The list of [relative-path, template-path, executable-flag] triples to render in addition to the base list when ‘database:` is set on the generator.

[
  ["config/database.rb", "app/database_config.template", false],
  ["app/models/application_record.rb", "app/application_record.template", false],
  ["db/migrate/.keep", "app/keep.template", false],
  ["db/seeds.rb", "app/seeds.template", false]
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(name, out:, destination:, force: false, database: nil) ⇒ AppGenerator

name is the new app’s name. out is the status stream. destination is the parent directory under which ‘<name>/` will be created. force allows overwriting existing files. database optionally enables the database template set.



48
49
50
51
52
# File 'lib/charming/generators/app_generator.rb', line 48

def initialize(name, out:, destination:, force: false, database: nil)
  super(out: out, destination: File.join(destination, name), force: force)
  @name = Name.new(name)
  @database = database
end

Instance Method Details

#generateObject

Renders every template in the chosen template list (base + optional database) and writes the files, then initializes a git repository in the new app directory.



56
57
58
59
60
61
# File 'lib/charming/generators/app_generator.rb', line 56

def generate
  file_templates.each do |path, template_path, executable|
    create_file(file_path(path), render_app_template(template_path), executable: executable)
  end
  initialize_git_repository
end