Merge pull request #1531 from middleman/backport-407a17b

Backport "Fix port suggestion from unused ports"
This commit is contained in:
Eliott Appleford 2015-05-28 09:38:13 +01:00
commit 3b88496803

View file

@ -222,21 +222,7 @@ module Middleman
begin begin
::WEBrick::HTTPServer.new(http_opts) ::WEBrick::HTTPServer.new(http_opts)
rescue Errno::EADDRINUSE rescue Errno::EADDRINUSE
attempts_left -= 1 logger.error "== Port #{port} is unavailable. Either close the instance of Middleman already running on #{port} or start this Middleman on a new port with: --port=#{unused_tcp_port}"
tried_ports << port
if attempts_left > 0
logger.error %(== Port #{port} is unavailable. Trying port #{port + 1} next.)
@port += 1
http_opts[:Port] = @port
retry
end
ports_sentence = tried_ports.to_sentence
logger.error %(== Ports #{ports_sentence} are unavailable. Either close the Middleman servers already running on ports #{ports_sentence} or start this Middleman server on a another port with: "middleman server --port=#{port + 1}".)
exit(1) exit(1)
end end
end end
@ -334,6 +320,15 @@ module Middleman
ip = Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? } ip = Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }
ip ? ip.ip_address : '127.0.0.1' ip ? ip.ip_address : '127.0.0.1'
end end
# Returns unused TCP port
# @return [Fixnum]
def unused_tcp_port
server = TCPServer.open(0)
port = server.addr[1]
server.close
port
end
end end
class FilteredWebrickLog < ::WEBrick::Log class FilteredWebrickLog < ::WEBrick::Log