Convert FileWatcher to a real extension

This commit is contained in:
Ben Hollis 2014-05-25 18:03:37 -07:00
parent 67bb394852
commit e649bc2809
3 changed files with 27 additions and 31 deletions

View file

@ -142,9 +142,6 @@ module Middleman
# Handle exceptions # Handle exceptions
include Middleman::CoreExtensions::ShowExceptions include Middleman::CoreExtensions::ShowExceptions
# Add Watcher Callbacks
include Middleman::CoreExtensions::FileWatcher
# Activate Data package # Activate Data package
include Middleman::CoreExtensions::Data include Middleman::CoreExtensions::Data
@ -191,6 +188,7 @@ module Middleman
# Parse YAML from templates. Must be before sitemap so sitemap # Parse YAML from templates. Must be before sitemap so sitemap
# extensions see updated frontmatter! # extensions see updated frontmatter!
activate :front_matter activate :front_matter
activate :file_watcher
# Initialize the Sitemap # Initialize the Sitemap
@sitemap = ::Middleman::Sitemap::Store.new(self) @sitemap = ::Middleman::Sitemap::Store.new(self)

View file

@ -1,5 +1,8 @@
# File Change Notifier # File Change Notifier
require 'middleman-core/core_extensions/file_watcher' Middleman::Extensions.register :file_watcher do
require 'middleman-core/core_extensions/file_watcher'
Middleman::CoreExtensions::FileWatcher
end
# Data looks at the data/ folder for YAML files and makes them available # Data looks at the data/ folder for YAML files and makes them available
# to dynamic requests. # to dynamic requests.

View file

@ -1,7 +1,11 @@
# API for watching file change events require 'pathname'
require 'set'
module Middleman module Middleman
module CoreExtensions module CoreExtensions
module FileWatcher # API for watching file change events
class FileWatcher < Extension
# Regexes in this list will filter out filenames of files that shouldn't cause file change notifications.
IGNORE_LIST = [ IGNORE_LIST = [
/^bin(\/|$)/, /^bin(\/|$)/,
/^\.bundle(\/|$)/, /^\.bundle(\/|$)/,
@ -20,39 +24,30 @@ module Middleman
/^tmp\// /^tmp\//
] ]
# Setup extension def initialize(app, options_hash={}, &block)
class << self super
# Once registered
def included(app)
require 'pathname'
require 'set'
app.send :include, InstanceMethods app.config.define_setting :file_watcher_ignore, IGNORE_LIST, 'Regexes for paths that should be ignored when they change.'
app.config.define_setting :file_watcher_ignore, IGNORE_LIST, 'Regexes for paths that should be ignored when they change.' # Directly include the #files method instead of using helpers so that this is available immediately
app.send :include, InstanceMethods
# Before parsing config, load the data/ directory end
app.before_configuration do
files.reload_path(config[:data_dir]) # Before parsing config, load the data/ directory
end def before_configuration
app.files.reload_path(app.config[:data_dir])
app.after_configuration do end
config[:file_watcher_ignore] << %r{^#{config[:build_dir]}(\/|$)}
end def after_configuration
app.config[:file_watcher_ignore] << %r{^#{app.config[:build_dir]}(\/|$)}
# After config, load everything else app.files.reload_path('.')
app.ready do
files.reload_path('.')
end
end
end end
# Instance methods
module InstanceMethods module InstanceMethods
# Access the file api # Access the file api
# @return [Middleman::CoreExtensions::FileWatcher::API] # @return [Middleman::CoreExtensions::FileWatcher::API]
def files def files
@_files_api ||= API.new(self) @files_api ||= API.new(self)
end end
end end