commands.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. require 'active_support/core_ext/object/inclusion'
  2. ARGV << '--help' if ARGV.empty?
  3. aliases = {
  4. "g" => "generate",
  5. "d" => "destroy",
  6. "c" => "console",
  7. "s" => "server",
  8. "db" => "dbconsole",
  9. "r" => "runner"
  10. }
  11. command = ARGV.shift
  12. command = aliases[command] || command
  13. case command
  14. when 'generate', 'destroy', 'plugin'
  15. require 'rails/generators'
  16. if command == 'plugin' && ARGV.first == 'new'
  17. require "rails/commands/plugin_new"
  18. else
  19. require APP_PATH
  20. Rails.application.require_environment!
  21. Rails.application.load_generators
  22. require "rails/commands/#{command}"
  23. end
  24. when 'benchmarker', 'profiler'
  25. require APP_PATH
  26. Rails.application.require_environment!
  27. require "rails/commands/#{command}"
  28. when 'console'
  29. require 'rails/commands/console'
  30. require APP_PATH
  31. Rails.application.require_environment!
  32. Rails::Console.start(Rails.application)
  33. when 'server'
  34. # Change to the application's path if there is no config.ru file in current dir.
  35. # This allows us to run script/rails server from other directories, but still get
  36. # the main config.ru and properly set the tmp directory.
  37. Dir.chdir(File.expand_path('../../', APP_PATH)) unless File.exists?(File.expand_path("config.ru"))
  38. require 'rails/commands/server'
  39. Rails::Server.new.tap { |server|
  40. # We need to require application after the server sets environment,
  41. # otherwise the --environment option given to the server won't propagate.
  42. require APP_PATH
  43. Dir.chdir(Rails.application.root)
  44. server.start
  45. }
  46. when 'dbconsole'
  47. require 'rails/commands/dbconsole'
  48. require APP_PATH
  49. Rails::DBConsole.start(Rails.application)
  50. when 'application', 'runner'
  51. require "rails/commands/#{command}"
  52. when 'new'
  53. if ARGV.first.in?(['-h', '--help'])
  54. require 'rails/commands/application'
  55. else
  56. puts "Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.\n"
  57. puts "Type 'rails' for help."
  58. exit(1)
  59. end
  60. when '--version', '-v'
  61. ARGV.unshift '--version'
  62. require 'rails/commands/application'
  63. else
  64. puts "Error: Command not recognized" unless command.in?(['-h', '--help'])
  65. puts <<-EOT
  66. Usage: rails COMMAND [ARGS]
  67. The most common rails commands are:
  68. generate Generate new code (short-cut alias: "g")
  69. console Start the Rails console (short-cut alias: "c")
  70. server Start the Rails server (short-cut alias: "s")
  71. dbconsole Start a console for the database specified in config/database.yml
  72. (short-cut alias: "db")
  73. new Create a new Rails application. "rails new my_app" creates a
  74. new application called MyApp in "./my_app"
  75. In addition to those, there are:
  76. application Generate the Rails application code
  77. destroy Undo code generated with "generate" (short-cut alias: "d")
  78. benchmarker See how fast a piece of code runs
  79. profiler Get profile information from a piece of code
  80. plugin Install a plugin
  81. runner Run a piece of code in the application environment (short-cut alias: "r")
  82. All commands can be run with -h (or --help) for more information.
  83. EOT
  84. exit(1)
  85. end