Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[close #24435] Send user_supplied_options to server #28137

Merged
merged 1 commit into from
Feb 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 44 additions & 11 deletions railties/lib/rails/commands/server/server_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,55 @@ def perform
no_commands do
def server_options
{
server: @server,
log_stdout: @log_stdout,
Port: port,
Host: host,
DoNotReverseLookup: true,
config: options[:config],
environment: environment,
daemonize: options[:daemon],
pid: pid,
caching: options["dev-caching"],
restart_cmd: restart_command
user_supplied_options: user_supplied_options,
server: @server,
log_stdout: @log_stdout,
Port: port,
Host: host,
DoNotReverseLookup: true,
config: options[:config],
environment: environment,
daemonize: options[:daemon],
pid: pid,
caching: options["dev-caching"],
restart_cmd: restart_command
}
end
end

private
def user_supplied_options
@user_supplied_options ||= begin
# Convert incoming options array to a hash of flags
# ["-p", "3001", "-c", "foo"] # => {"-p" => true, "-c" => true}
user_flag = {}
@original_options.each_with_index { |command, i| user_flag[command] = true if i.even? }

# 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"]
user_supplied_options << :Port if ENV["PORT"]
user_supplied_options.uniq
end
end

def port
ENV.fetch("PORT", options[:port]).to_i
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like ENV["PORT"] should count as user-supplied for our purposes here, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.

end
Expand Down
8 changes: 8 additions & 0 deletions railties/test/commands/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ def test_log_stdout
end
end

def test_records_user_supplied_options
server_options = parse_arguments(["-p", 3001])
assert_equal [:Port], server_options[:user_supplied_options]

server_options = parse_arguments(["--port", 3001])
assert_equal [:Port], server_options[:user_supplied_options]
end

def test_default_options
server = Rails::Server.new
old_default_options = server.default_options
Expand Down