Switch AssetHost to be a new-style extension. Setup extensions app scope earlier. Add supports_multiple_instances flag for extensions.
This commit is contained in:
parent
c2e2839b79
commit
b12a7bff3d
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -8,6 +8,7 @@ pkg
|
|||
Gemfile.lock
|
||||
docs
|
||||
.rbenv-*
|
||||
.ruby-version
|
||||
.*.swp
|
||||
build
|
||||
doc
|
||||
|
|
|
@ -37,6 +37,7 @@ module Middleman
|
|||
class << self
|
||||
# @private
|
||||
def registered(app)
|
||||
app.define_hook :initialized
|
||||
app.define_hook :after_configuration
|
||||
app.define_hook :before_configuration
|
||||
app.define_hook :build_config
|
||||
|
@ -68,7 +69,6 @@ module Middleman
|
|||
# @param [Hash] options Per-extension options hash
|
||||
# @return [void]
|
||||
def register(extension, options={}, &block)
|
||||
if extension.instance_of? Module
|
||||
extend extension
|
||||
if extension.respond_to?(:registered)
|
||||
if extension.method(:registered).arity === 1
|
||||
|
@ -78,9 +78,6 @@ module Middleman
|
|||
end
|
||||
end
|
||||
extension
|
||||
elsif extension.instance_of?(Class) && extension.ancestors.include?(::Middleman::Extension)
|
||||
extension.new(self, options, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -105,7 +102,22 @@ module Middleman
|
|||
logger.error "== Unknown Extension: #{ext}"
|
||||
else
|
||||
logger.debug "== Activating: #{ext}"
|
||||
|
||||
if ext_module.instance_of? Module
|
||||
extensions[ext] = self.class.register(ext_module, options, &block)
|
||||
elsif ext_module.instance_of?(Class) && ext_module.ancestors.include?(::Middleman::Extension)
|
||||
if ext_module.supports_multiple_instances?
|
||||
extensions[ext] ||= {}
|
||||
key = "instance_#{extensions[ext].keys.length}"
|
||||
extensions[ext][key] = ext_module.new(self.class, options, &block)
|
||||
else
|
||||
if extensions[ext]
|
||||
logger.error "== #{ext} already activated. Overwriting."
|
||||
end
|
||||
|
||||
extensions[ext] = ext_module.new(self.class, options, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -141,6 +153,8 @@ module Middleman
|
|||
instance_eval File.read(local_config), local_config, 1
|
||||
end
|
||||
|
||||
run_hook :initialized
|
||||
|
||||
run_hook :build_config if build?
|
||||
run_hook :development_config if development?
|
||||
|
||||
|
@ -148,10 +162,16 @@ module Middleman
|
|||
|
||||
logger.debug "Loaded extensions:"
|
||||
self.extensions.each do |ext,_|
|
||||
if ext.is_a?(Hash)
|
||||
ext.each do |k,_|
|
||||
logger.debug "== Extension: #{k}"
|
||||
end
|
||||
else
|
||||
logger.debug "== Extension: #{ext}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require "active_support/core_ext/class/attribute"
|
||||
|
||||
module Middleman
|
||||
|
||||
module Extensions
|
||||
|
@ -102,6 +104,8 @@ module Middleman
|
|||
end
|
||||
|
||||
class Extension
|
||||
class_attribute :supports_multiple_instances, :instance_reader => false, :instance_writer => false
|
||||
|
||||
class << self
|
||||
def config
|
||||
@_config ||= ::Middleman::Configuration::ConfigurationManager.new
|
||||
|
@ -125,10 +129,11 @@ module Middleman
|
|||
yield @options if block_given?
|
||||
|
||||
ext = self
|
||||
klass.after_configuration do
|
||||
klass.initialized do
|
||||
ext.app = self
|
||||
ext.after_configuration
|
||||
end
|
||||
|
||||
klass.after_configuration(&method(:after_configuration))
|
||||
end
|
||||
|
||||
def after_configuration
|
||||
|
|
|
@ -1,7 +1,56 @@
|
|||
Feature: Alternate between multiple asset hosts
|
||||
In order to speed up page loading
|
||||
|
||||
Scenario: Rendering html with the feature enabled
|
||||
Given the Server is running at "asset-host-app"
|
||||
Scenario: Set single host globally
|
||||
Given a fixture app "asset-host-app"
|
||||
And a file named "config.rb" with:
|
||||
"""
|
||||
activate :asset_host
|
||||
set :asset_host, "http://assets1.example.com"
|
||||
"""
|
||||
And the Server is running
|
||||
When I go to "/asset_host.html"
|
||||
Then I should see "http://assets1"
|
||||
When I go to "/stylesheets/asset_host.css"
|
||||
Then I should see "http://assets1"
|
||||
|
||||
Scenario: Set proc host globally
|
||||
Given a fixture app "asset-host-app"
|
||||
And a file named "config.rb" with:
|
||||
"""
|
||||
activate :asset_host
|
||||
set :asset_host do |asset|
|
||||
"http://assets%d.example.com" % (asset.hash % 4)
|
||||
end
|
||||
"""
|
||||
And the Server is running
|
||||
When I go to "/asset_host.html"
|
||||
Then I should see "http://assets"
|
||||
When I go to "/stylesheets/asset_host.css"
|
||||
Then I should see "http://assets"
|
||||
|
||||
Scenario: Set single host with inline-option
|
||||
Given a fixture app "asset-host-app"
|
||||
And a file named "config.rb" with:
|
||||
"""
|
||||
activate :asset_host, :host => "http://assets1.example.com"
|
||||
"""
|
||||
And the Server is running
|
||||
When I go to "/asset_host.html"
|
||||
Then I should see "http://assets1"
|
||||
When I go to "/stylesheets/asset_host.css"
|
||||
Then I should see "http://assets1"
|
||||
|
||||
Scenario: Set proc host with inline-option
|
||||
Given a fixture app "asset-host-app"
|
||||
And a file named "config.rb" with:
|
||||
"""
|
||||
activate :asset_host, :host => Proc.new { |asset|
|
||||
"http://assets%d.example.com" % (asset.hash % 4)
|
||||
}
|
||||
"""
|
||||
And the Server is running
|
||||
When I go to "/asset_host.html"
|
||||
Then I should see "http://assets"
|
||||
When I go to "/stylesheets/asset_host.css"
|
||||
Then I should see "http://assets"
|
|
@ -1,7 +0,0 @@
|
|||
Feature: Alternate between multiple asset hosts
|
||||
In order to speed up page loading
|
||||
|
||||
Scenario: Rendering css with the feature enabled
|
||||
Given the Server is running at "asset-host-app"
|
||||
When I go to "/stylesheets/asset_host.css"
|
||||
Then I should see "http://assets"
|
|
@ -1,6 +0,0 @@
|
|||
set :layout, false
|
||||
|
||||
activate :asset_host
|
||||
set :asset_host do |asset|
|
||||
"http://assets%d.example.com" % (asset.hash % 4)
|
||||
end
|
|
@ -83,7 +83,7 @@ module Middleman
|
|||
# to avoid browser caches failing to update to your new content.
|
||||
Middleman::Extensions.register(:asset_hash) do
|
||||
require "middleman-more/extensions/asset_hash"
|
||||
Middleman::Extensions::AssetHash::Extension
|
||||
Middleman::Extensions::AssetHash
|
||||
end
|
||||
|
||||
# AssetHost allows you to setup multiple domains to host your static
|
||||
|
|
|
@ -52,8 +52,14 @@ module Middleman
|
|||
# No line-comments in test mode (changing paths mess with sha1)
|
||||
compass_config.line_comments = false if ENV["TEST"]
|
||||
|
||||
if config.defines_setting?(:asset_host) && config[:asset_host].is_a?(Proc)
|
||||
compass_config.asset_host(&config[:asset_host])
|
||||
if extensions[:asset_host] && asset_host = extensions[:asset_host].host
|
||||
if asset_host.is_a?(Proc)
|
||||
compass_config.asset_host(&asset_host)
|
||||
else
|
||||
compass_config.asset_host do |asset|
|
||||
asset_host
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
module Middleman
|
||||
module Extensions
|
||||
module AssetHash
|
||||
class Extension < ::Middleman::Extension
|
||||
class AssetHash < ::Middleman::Extension
|
||||
option :exts, %w(.jpg .jpeg .png .gif .js .css .otf .woff .eot .ttf .svg), "List of extensions that get asset hashes appended to them."
|
||||
option :ignore, [], "Regexes of filenames to skip adding asset hashes to"
|
||||
|
||||
|
@ -15,23 +14,11 @@ module Middleman
|
|||
|
||||
def after_configuration
|
||||
# Allow specifying regexes to ignore, plus always ignore apple touch icons
|
||||
ignore = Array(options.ignore) + [/^apple-touch-icon/]
|
||||
@ignore = Array(options.ignore) + [/^apple-touch-icon/]
|
||||
|
||||
app.sitemap.register_resource_list_manipulator(
|
||||
:asset_hash,
|
||||
AssetHashManager.new(app, options.exts, ignore)
|
||||
)
|
||||
app.sitemap.register_resource_list_manipulator(:asset_hash, self)
|
||||
|
||||
app.use Middleware, :exts => options.exts, :middleman_app => app, :ignore => ignore
|
||||
end
|
||||
end
|
||||
|
||||
# Central class for managing asset_hash extension
|
||||
class AssetHashManager
|
||||
def initialize(app, exts, ignore)
|
||||
@app = app
|
||||
@exts = exts
|
||||
@ignore = ignore
|
||||
app.use Middleware, :exts => options.exts, :middleman_app => app, :ignore => @ignore
|
||||
end
|
||||
|
||||
# Update the main sitemap resource list
|
||||
|
@ -50,7 +37,7 @@ module Middleman
|
|||
-1
|
||||
end
|
||||
end.each do |resource|
|
||||
next unless @exts.include? resource.ext
|
||||
next unless options.exts.include? resource.ext
|
||||
next if @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) }
|
||||
|
||||
# Render through the Rack interface so middleware and mounted apps get a shot
|
||||
|
@ -89,7 +76,7 @@ module Middleman
|
|||
|
||||
if body
|
||||
# TODO: This regex will change some paths in plan HTML (not in a tag) - is that OK?
|
||||
body.gsub! /([=\'\"\(]\s*)([^\s\'\"\)]+(#{@exts_regex_text}))/ do |match|
|
||||
body.gsub!(/([=\'\"\(]\s*)([^\s\'\"\)]+(#{@exts_regex_text}))/) do |match|
|
||||
opening_character = $1
|
||||
asset_path = $2
|
||||
|
||||
|
@ -116,7 +103,6 @@ module Middleman
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -3,24 +3,19 @@ module Middleman
|
|||
module Extensions
|
||||
|
||||
# Asset Host module
|
||||
module AssetHost
|
||||
class AssetHost < ::Middleman::Extension
|
||||
option :host, nil, 'The asset host to use, or false for no asset host, or a Proc to determine asset host'
|
||||
|
||||
# Setup extension
|
||||
class << self
|
||||
def initialize(app, options_hash={}, &block)
|
||||
super
|
||||
|
||||
# Once registered
|
||||
def registered(app, options={})
|
||||
app.config.define_setting :asset_host, false, 'The asset host to use, or false for no asset host, or a Proc to determine asset host'
|
||||
|
||||
if options[:host]
|
||||
config[:asset_host] = options[:host]
|
||||
end
|
||||
|
||||
# Include methods
|
||||
# Backwards compatible API
|
||||
app.config.define_setting :asset_host, nil, 'The asset host to use, or false for no asset host, or a Proc to determine asset host'
|
||||
app.send :include, InstanceMethods
|
||||
end
|
||||
|
||||
alias :included :registered
|
||||
def host
|
||||
app.config[:asset_host] || options[:host]
|
||||
end
|
||||
|
||||
# Asset Host Instance Methods
|
||||
|
@ -32,13 +27,15 @@ module Middleman
|
|||
# @param [String] prefix
|
||||
# @return [String]
|
||||
def asset_url(path, prefix="")
|
||||
original_output = super
|
||||
return original_output unless config[:asset_host]
|
||||
controller = extensions[:asset_host]
|
||||
|
||||
asset_prefix = if config[:asset_host].is_a?(Proc)
|
||||
config[:asset_host].call(original_output)
|
||||
elsif config[:asset_host].is_a?(String)
|
||||
config[:asset_host]
|
||||
original_output = super
|
||||
return original_output unless controller.host
|
||||
|
||||
asset_prefix = if controller.host.is_a?(Proc)
|
||||
controller.host.call(original_output)
|
||||
elsif controller.host.is_a?(String)
|
||||
controller.host
|
||||
end
|
||||
|
||||
File.join(asset_prefix, original_output)
|
||||
|
|
Loading…
Reference in a new issue