Rails チュートリアル 1章 勉強メモ12

Rails チュートリアルの勉強メモ

railstutorial.jp

rails server を追ってみる

rails new との違い

gems/railties-5.1.7/lib/rails/app_loader.rb @ line 36 Rails::AppLoader#exec_app:

    32: def exec_app
    33:   original_cwd = Dir.pwd
    34: 
    35:   loop do
 => 36:     if exe = find_executable
    37:       contents = File.read(exe)
    38: 
    39:       if contents =~ /(APP|ENGINE)_PATH/
    40:         exec RUBY, exe, *ARGV
    41:         break # non reachable, hack to be able to stub exec in the test suite
    42:       elsif exe.end_with?("bin/rails") && contents.include?("This file was generated by Bundler")
    43:         $stderr.puts(BUNDLER_WARNING)
    44:         Object.const_set(:APP_PATH, File.expand_path("config/application", Dir.pwd))
    45:         require File.expand_path("../boot", APP_PATH)
    46:         require "rails/commands"
    47:         break
    48:       end
    49:     end
    50: 
    51:     # If we exhaust the search there is no executable, this could be a
    52:     # call to generate a new application, so restore the original cwd.
    53:     Dir.chdir(original_cwd) && return if Pathname.new(Dir.pwd).root?
    54: 
    55:     # Otherwise keep moving upwards in search of an executable.
    56:     Dir.chdir("..")
    57:   end
    58: end

Rails チュートリアル 1章 勉強メモ3 - mir3636のブログ

find_executable で bin/rails または script/rails を探している 見つからなかったら一つ上の Dir へ行くを繰り返す rails アプリを new していれば bin/rails があるはずなのでおそらくそれを探している 今回はないのでそのまま続行

  • はい、今回は rails new でバッチリ bin/rails が作られているので if 文に突っ込んでいきます
    39:       if contents =~ /(APP|ENGINE)_PATH/
    40:         exec RUBY, exe, *ARGV
    41:         break # non reachable, hack to be able to stub exec in the test suite
  • 39行目で bin/rails の中身をパターンマッチ
  • 40行目に入り、bin/railsruby で実行

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'
  • spring をロード

GitHub - rails/spring: Rails application preloader

  • rails の preloader らしい
  • 本筋じゃないのでスルーで

  • APP_PATH を設定

  • config/boot を読む

config/boot

hello_app/config/boot.rb @ line 1 :

 => 1: ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
    2: 
    3: require 'bundler/setup' # Set up gems listed in the Gemfile.
  • Gemfile のリストから gem をセットアップ(bundler/setup.rb

    • LOAD_PATH に Gemfile のリストに対応したパスを入れている
  • 眠くなってきたので終了

  • 次は rails/commands を読むかと