# Set up gems listed in the Gemfile.ENV['BUNDLE_GEMFILE']||=File.expand_path('../../Gemfile',__FILE__)require'bundler/setup'ifFile.exists?(ENV['BUNDLE_GEMFILE'])
when'server'# Change to the application's path if there is no config.ru file in current dir.# This allows us to run `rails server` from other directories, but still get# the main config.ru and properly set the tmp directory.Dir.chdir(File.expand_path('../../',APP_PATH))unlessFile.exists?(File.expand_path("config.ru"))require'rails/commands/server'Rails::Server.new.tapdo|server|# We need to require application after the server sets environment,# otherwise the --environment option given to the server won't propagate.requireAPP_PATHDir.chdir(Rails.application.root)server.startend
defparse!(args)args,options=args.dup,{}opt_parser=OptionParser.newdo|opts|opts.banner="Usage: rails server [mongrel, thin, etc] [options]"opts.on("-p","--port=port",Integer,"Runs Rails on the specified port.","Default: 3000"){|v|options[:Port]=v}...
defstarturl="#{options[:SSLEnable]?'https':'http'}://#{options[:Host]}:#{options[:Port]}"puts"=> Booting #{ActiveSupport::Inflector.demodulize(server)}"puts"=> Rails #{Rails.version} application starting in #{Rails.env} on #{url}"puts"=> Run `rails server -h` for more startup options"trap(:INT){exit}puts"=> Ctrl-C to shutdown server"unlessoptions[:daemonize]#Create required tmp directories if not found%w(cache pids sessions sockets).eachdo|dir_to_make|FileUtils.mkdir_p(Rails.root.join('tmp',dir_to_make))endunlessoptions[:daemonize]wrapped_app# touch the app so the logger is set upconsole=ActiveSupport::Logger.new($stdout)console.formatter=Rails.logger.formatterRails.logger.extend(ActiveSupport::Logger.broadcast(console))endsuperensure# The '-h' option calls exit before @options is set.# If we call 'options' with it unset, we get double help banners.puts'Exiting'unless@options&&options[:daemonize]end