Rails チュートリアル 1章 勉強メモ13
Rails チュートリアルの勉強メモ
rails server を追ってみる
bin/rails
- hello_app/bin/rails 1: #!/usr/bin/env ruby 2: begin 3: load File.expand_path('../spring', __FILE__) 4: rescue LoadError => e 5: raise unless e.message.include?('spring') 6: end 7: APP_PATH = File.expand_path('../config/application', __dir__) 8: require_relative '../config/boot' 9: require 'rails/commands'
- require 'rails/commands' から読む
rails/commands
require "rails/command" aliases = { "g" => "generate", "d" => "destroy", "c" => "console", "s" => "server", "db" => "dbconsole", "r" => "runner", "t" => "test" } command = ARGV.shift command = aliases[command] || command Rails::Command.invoke command, ARGV
- aliases で コマンドの省略に対応
Rails::Command.invoke command, ARGV
でコマンド実行invoke で server コマンドを見つけて実行
- Rails::Command::ServerCommand
thor/command.rb の run にて
[13] pry(#<Thor::Command>)> instance.method(:server).source_location => ["$HOME/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/railties-5.1.7/lib/rails/commands/server/server_command.rb", 128] [14] pry(#<Thor::Command>)> instance.method(:server).original_name => :perform
gems/railties-5.1.7/lib/rails/commands/server/server_command.rb @ line 129 Rails::Command::ServerCommand#perform: 128: def perform => 129: set_application_directory! 130: Rails::Server.new(server_options).tap do |server| 131: # Require application after server sets environment to propagate 132: # the --environment option. 133: require APP_PATH 134: Dir.chdir(Rails.application.root) 135: server.start 136: end 137: end
- set_application_directory! をみる
gems/railties-5.1.7/lib/rails/command/actions.rb @ line 8 Rails::Command::Actions#set_application_directory!: 7: def set_application_directory! => 8: Dir.chdir(File.expand_path("../../", APP_PATH)) unless File.exist?(File.expand_path("config.ru")) 9: end
Rails::Server.new(server_options).tap
- tap について少し調べた
- ブロックにレシーバ自身を入れてブロックを実行するらしい
- ここでは Rails::Server.new(server_options) をブロックに入れている
[1] pry(#<Rails::Command::ServerCommand>)> server => #<Rails::Server:0x00007fde42bedb60 @default_options= {:user_supplied_options=>[], :server=>nil, :log_stdout=>true, :Port=>3000, :Host=>"localhost", :DoNotReverseLookup=>true, :config=>"config.ru", :environment=>"development", :daemonize=>false, :pid=>"(rails newしたとこ)/hello_app/tmp/pids/server.pid", :caching=>nil, :restart_cmd=>"bin/rails server "}, @ignore_options=[], @options= {:user_supplied_options=>[], :server=>nil, :log_stdout=>true, :Port=>3000, :Host=>"localhost", :DoNotReverseLookup=>true, :config=>"config.ru", :environment=>"development", :daemonize=>false, :pid=>"(rails newしたとこ)/hello_app/tmp/pids/server.pid", :caching=>nil, :restart_cmd=>"bin/rails server "}, @use_default_options=false>
- APP_PATH = hello_app/config/application
require_relative 'boot' require 'rails/all' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) module HelloApp class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 5.1 # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. end end
おまけ server.start
gems/railties-5.1.7/lib/rails/commands/server/server_command.rb @ line 38 Rails::Server#start: 37: def start => 38: print_boot_information 39: trap(:INT) { exit } 40: create_tmp_directories 41: setup_dev_caching 42: log_to_stdout if options[:log_stdout] 43: 44: super 45: ensure 46: # The '-h' option calls exit before @options is set. 47: # If we call 'options' with it unset, we get double help banners. 48: puts "Exiting" unless @options && options[:daemonize] 49: end