begin migration to hooks callback library

This commit is contained in:
Thomas Reynolds 2011-11-08 14:57:55 -08:00
parent 19e566f6a5
commit 0ed1a73450
7 changed files with 56 additions and 42 deletions

View file

@ -194,9 +194,8 @@ module Middleman
:AccessLog => []
}
app_class = options[:app] ||= ::Middleman.server
app_class.set :environment, options[:environment].to_sym
opts[:app] = app_class.new
app_class = options[:app] ||= ::Middleman.server.new
opts[:app] = app_class
opts[:server] = 'thin'
server = ::Rack::Server.new(opts)

View file

@ -1,6 +1,7 @@
# require 'sinatra/synchrony'
require "i18n"
require "hooks"
require "active_support"
require "active_support/json"
@ -9,8 +10,8 @@ require "active_support/core_ext/class/attribute_accessors"
module Middleman::Base
class << self
def registered(app)
# app.register ::Sinatra::Synchrony
app.send :include, ::Hooks
app.define_hook :initialized
app.extend ClassMethods
app.send :include, InstanceMethods
@ -19,6 +20,7 @@ module Middleman::Base
app.set :root, Dir.pwd
app.set :sessions, false
app.set :logging, false
app.set :protection, false
app.set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development
# Middleman-specific options
@ -155,9 +157,24 @@ module Middleman::Base
# Convenience method to check if we're in build mode
def build?; environment == :build; end
# Creates a Rack::Builder instance with all the middleware set up and
# an instance of this class as end point.
def build_new(inst=false)
builder = Rack::Builder.new
setup_default_middleware builder
setup_middleware builder
builder.run inst || new!
builder.to_app
end
end
module InstanceMethods
def initialize(*args)
super
run_hook :initialized, settings
end
def forward
raise ::Sinatra::NotFound
end

View file

@ -1,29 +1,21 @@
module Middleman::CoreExtensions::FileWatcher
class << self
def registered(app)
app.set :run_after_file_change, []
app.set :run_after_file_delete, []
app.define_hook :file_changed
app.define_hook :file_deleted
app.extend ClassMethods
app.send :include, InstanceMethods
end
alias :included :registered
end
module ClassMethods
module InstanceMethods
def file_did_change(path)
settings.run_after_file_change.each { |block| block.call(path) }
end
def on_file_change(&block)
settings.run_after_file_change << block
run_hook :file_changed, path
end
def file_did_delete(path)
settings.run_after_file_delete.each { |block| block.call(path) }
end
def on_file_delete(&block)
settings.run_after_file_delete << block
run_hook :file_deleted, path
end
end
end

View file

@ -3,35 +3,37 @@ require 'find'
module Middleman::CoreExtensions::Sitemap
class << self
def registered(app)
app.set :sitemap, SitemapStore.new(app)
sm = SitemapStore.new
app.set :sitemap, sm
app.initialized do |scope|
sm.setup(scope)
end
app.file_changed do |file|
sm.touch_file(file)
end
app.file_deleted do |file|
sm.remove_file(file)
end
end
alias :included :registered
end
class SitemapStore
def initialize(app)
@app = app
def initialize
@map = {}
@ignored_paths = false
@generic_paths = false
@proxied_paths = false
@app.on_file_change do |file|
touch_file(file)
end
@app.on_file_delete do |file|
remove_file(file)
end
setup
# @app.after_configuration do
# sitemap.setup
# end
end
def setup
def setup(app)
@app = app
@source = File.expand_path(@app.views, @app.root)
build_static_map
end

View file

@ -74,12 +74,16 @@ module Guard
end
def server_start
@app = ::Middleman.server
puts "== The Middleman is standing watch on port #{@options[:Port]}"
app = ::Middleman.server
app.set :environment, @options[:environment].to_sym
puts "== The Middleman is standing watch on port #{@options[:Port]||4567}"
@app = app.new!
app_rack = app.build_new(@app)
@server_job = fork do
opts = @options.dup
opts[:app] = @app
opts[:app] = app_rack
::Middleman.start_server(opts)
end
end

View file

@ -37,13 +37,13 @@ Gem::Specification.new do |s|
s.add_dependency("sprockets-sass", ["~> 0.3.0"])
s.add_dependency("padrino-core", ["~> 0.10.5"])
s.add_dependency("padrino-helpers", ["~> 0.10.5"])
s.add_dependency("hooks")
s.add_dependency("eventmachine", ["1.0.0.beta.4.1"])
s.add_dependency("win32-process", ["~> 0.6.5"])
s.add_dependency("rb-fchange")
s.add_dependency("guard", ["~> 0.8.8"])
# s.add_dependency("middleman-livereload", ["~> 0.2.0"])
# Development and test
# s.add_development_dependency("jquery-rails")

View file

@ -26,7 +26,6 @@ Gem::Specification.new do |s|
s.add_dependency("tilt", ["~> 1.3.1"])
s.add_dependency("maruku", ["~> 0.6.0"])
s.add_dependency("sinatra", ["~> 1.3.1"])
# s.add_dependency("sinatra-synchrony", ["~> 0.2.1"])
s.add_dependency("rack-test", ["~> 0.6.1"])
s.add_dependency("uglifier", ["~> 1.0.0"])
s.add_dependency("slim", ["~> 1.0.2"])
@ -39,6 +38,7 @@ Gem::Specification.new do |s|
s.add_dependency("sprockets-sass", ["~> 0.3.0"])
s.add_dependency("padrino-core", ["~> 0.10.5"])
s.add_dependency("padrino-helpers", ["~> 0.10.5"])
s.add_dependency("hooks")
s.add_dependency("guard", ["~> 0.8.8"])
s.add_dependency("eventmachine", ["1.0.0.beta.4"])