Add hook for before_server
This commit is contained in:
parent
a61b2e3b87
commit
0f8ea86119
17
middleman-core/features/cli/preview_server-hook.feature
Normal file
17
middleman-core/features/cli/preview_server-hook.feature
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
Feature: Run preview server before hook
|
||||||
|
|
||||||
|
Scenario: When run
|
||||||
|
Given a fixture app "preview-server-hook-app"
|
||||||
|
And the default aruba timeout is 30 seconds
|
||||||
|
When I run `middleman server --server-name localhost --bind-address 127.0.0.1` interactively
|
||||||
|
And I stop middleman if the output contains:
|
||||||
|
"""
|
||||||
|
### END ###
|
||||||
|
"""
|
||||||
|
Then the output should contain:
|
||||||
|
"""
|
||||||
|
/// 127.0.0.1:4567 ///
|
||||||
|
/// 4567 ///
|
||||||
|
/// localhost ///
|
||||||
|
/// http://localhost:4567 ///
|
||||||
|
"""
|
19
middleman-core/fixtures/preview-server-hook-app/config.rb
Normal file
19
middleman-core/fixtures/preview-server-hook-app/config.rb
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
set :layout, false
|
||||||
|
|
||||||
|
class MyFeature < Middleman::Extension
|
||||||
|
def initialize(app, options_hash = {}, &block)
|
||||||
|
super
|
||||||
|
|
||||||
|
app.before_server do |server_information|
|
||||||
|
puts "/// #{server_information.listeners.first} ///"
|
||||||
|
puts "/// #{server_information.port} ///"
|
||||||
|
puts "/// #{server_information.server_name} ///"
|
||||||
|
puts "/// #{server_information.site_addresses.first} ///"
|
||||||
|
puts "/// ### END ### ///"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
::Middleman::Extensions.register(:my_feature, MyFeature)
|
||||||
|
|
||||||
|
activate :my_feature
|
|
@ -0,0 +1,9 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>preview-server-hook-app</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>preview-server-hook-app</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -216,7 +216,8 @@ module Middleman
|
||||||
:before_shutdown,
|
:before_shutdown,
|
||||||
:before, # Before Rack requests
|
:before, # Before Rack requests
|
||||||
:before_render,
|
:before_render,
|
||||||
:after_render
|
:after_render,
|
||||||
|
:before_server
|
||||||
])
|
])
|
||||||
|
|
||||||
@middleware = Set.new
|
@middleware = Set.new
|
||||||
|
|
|
@ -6,6 +6,7 @@ require 'middleman-core/logger'
|
||||||
require 'middleman-core/rack'
|
require 'middleman-core/rack'
|
||||||
require 'middleman-core/preview_server/server_information'
|
require 'middleman-core/preview_server/server_information'
|
||||||
require 'middleman-core/preview_server/server_url'
|
require 'middleman-core/preview_server/server_url'
|
||||||
|
require 'middleman-core/preview_server/server_information_callback_proxy'
|
||||||
|
|
||||||
# rubocop:disable GlobalVars
|
# rubocop:disable GlobalVars
|
||||||
module Middleman
|
module Middleman
|
||||||
|
@ -55,6 +56,8 @@ module Middleman
|
||||||
# reloading later on.
|
# reloading later on.
|
||||||
::Middleman::Profiling.report('server_start')
|
::Middleman::Profiling.report('server_start')
|
||||||
|
|
||||||
|
app.execute_callbacks(:before_server, [ServerInformationCallbackProxy.new(server_information)])
|
||||||
|
|
||||||
loop do
|
loop do
|
||||||
@webrick.start
|
@webrick.start
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
module Middleman
|
||||||
|
class PreviewServer
|
||||||
|
# This class wraps server information to be used in call back
|
||||||
|
#
|
||||||
|
# * listeners
|
||||||
|
# * port
|
||||||
|
# * server name
|
||||||
|
# * site_addresses
|
||||||
|
#
|
||||||
|
# All information is "dupped" and the callback is not meant to be used to
|
||||||
|
# modify these information.
|
||||||
|
class ServerInformationCallbackProxy
|
||||||
|
attr_reader :server_name, :port, :site_addresses, :listeners
|
||||||
|
|
||||||
|
def initialize(server_information)
|
||||||
|
@listeners = ServerUrl.new(
|
||||||
|
hosts: server_information.listeners,
|
||||||
|
port: server_information.port,
|
||||||
|
https: server_information.https?,
|
||||||
|
format_output: false
|
||||||
|
).to_bind_addresses
|
||||||
|
|
||||||
|
@port = server_information.port
|
||||||
|
@server_name = server_information.server_name.dup unless server_information.server_name == nil
|
||||||
|
|
||||||
|
@site_addresses = ServerUrl.new(
|
||||||
|
hosts: server_information.site_addresses,
|
||||||
|
port: server_information.port,
|
||||||
|
https: server_information.https?,
|
||||||
|
format_output: false
|
||||||
|
).to_urls
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -6,7 +6,7 @@ module Middleman
|
||||||
class ServerUrl
|
class ServerUrl
|
||||||
private
|
private
|
||||||
|
|
||||||
attr_reader :hosts, :port, :https
|
attr_reader :hosts, :port, :https, :format_output
|
||||||
|
|
||||||
public
|
public
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ module Middleman
|
||||||
@hosts = opts.fetch(:hosts)
|
@hosts = opts.fetch(:hosts)
|
||||||
@port = opts.fetch(:port)
|
@port = opts.fetch(:port)
|
||||||
@https = opts.fetch(:https, false)
|
@https = opts.fetch(:https, false)
|
||||||
|
@format_output = opts.fetch(:format_output, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return bind addresses
|
# Return bind addresses
|
||||||
|
@ -21,7 +22,11 @@ module Middleman
|
||||||
# @return [Array]
|
# @return [Array]
|
||||||
# List of bind addresses of format host:port
|
# List of bind addresses of format host:port
|
||||||
def to_bind_addresses
|
def to_bind_addresses
|
||||||
|
if format_output
|
||||||
hosts.map { |l| format('"%s:%s"', l.to_s, port) }
|
hosts.map { |l| format('"%s:%s"', l.to_s, port) }
|
||||||
|
else
|
||||||
|
hosts.map { |l| format('%s:%s', l.to_s, port) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return server urls
|
# Return server urls
|
||||||
|
@ -29,7 +34,11 @@ module Middleman
|
||||||
# @return [Array]
|
# @return [Array]
|
||||||
# List of urls of format http://host:port
|
# List of urls of format http://host:port
|
||||||
def to_urls
|
def to_urls
|
||||||
|
if format_output
|
||||||
hosts.map { |l| format('"%s://%s:%s"', https? ? 'https' : 'http', l.to_browser, port) }
|
hosts.map { |l| format('"%s://%s:%s"', https? ? 'https' : 'http', l.to_browser, port) }
|
||||||
|
else
|
||||||
|
hosts.map { |l| format('%s://%s:%s', https? ? 'https' : 'http', l.to_browser, port) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return server config urls
|
# Return server config urls
|
||||||
|
@ -37,7 +46,11 @@ module Middleman
|
||||||
# @return [Array]
|
# @return [Array]
|
||||||
# List of urls of format http://host:port/__middleman
|
# List of urls of format http://host:port/__middleman
|
||||||
def to_config_urls
|
def to_config_urls
|
||||||
|
if format_output
|
||||||
hosts.map { |l| format('"%s://%s:%s/__middleman"', https? ? 'https' : 'http', l.to_browser, port) }
|
hosts.map { |l| format('"%s://%s:%s/__middleman"', https? ? 'https' : 'http', l.to_browser, port) }
|
||||||
|
else
|
||||||
|
hosts.map { |l| format('%s://%s:%s/__middleman', https? ? 'https' : 'http', l.to_browser, port) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
Loading…
Reference in a new issue