Class: Rails::CommandsTasks

Inherits:
Object show all
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")
 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"

In addition to those, there are:
 application  Generate the Rails application code
 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")

All commands can be run with -h (or --help) for more information.
EOT
COMMAND_WHITELIST =
%(plugin generate destroy console server dbconsole application runner new version help)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CommandsTasks

Returns a new instance of CommandsTasks.



33
34
35
# File 'railties/lib/rails/commands/commands_tasks.rb', line 33

def initialize(argv)
  @argv = argv
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv



8
9
10
# File 'railties/lib/rails/commands/commands_tasks.rb', line 8

def argv
  @argv
end

Instance Method Details

#applicationObject



86
87
88
# File 'railties/lib/rails/commands/commands_tasks.rb', line 86

def application
  require_command!("application")
end

#consoleObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'railties/lib/rails/commands/commands_tasks.rb', line 54

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



81
82
83
84
# File 'railties/lib/rails/commands/commands_tasks.rb', line 81

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

#generateObject



50
51
52
# File 'railties/lib/rails/commands/commands_tasks.rb', line 50

def generate
  generate_or_destroy(:generate)
end

#helpObject



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

def help
  write_help_message
end

#newObject



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

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

#pluginObject



46
47
48
# File 'railties/lib/rails/commands/commands_tasks.rb', line 46

def plugin
  require_command!("plugin")
end

#run_command!(command) ⇒ Object



37
38
39
40
41
42
43
44
# File 'railties/lib/rails/commands/commands_tasks.rb', line 37

def run_command!(command)
  command = parse_command(command)
  if COMMAND_WHITELIST.include?(command)
    send(command)
  else
    write_error_message(command)
  end
end

#runnerObject



90
91
92
# File 'railties/lib/rails/commands/commands_tasks.rb', line 90

def runner
  require_command!("runner")
end

#serverObject



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

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

#versionObject



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

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