class Rails::CommandsTasks
:nodoc:
it before they are run.
Warning: This class mutates ARGV because some commands require manipulating
initiation sequence.
This is a class which takes in a rails command and initiates the appropriate
def console
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
def dbconsole
def dbconsole require_command!("dbconsole") Rails::DBConsole.start end
def destroy
def destroy generate_or_destroy(:destroy) end
def exit_with_initialization_warning!
def exit_with_initialization_warning! puts "Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.\n" puts "Type 'rails' for help." exit(1) end
def generate
def generate generate_or_destroy(:generate) end
def generate_or_destroy(command)
def generate_or_destroy(command) require 'rails/generators' require_application_and_environment! Rails.application.load_generators require_command!(command) end
def help
def help write_help_message write_commands ADDITIONAL_COMMANDS + formatted_rake_tasks end
def initialize(argv)
def initialize(argv) @argv = argv end
def new
def new if %w(-h --help).include?(argv.first) require_command!("application") else exit_with_initialization_warning! end end
def parse_command(command)
def parse_command(command) case command when '--version', '-v' 'version' when '--help', '-h' 'help' else command end end
def plugin
def plugin require_command!("plugin") end
def require_application_and_environment!
def require_application_and_environment! require APP_PATH Rails.application.require_environment! end
def require_command!(command)
def require_command!(command) require "rails/commands/#{command}" end
def run_command!(command)
def run_command!(command) command = parse_command(command) if COMMAND_WHITELIST.include?(command) send(command) else run_rake_task(command) end end
def runner
def runner require_command!("runner") end
def server
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
def set_application_directory!
This allows us to run `rails server` from other directories, but still get
Change to the application's path if there is no config.ru file in current directory.
def set_application_directory! Dir.chdir(File.expand_path('../../', APP_PATH)) unless File.exist?(File.expand_path("config.ru")) end
def shift_argv!
def shift_argv! argv.shift if argv.first && argv.first[0] != '-' end
def test
def test require_command!("test") end
def version
def version argv.unshift '--version' require_command!("application") end
def write_commands(commands)
def write_commands(commands) width = commands.map { |name, _| name.size }.max || 10 commands.each { |command| printf(" %-#{width}s %s\n", *command) } end
def write_help_message
def write_help_message puts HELP_MESSAGE end