123456789_123456789_123456789_123456789_123456789_

Class: Rails::Generators::PluginGenerator

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, AppBase, Base, Thor::Group
Instance Chain:
self, AppBase, AppName, Database, Base, Actions, Thor::Actions, Thor::Group
Inherits: Rails::Generators::AppBase
Defined in: railties/lib/rails/generators/rails/plugin/plugin_generator.rb

Constant Summary

Database - Included

DATABASES, JDBC_DATABASES

AppName - Included

RESERVED_NAMES

AppBase - Inherited

BUN_VERSION, NODE_LTS_VERSION, OPTION_IMPLICATIONS

Class Attribute Summary

Base - Inherited

Class Method Summary

AppBase - Inherited

Base - Inherited

.base_root

Returns the base root for a common set of generators.

.default_source_root

Returns the default source root for a given generator.

.desc

Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description.

.hide!

Convenience method to hide this generator from the available ones when running rails generator command.

.hook_for

Invoke a generator based on the value supplied by the user to the given option named “name”.

.namespace

Convenience method to get the namespace from the class name.

.remove_hook_for

Remove a previously added hook.

.source_root

Returns the source root for this generator using default_source_root as default.

.add_shebang_option!

Small macro to add ruby as an option to the generator with proper default value plus an instance helper method called shebang.

.banner

Use Rails default banner.

.base_name

Sets the base_name taking into account the current class namespace.

.default_aliases_for_option

Returns default aliases for the option name given doing a lookup in aliases.

.default_for_option

Returns default for the option name given doing a lookup in config.

.default_generator_root,
.default_value_for_option

Returns the default value for the option name given doing a lookup in options.

.generator_name

Removes the namespaces and get the generator name.

.usage_path,
.class_option

Make class option aware of options and aliases.

.inherited

Cache source root and add lib/generators/base/generator/templates to source paths.

.hooks

Keep hooks configuration that are used on prepare_for_invocation.

.prepare_for_invocation

Prepare class invocation to search on ::Rails namespace if a previous added hook is being used.

Instance Attribute Summary

Instance Method Summary

AppBase - Inherited

AppName - Included

Database - Included

Base - Inherited

#class_collisions

Check whether the given class names are already taken by user application or Ruby on ::Rails.

#extract_last_module

Takes in an array of nested modules and extracts the last module.

#indent,
#module_namespacing

Wrap block with namespace of current application if namespace exists and is not skipped.

#namespace, #namespace_dirs, #namespaced_path, #wrap_with_namespace

Actions - Included

#add_source

Add the given source to Gemfile

#application
#environment

Adds configuration code to a Rails runtime environment.

#gem

Adds a gem declaration to the Gemfile for the specified gem.

#gem_group

Wraps gem entries inside a group.

#generate

Runs another generator.

#git

Runs one or more git commands.

#github,
#initializer

Creates an initializer file in config/initializers/.

#lib

Creates a file in lib/.

#rails_command

Runs the specified Rails command.

#rake

Runs the specified Rake task.

#rakefile

Creates a Rake tasks file in lib/tasks/.

#readme

Reads the given file at the source root and prints it in the console.

#route

Make an entry in Rails routing file config/routes.rb.

#vendor

Creates a file in vendor/.

#append_file_with_newline

Append string to a file with a newline if necessary.

#execute_command

Runs the supplied command using either “rake …” or “rails …” based on the executor parameter provided.

#indentation

Indent the Gemfile to the depth of @indentation.

#log

Define log for backwards compatibility.

#match_file,
#optimize_indentation

Returns optimized string with indentation.

#quote

Always returns value in double quotes.

#rebase_indentation
#route_namespace_pattern,
#with_indentation

Manage Gemfile indentation for a DSL action block.

#initialize

Constructor Details

.new(*args) ⇒ PluginGenerator

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 214

def initialize(*args)
  @dummy_path = nil
  super
  imply_options

  if !engine? || !with_dummy_app?
    self.options = options.merge(skip_asset_pipeline: true).freeze
  end
end

Class Method Details

Instance Attribute Details

#api?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 368

def api?
  options[:api]
end

#engine?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 348

def engine?
  full? || mountable? || options[:engine]
end

#full?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 352

def full?
  options[:full]
end

#inside_application?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 460

def inside_application?
  rails_app_path && destination_root.start_with?(rails_app_path.to_s)
end

#mountable?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 356

def mountable?
  options[:mountable]
end

#skip_git?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 360

def skip_git?
  options[:skip_git]
end

#valid_const?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 427

def valid_const?
  if /-\d/.match?(original_name)
    raise Error, "Invalid plugin name #{original_name}. Please give a name which does not contain a namespace starting with numeric characters."
  elsif /[^\w-]+/.match?(original_name)
    raise Error, "Invalid plugin name #{original_name}. Please give a name which uses only alphabetic, numeric, \"_\" or \"-\" characters."
  elsif /^\d/.match?(camelized)
    raise Error, "Invalid plugin name #{original_name}. Please give a name which does not start with numbers."
  elsif RESERVED_NAMES.include?(name)
    raise Error, "Invalid plugin name #{original_name}. Please give a " \
                 "name which does not match one of the reserved rails " \
                 "words: #{RESERVED_NAMES.join(", ")}"
  elsif Object.const_defined?(camelized)
    raise Error, "Invalid plugin name #{original_name}, constant #{camelized} is already in use. Please choose another plugin name."
  end
end

#with_dummy_app?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 364

def with_dummy_app?
  options[:skip_test].blank? || options[:dummy_path] != "test/dummy"
end

Instance Method Details

#author (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 405

def author
  default = "TODO: Write your name"
  if skip_git?
    @author = default
  else
    @author = `git config user.name`.chomp rescue default
  end
end

#camelized (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 401

def camelized
  @camelized ||= name.gsub(/\W/, "_").squeeze("_").camelize
end

#camelized_modules (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 393

def camelized_modules
  @camelized_modules ||= namespaced_name.camelize
end

#create_app_files

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 242

def create_app_files
  build(:app)
end

#create_assets_manifest_file

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 254

def create_assets_manifest_file
  build(:assets_manifest) if !api? && engine?
end

#create_bin_files

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 262

def create_bin_files
  build(:bin)
end

#create_config_files

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 246

def create_config_files
  build(:config)
end

#create_dummy_app(path = nil) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 334

def create_dummy_app(path = nil)
  dummy_path(path) if path

  say_status :vendor_app, dummy_path
  mute do
    build(:generate_test_dummy)
    build(:test_dummy_config)
    build(:test_dummy_sprocket_assets) unless skip_sprockets?
    build(:test_dummy_clean)
    # ensure that bin/rails has proper dummy_path
    build(:bin, true)
  end
end

#create_lib_files

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 250

def create_lib_files
  build(:lib)
end

#create_public_stylesheets_files

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 258

def create_public_stylesheets_files
  build(:stylesheets) unless api?
end

#create_root_files

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 232

def create_root_files
  build(:readme)
  build(:rakefile)
  build(:gemspec)   unless options[:skip_gemspec]
  build(:license)
  build(:gitignore) unless options[:skip_git]
  build(:gemfile)
  build(:version_control)
end

#create_test_dummy_files

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 270

def create_test_dummy_files
  return unless with_dummy_app?
  create_dummy_app
end

#create_test_files

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 266

def create_test_files
  build(:test) unless options[:skip_test]
end

#dummy_path(path = nil) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 447

def dummy_path(path = nil)
  @dummy_path = path if path
  @dummy_path || options[:dummy_path]
end

#email (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 414

def email
  default = "TODO: Write your email address"
  if skip_git?
    @email = default
  else
    @email = `git config user.email`.chomp rescue default
  end
end

#finish_template

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 279

def finish_template
  build(:leftovers)
end

#gemfile_entries (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 306

def gemfile_entries
  [
    rails_gemfile_entry,
    simplify_gemfile_entries(
      web_server_gemfile_entry,
      database_gemfile_entry,
      asset_pipeline_gemfile_entry,
    ),
  ].flatten.compact
end

#get_builder_class (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 443

def get_builder_class
  defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder
end

#humanized (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 397

def humanized
  @humanized ||= original_name.underscore.humanize
end

#modules (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 380

def modules
  @modules ||= namespaced_name.camelize.split("::")
end

#mute(&block) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 452

def mute(&block)
  shell.mute(&block)
end

#name

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 285

def name
  @name ||= begin
    # same as ActiveSupport::Inflector#underscore except not replacing '-'
    underscored = original_name.dup
    underscored.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    underscored.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
    underscored.downcase!

    underscored
  end
end

#namespaced_name

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 301

def namespaced_name
  @namespaced_name ||= name.tr("-", "/")
end

#original_name (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 376

def original_name
  @original_name ||= File.basename(destination_root)
end

#plugin_path

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 193

alias_method :plugin_path, :app_path

#rails_app_path (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 456

def rails_app_path
  APP_PATH.sub("/config/application", "") if defined?(APP_PATH)
end

#rails_gemfile_entry (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 317

def rails_gemfile_entry
  if options[:skip_gemspec]
    super
  elsif rails_prerelease?
    super.dup.tap do |entry|
      entry.comment = <<~COMMENT
        Your gem is dependent on a prerelease version of Rails. Once you can lock this
        dependency down to a specific version, move it to your gemspec.
      COMMENT
    end
  end
end

#rails_version_specifier(gem_version = Rails.gem_version) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 423

def rails_version_specifier(gem_version = Rails.gem_version)
  [">= #{gem_version}"]
end

#relative_path (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 464

def relative_path
  return unless inside_application?
  app_path.delete_prefix("#{rails_app_path}/")
end

#simplify_gemfile_entries(*gemfile_entries) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 330

def simplify_gemfile_entries(*gemfile_entries)
  gemfile_entries.flatten.compact.map { |entry| GemfileEntry.floats(entry.name) }
end

#target_rails_prerelease

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 228

def target_rails_prerelease
  super("plugin new")
end

#underscored_name

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 297

def underscored_name
  @underscored_name ||= original_name.underscore
end

#update_gemfile

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 275

def update_gemfile
  build(:gemfile_entry) unless options[:skip_gemfile_entry]
end

#wrap_in_modules(unwrapped_code) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/plugin/plugin_generator.rb', line 384

def wrap_in_modules(unwrapped_code)
  unwrapped_code = "#{unwrapped_code}".strip.gsub(/\s$\n/, "")
  modules.reverse.inject(unwrapped_code) do |content, mod|
    str = +"module #{mod}\n"
    str << content.lines.map { |line| "  #{line}" }.join
    str << (content.present? ? "\nend" : "end")
  end
end