From 0ed1a73450ddad8009bad53f7491d2ae140cefec Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Tue, 8 Nov 2011 14:57:55 -0800 Subject: [PATCH] begin migration to hooks callback library --- lib/middleman.rb | 5 ++- lib/middleman/base.rb | 21 +++++++++-- lib/middleman/core_extensions/file_watcher.rb | 20 ++++------- lib/middleman/core_extensions/sitemap.rb | 36 ++++++++++--------- lib/middleman/guard.rb | 12 ++++--- middleman-x86-mingw32.gemspec | 2 +- middleman.gemspec | 2 +- 7 files changed, 56 insertions(+), 42 deletions(-) diff --git a/lib/middleman.rb b/lib/middleman.rb index 06fea292..c2971225 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -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) diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index 0d01c7d0..63265803 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -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 diff --git a/lib/middleman/core_extensions/file_watcher.rb b/lib/middleman/core_extensions/file_watcher.rb index b2c60c1f..6fd64eeb 100644 --- a/lib/middleman/core_extensions/file_watcher.rb +++ b/lib/middleman/core_extensions/file_watcher.rb @@ -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 \ No newline at end of file diff --git a/lib/middleman/core_extensions/sitemap.rb b/lib/middleman/core_extensions/sitemap.rb index 6dbfd6f2..5f0fd832 100644 --- a/lib/middleman/core_extensions/sitemap.rb +++ b/lib/middleman/core_extensions/sitemap.rb @@ -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 diff --git a/lib/middleman/guard.rb b/lib/middleman/guard.rb index 2138a5db..878beadc 100644 --- a/lib/middleman/guard.rb +++ b/lib/middleman/guard.rb @@ -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 diff --git a/middleman-x86-mingw32.gemspec b/middleman-x86-mingw32.gemspec index af442b24..dc674df7 100644 --- a/middleman-x86-mingw32.gemspec +++ b/middleman-x86-mingw32.gemspec @@ -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") diff --git a/middleman.gemspec b/middleman.gemspec index ce49a3af..a6e7d0a2 100644 --- a/middleman.gemspec +++ b/middleman.gemspec @@ -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"])