aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/commands/plugin/plugin_command.rb
blob: b40ab006af582207ea7d0670d0cdca221b658cdd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Rails
  module Command
    class PluginCommand < Base # :nodoc:
      hide_command!

      def help
        run_plugin_generator %w( --help )
      end

      def self.banner(*) # :nodoc:
        "#{executable} new [options]"
      end

      class_option :rc, type: :string, default: File.join("~", ".railsrc"),
        desc: "Initialize the plugin command with previous defaults. Uses .railsrc in your home directory by default."

      class_option :no_rc, desc: "Skip evaluating .railsrc."

      def perform(type = nil, *plugin_args)
        plugin_args << "--help" unless type == "new"

        unless options.key?("no_rc") # Thor's not so indifferent access hash.
          railsrc = File.expand_path(options[:rc])

          if File.exist?(railsrc)
            extra_args = File.read(railsrc).split(/\n+/).flat_map(&:split)
            puts "Using #{extra_args.join(" ")} from #{railsrc}"
            plugin_args.insert(1, *extra_args)
          end
        end

        run_plugin_generator plugin_args
      end

      private
        def run_plugin_generator(plugin_args)
          require "rails/generators"
          require "rails/generators/rails/plugin/plugin_generator"
          Rails::Generators::PluginGenerator.start plugin_args
        end
    end
  end
end