aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/commands/common_commands_tasks.rb
blob: c1484d7ae2022fab58b8b9b9b2fd7c4a283f3fb3 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module Rails
  module CommonCommandsTasks # :nodoc:
    def run_command!(command)
      command = parse_command(command)

      if command_whitelist.include?(command)
        send(command)
      else
        run_rake_task(command)
      end
    end

    def generate
      generate_or_destroy(:generate)
    end

    def destroy
      generate_or_destroy(:destroy)
    end

    def test
      require_command!("test")
    end

    def version
      argv.unshift "--version"
      require_command!("application")
    end

    def help
      write_help_message
      write_commands(commands)
    end

    private

      def generate_or_destroy(command)
        require "rails/generators"
        require_application_and_environment!
        load_generators
        require_command!(command)
      end

      def require_command!(command)
        require "rails/commands/#{command}"
      end

      def write_help_message
        puts help_message
      end

      def write_commands(commands)
        width = commands.map { |name, _| name.size }.max || 10
        commands.each { |command| printf(" %-#{width}s   %s\n", *command) }
      end

      def parse_command(command)
        case command
        when "--version", "-v"
          "version"
        when "--help", "-h"
          "help"
        else
          command
        end
      end
  end
end