123456789_123456789_123456789_123456789_123456789_

Class: Rails::Command::ServerCommand

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Base, Thor
Instance Chain:
Inherits: Rails::Command::Base
Defined in: railties/lib/rails/commands/server/server_command.rb

Constant Summary

Class Attribute Summary

Base - Inherited

.bin, .bin?,
.engine?

Returns true when the app is a Rails engine.

.exit_on_failure?

Class Method Summary

Base - Inherited

.banner,
.base_name

Sets the base_name taking into account the current class namespace.

.command_name

Return command name without namespaces.

.default_command_root

Default file root to place extra files a command might need, placed one folder above the command file.

.desc

Tries to get the description from a USAGE file one folder above the command root.

.executable,
.hide_command!

Convenience method to hide this command from the available ones when running rails command.

.namespace

Convenience method to get the namespace from the class name.

.printing_commands,
.usage_path

Path to lookup a USAGE description in a file.

.create_command

Allow the command method to be called perform.

.namespaced_name, .resolve_path, .class_usage,
.help

Override Thor’s class-level help to also show the USAGE.

.inherited, .perform

Instance Attribute Summary

Instance Method Summary

EnvironmentArgument - Included

Actions - Included

#boot_application!, #load_environment_config!,
#load_generators

See additional method definition at line 36.

#load_tasks

See additional method definition at line 31.

#require_application!,
#set_application_directory!

Change to the application’s path if there is no config.ru file in current directory.

Constructor Details

.new(args, local_options) ⇒ ServerCommand

[ GitHub ]

  
# File 'railties/lib/rails/commands/server/server_command.rb', line 125

def initialize(args, local_options, *)
  super

  @original_options = local_options - %w( --restart )
end

Instance Attribute Details

#log_to_stdout?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/server/server_command.rb', line 239

def log_to_stdout?
  options.fetch(:log_to_stdout) do
    options[:daemon].blank? && environment == "development"
  end
end

Instance Method Details

#early_hints (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/server/server_command.rb', line 235

def early_hints
  options[:early_hints]
end

#environment (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/server/server_command.rb', line 227

def environment
  options[:environment] || Rails::Command.environment
end

#host (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/server/server_command.rb', line 217

def host
  if options[:binding]
    options[:binding]
  else
    default_host = environment == "development" ? "localhost" : "0.0.0.0"

    ENV.fetch("BINDING", default_host)
  end
end

#perform

[ GitHub ]

  
# File 'railties/lib/rails/commands/server/server_command.rb', line 132

def perform
  set_application_directory!
  prepare_restart

  Rails::Server.new(server_options).tap do |server|
    # Require application after server sets environment to propagate
    # the --environment option.
    require APP_PATH
    Dir.chdir(Rails.application.root)

    if server.serveable?
      print_boot_information(server.server, server.served_url)
      after_stop_callback = -> { say "Exiting" unless options[:daemon] }
      server.start(after_stop_callback)
    else
      say rack_server_suggestion(options[:using])
    end
  end
end

#pid (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/server/server_command.rb', line 245

def pid
  default_pidfile = environment == "development" ? DEFAULT_PIDFILE : nil
  pid = options[:pid] || ENV["PIDFILE"] || default_pidfile
  File.expand_path(pid) if pid
end

#port (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/server/server_command.rb', line 213

def port
  options[:port] || ENV.fetch("PORT", DEFAULT_PORT).to_i
end

#prepare_restart (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/server/server_command.rb', line 251

def prepare_restart
  FileUtils.rm_f(pid) if pid && options[:restart]
end

#rack_server_suggestion(server) (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/server/server_command.rb', line 255

def rack_server_suggestion(server)
  if server.nil?
    <<~MSG
      Could not find a server gem. Maybe you need to add one to the Gemfile?

        gem "#{RECOMMENDED_SERVER}"

      Run `#{executable} --help` for more options.
    MSG
  elsif server.in?(RACK_HANDLER_GEMS)
    <<~MSG
      Could not load server "#{server}". Maybe you need to the add it to the Gemfile?

        gem "#{server}"

      Run `#{executable} --help` for more options.
    MSG
  else
    error = CorrectableNameError.new("Could not find server '#{server}'.", server, RACK_HANDLERS)
    <<~MSG
      #{error.detailed_message}
      Run `#{executable} --help` for more options.
    MSG
  end
end

#restart_command (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/server/server_command.rb', line 231

def restart_command
  "#{executable} #{@original_options.join(" ")} --restart"
end

#user_supplied_options (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/server/server_command.rb', line 173

def user_supplied_options
  @user_supplied_options ||= begin
    # Convert incoming options array to a hash of flags
    #   ["-p3001", "-C", "--binding", "127.0.0.1"] # => {"-p"=>true, "-C"=>true, "--binding"=>true}
    user_flag = {}
    @original_options.each do |command|
      if command.start_with?("--")
        option = command.split("=")[0]
        user_flag[option] = true
      elsif command =~ /\A(-.)/
        user_flag[Regexp.last_match[0]] = true
      end
    end

    # Collect all options that the user has explicitly defined so we can
    # differentiate them from defaults
    user_supplied_options = []
    self.class.class_options.select do |key, option|
      if option.aliases.any? { |name| user_flag[name] } || user_flag["--#{option.name}"]
        name = option.name.to_sym
        case name
        when :port
          name = :Port
        when :binding
          name = :Host
        when :dev_caching
          name = :caching
        when :daemonize
          name = :daemon
        end
        user_supplied_options << name
      end
    end
    user_supplied_options << :Host if ENV["HOST"] || ENV["BINDING"]
    user_supplied_options << :Port if ENV["PORT"]
    user_supplied_options << :pid if ENV["PIDFILE"]
    user_supplied_options.uniq
  end
end