class Rails::Generators::PluginGenerator

:nodoc:

def self.banner

def self.banner
  "rails plugin new #{arguments.map(&:usage).join(' ')} [options]"
end

def api?

def api?
  options[:api]
end

def author

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

def camelized

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

def camelized_modules

def camelized_modules
  @camelized_modules ||= namespaced_name.camelize
end

def create_app_files

def create_app_files
  build(:app)
end

def create_assets_manifest_file

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

def create_bin_files

def create_bin_files
  build(:bin)
end

def create_config_files

def create_config_files
  build(:config)
end

def create_dummy_app(path = nil)

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

def create_lib_files

def create_lib_files
  build(:lib)
end

def create_public_stylesheets_files

def create_public_stylesheets_files
  build(:stylesheets) unless api?
end

def create_root_files

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

def create_test_dummy_files

def create_test_dummy_files
  return unless with_dummy_app?
  create_dummy_app
end

def create_test_files

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

def dummy_path(path = nil)

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

def email

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

def engine?

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

def finish_template

def finish_template
  build(:leftovers)
end

def full?

def full?
  options[:full]
end

def gemfile_entries

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

def get_builder_class

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

def humanized

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

def initialize(*args)

def initialize(*args)
  @dummy_path = nil
  super
  if !engine? || !with_dummy_app?
    self.options = options.merge(skip_asset_pipeline: true).freeze
  end
end

def inside_application?

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

def modules

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

def mountable?

def mountable?
  options[:mountable]
end

def mute(&block)

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

def name

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

def namespaced_name

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

def original_name

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

def rails_app_path

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

def rails_gemfile_entry

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

def rails_version_specifier(gem_version = Rails.gem_version)

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

def relative_path

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

def simplify_gemfile_entries(*gemfile_entries)

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

def skip_git?

def skip_git?
  options[:skip_git]
end

def target_rails_prerelease

def target_rails_prerelease
  super("plugin new")
end

def underscored_name

def underscored_name
  @underscored_name ||= original_name.underscore
end

def update_gemfile

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

def valid_const?

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

def with_dummy_app?

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

def wrap_in_modules(unwrapped_code)

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