跳至内容 跳至搜索

插件构建器允许您覆盖插件生成器的元素,而无需被迫反转默认生成器的操作。

这使您可以覆盖整个操作,例如创建 Gemfile、README 或 JavaScript 文件,而无需了解这些操作的确切内容,以便您可以创建另一个模板操作。

方法
A
B
C
G
L
R
S
T
V

常量

DUMMY_IGNORE_OPTIONS = %i[dev edge master template]
 

实例公共方法

app()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 20
def app
  if mountable?
    if api?
      directory "app", exclude_pattern: %r{app/(views|helpers)}
    else
      directory "app"
      empty_directory_with_keep_file "app/assets/images/#{namespaced_name}"
    end

    empty_directory_with_keep_file "app/models/concerns"
    empty_directory_with_keep_file "app/controllers/concerns"
    remove_dir "app/mailers" if options[:skip_action_mailer]
    remove_dir "app/jobs" if options[:skip_active_job]
  elsif full?
    empty_directory_with_keep_file "app/models"
    empty_directory_with_keep_file "app/controllers"
    empty_directory_with_keep_file "app/models/concerns"
    empty_directory_with_keep_file "app/controllers/concerns"
    empty_directory_with_keep_file "app/mailers" unless options[:skip_action_mailer]
    empty_directory_with_keep_file "app/jobs" unless options[:skip_active_job]

    unless api?
      empty_directory_with_keep_file "app/assets/images/#{namespaced_name}"
      empty_directory_with_keep_file "app/helpers"
      empty_directory_with_keep_file "app/views"
    end
  end
end

bin()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 181
def bin
  exclude_pattern = Regexp.union([(engine? ? /test\.tt/ : /rails\.tt/), (/rubocop/ if skip_rubocop?)].compact)
  directory "bin", { exclude_pattern: exclude_pattern } do |content|
    "#{shebang}\n" + content
  end
  chmod "bin", 0755 & ~File.umask, verbose: false
end

cifiles()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 69
def cifiles
  empty_directory ".github/workflows"
  template "github/ci.yml", ".github/workflows/ci.yml"
  template "github/dependabot.yml", ".github/dependabot.yml"
end

config()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 97
def config
  template "config/routes.rb" if engine?
end

gemfile()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 53
def gemfile
  template "Gemfile"
end

gemfile_entry()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 189
def gemfile_entry
  return unless inside_application?

  gemfile_in_app_path = File.join(rails_app_path, "Gemfile")
  if File.exist? gemfile_in_app_path
    entry = %{\ngem "#{name}", path: "#{relative_path}"}
    append_file gemfile_in_app_path, entry
  end
end

gemspec()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 61
def gemspec
  template "%name%.gemspec"
end

generate_test_dummy(force = false)

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 122
def generate_test_dummy(force = false)
  opts = options.transform_keys(&:to_sym).except(*DUMMY_IGNORE_OPTIONS)
  opts[:force] = force
  opts[:skip_thruster] = true
  opts[:skip_brakeman] = true
  opts[:skip_bundle] = true
  opts[:skip_ci] = true
  opts[:skip_kamal] = true
  opts[:skip_solid] = true
  opts[:skip_git] = true
  opts[:skip_hotwire] = true
  opts[:skip_rubocop] = true
  opts[:dummy_app] = true

  invoke Rails::Generators::AppGenerator,
    [ File.expand_path(dummy_path, destination_root) ], opts
end

gitignore()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 65
def gitignore
  template "gitignore", ".gitignore"
end

lib()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 85
def lib
  template "lib/%namespaced_name%.rb"
  template "lib/tasks/%namespaced_name%_tasks.rake"
  template "lib/%namespaced_name%/version.rb"

  if engine?
    template "lib/%namespaced_name%/engine.rb"
  else
    template "lib/%namespaced_name%/railtie.rb"
  end
end

license()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 57
def license
  template "MIT-LICENSE" unless inside_application?
end

rakefile()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 16
def rakefile
  template "Rakefile"
end

readme()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 49
def readme
  template "README.md"
end

rubocop()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 75
def rubocop
  template "rubocop.yml", ".rubocop.yml"
end

stylesheets()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 172
def stylesheets
  if mountable?
    copy_file "rails/stylesheets.css",
              "app/assets/stylesheets/#{namespaced_name}/application.css"
  elsif full?
    empty_directory_with_keep_file "app/assets/stylesheets/#{namespaced_name}"
  end
end

test()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 101
def test
  template "test/test_helper.rb"
  template "test/%namespaced_name%_test.rb"

  if engine?
    empty_directory_with_keep_file "test/fixtures/files"
    empty_directory_with_keep_file "test/controllers"
    empty_directory_with_keep_file "test/mailers"
    empty_directory_with_keep_file "test/models"
    empty_directory_with_keep_file "test/integration"

    unless api?
      empty_directory_with_keep_file "test/helpers"
    end

    template "test/integration/navigation_test.rb"
  end
end

test_dummy_assets()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 155
def test_dummy_assets
  template "rails/stylesheets.css", "#{dummy_path}/app/assets/stylesheets/application.css", force: true
end

test_dummy_clean()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 159
def test_dummy_clean
  inside dummy_path do
    remove_file ".ruby-version"
    remove_dir "db"
    remove_file "Gemfile"
    remove_dir "lib"
    remove_file "public/robots.txt"
    remove_file "README.md"
    remove_file "test"
    remove_file "vendor"
  end
end

test_dummy_config()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 140
    def test_dummy_config
      template "rails/boot.rb", "#{dummy_path}/config/boot.rb", force: true

      if mountable?
        template "rails/routes.rb", "#{dummy_path}/config/routes.rb", force: true
      end
      if engine? && !api?
        insert_into_file "#{dummy_path}/config/application.rb", indent(<<~RUBY, 4), after: /^\s*config\.load_defaults.*\n/

          # For compatibility with applications that use this config
          config.action_controller.include_all_helpers = false
        RUBY
      end
    end

version_control()

# File railties/lib/rails/generators/rails/plugin/plugin_generator.rb, line 79
def version_control
  if !options[:skip_git] && !options[:pretend]
    run git_init_command, capture: options[:quiet], abort_on_failure: false
  end
end