Class: Rails::CommandsTasks

Inherits:
Object
  • Object
show all
Defined in:
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 =
"Usage: rails COMMAND [ARGS]\n\nThe most common rails commands are:\n generate    Generate new code (short-cut alias: \"g\")\n console     Start the Rails console (short-cut alias: \"c\")\n server      Start the Rails server (short-cut alias: \"s\")\n dbconsole   Start a console for the database specified in config/database.yml\n             (short-cut alias: \"db\")\n new         Create a new Rails application. \"rails new my_app\" creates a\n             new application called MyApp in \"./my_app\"\n\nIn addition to those, there are:\n destroy      Undo code generated with \"generate\" (short-cut alias: \"d\")\n plugin new   Generates skeleton for developing a Rails plugin\n runner       Run a piece of code in the application environment (short-cut alias: \"r\")\n\nAll commands can be run with -h (or --help) for more information.\n"
COMMAND_WHITELIST =
%w(plugin generate destroy console server dbconsole runner new version help)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CommandsTasks



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

def initialize(argv)
  @argv = argv
end

Instance Attribute Details

#argvObject (readonly)

:nodoc:



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

def argv
  @argv
end

Instance Method Details

#consoleObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rails/commands/commands_tasks.rb', line 57

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



84
85
86
87
# File 'lib/rails/commands/commands_tasks.rb', line 84

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

#destroyObject



53
54
55
# File 'lib/rails/commands/commands_tasks.rb', line 53

def destroy
  generate_or_destroy(:destroy)
end

#generateObject



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

def generate
  generate_or_destroy(:generate)
end

#helpObject



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

def help
  write_help_message
end

#newObject



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

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

#pluginObject



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

def plugin
  require_command!("plugin")
end

#run_command!(command) ⇒ Object



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

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

#runnerObject



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

def runner
  require_command!("runner")
end

#serverObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rails/commands/commands_tasks.rb', line 71

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



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

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