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, Base, Actions, Thor::Actions, Thor::Group
Inherits: Rails::Generators::AppBase
Defined in: railties/lib/rails/generators/rails/plugin/plugin_generator.rb

Constant Summary

AppName - Included

RESERVED_NAMES

AppBase - Inherited

BUN_VERSION, CSS_OPTIONS, JAVASCRIPT_OPTIONS, 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

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 225

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 389

def api?
  options[:api]
end

#engine?Boolean (readonly, private)

[ GitHub ]

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

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

#full?Boolean (readonly, private)

[ GitHub ]

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

def full?
  options[:full]
end

#inside_application?Boolean (readonly, private)

[ GitHub ]

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

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 377

def mountable?
  options[:mountable]
end

#skip_git?Boolean (readonly, private)

[ GitHub ]

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

def skip_git?
  options[:skip_git]
end

#valid_const?Boolean (readonly, private)

[ GitHub ]

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

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 385

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 426

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 422

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

#camelized_modules (private)

[ GitHub ]

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

def camelized_modules
  @camelized_modules ||= namespaced_name.camelize
end

#create_app_files

[ GitHub ]

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

def create_app_files
  build(:app)
end

#create_assets_manifest_file

[ GitHub ]

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

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 283

def create_bin_files
  build(:bin)
end

#create_cifiles

[ GitHub ]

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

def create_cifiles
  return if skip_ci?
  build(:cifiles)
end

#create_config_files

[ GitHub ]

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

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 355

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_assets) unless skip_asset_pipeline?
    build(:test_dummy_clean)
    # ensure that bin/rails has proper dummy_path
    build(:bin)
  end
end

#create_lib_files

[ GitHub ]

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

def create_lib_files
  build(:lib)
end

#create_public_stylesheets_files

[ GitHub ]

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

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 243

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_rubocop_file

[ GitHub ]

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

def create_rubocop_file
  return if skip_rubocop?
  build(:rubocop)
end

#create_test_dummy_files

[ GitHub ]

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

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 287

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 468

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 435

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 300

def finish_template
  build(:leftovers)
end

#gemfile_entries (private)

[ GitHub ]

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

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 464

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

#humanized (private)

[ GitHub ]

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

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

#modules (private)

[ GitHub ]

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

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

#mute(&block) (private)

[ GitHub ]

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

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

#name

[ GitHub ]

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

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 322

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

#original_name (private)

[ GitHub ]

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

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

#plugin_path

[ GitHub ]

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

alias_method :plugin_path, :app_path

#rails_app_path (private)

[ GitHub ]

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

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 338

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 444

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 485

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 351

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 239

def target_rails_prerelease
  super("plugin new")
end

#test_command (private)

[ GitHub ]

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

def test_command
  if engine? && !options[:skip_active_record] && with_dummy_app?
    "bin/rails db:test:prepare test"
  elsif engine?
    "bin/rails test"
  else
    "bin/test"
  end
end

#underscored_name

[ GitHub ]

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

def underscored_name
  @underscored_name ||= original_name.underscore
end

#update_gemfile

[ GitHub ]

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

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 405

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