Class: Rails::CommandsTasks

Inherits:
Object show all
Includes:
RakeProxy
Defined in:
railties/lib/rails/commands/commands_tasks.rb

Overview

This is a class which takes in a rails command and initiates the appropriate initiation sequence.

Warning: This class mutates ARGV because some commands require manipulating it before they are run.

Constant Summary collapse

HELP_MESSAGE =
<<-EOT
Usage: rails COMMAND [ARGS]

The most common rails commands are:
 generate    Generate new code (short-cut alias: "g")
 console     Start the Rails console (short-cut alias: "c")
 server      Start the Rails server (short-cut alias: "s")
 test        Run tests (short-cut alias: "t")
 dbconsole   Start a console for the database specified in config/database.yml
             (short-cut alias: "db")
 new         Create a new Rails application. "rails new my_app" creates a
             new application called MyApp in "./my_app"

All commands can be run with -h (or --help) for more information.

In addition to those commands, there are:
EOT
ADDITIONAL_COMMANDS =
[
  [ 'destroy', 'Undo code generated with "generate" (short-cut alias: "d")' ],
  [ 'plugin new', 'Generates skeleton for developing a Rails plugin' ],
  [ 'runner',
    'Run a piece of code in the application environment (short-cut alias: "r")' ]
]
COMMAND_WHITELIST =
%w(plugin generate destroy console server dbconsole runner new version help test)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CommandsTasks

Returns a new instance of CommandsTasks.


41
42
43
# File 'railties/lib/rails/commands/commands_tasks.rb', line 41

def initialize(argv)
  @argv = argv
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv


12
13
14
# File 'railties/lib/rails/commands/commands_tasks.rb', line 12

def argv
  @argv
end

Instance Method Details

#consoleObject


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'railties/lib/rails/commands/commands_tasks.rb', line 67

def console
  require_command!("console")
  options = Rails::Console.parse_arguments(argv)

  # RAILS_ENV needs to be set before config/application is required
  ENV['RAILS_ENV'] = options[:environment] if options[:environment]

  # shift ARGV so IRB doesn't freak
  shift_argv!

  require_application_and_environment!
  Rails::Console.start(Rails.application, options)
end

#dbconsoleObject


98
99
100
101
# File 'railties/lib/rails/commands/commands_tasks.rb', line 98

def dbconsole
  require_command!("dbconsole")
  Rails::DBConsole.start
end

#destroyObject


63
64
65
# File 'railties/lib/rails/commands/commands_tasks.rb', line 63

def destroy
  generate_or_destroy(:destroy)
end

#generateObject


59
60
61
# File 'railties/lib/rails/commands/commands_tasks.rb', line 59

def generate
  generate_or_destroy(:generate)
end

#helpObject


120
121
122
123
# File 'railties/lib/rails/commands/commands_tasks.rb', line 120

def help
  write_help_message
  write_commands ADDITIONAL_COMMANDS + formatted_rake_tasks
end

#newObject


107
108
109
110
111
112
113
# File 'railties/lib/rails/commands/commands_tasks.rb', line 107

def new
  if %w(-h --help).include?(argv.first)
    require_command!("application")
  else
    exit_with_initialization_warning!
  end
end

#pluginObject


55
56
57
# File 'railties/lib/rails/commands/commands_tasks.rb', line 55

def plugin
  require_command!("plugin")
end

#run_command!(command) ⇒ Object


45
46
47
48
49
50
51
52
53
# File 'railties/lib/rails/commands/commands_tasks.rb', line 45

def run_command!(command)
  command = parse_command(command)

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

#runnerObject


103
104
105
# File 'railties/lib/rails/commands/commands_tasks.rb', line 103

def runner
  require_command!("runner")
end

#serverObject


81
82
83
84
85
86
87
88
89
90
91
92
# File 'railties/lib/rails/commands/commands_tasks.rb', line 81

def server
  set_application_directory!
  require_command!("server")

  Rails::Server.new.tap do |server|
    # We need to require application after the server sets environment,
    # otherwise the --environment option given to the server won't propagate.
    require APP_PATH
    Dir.chdir(Rails.application.root)
    server.start
  end
end

#testObject


94
95
96
# File 'railties/lib/rails/commands/commands_tasks.rb', line 94

def test
  require_command!("test")
end

#versionObject


115
116
117
118
# File 'railties/lib/rails/commands/commands_tasks.rb', line 115

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