Remove App include in File Watcher

This commit is contained in:
Thomas Reynolds 2014-07-05 16:44:04 -07:00
parent 60bbe44e0e
commit 0ca6c37e5d
9 changed files with 35 additions and 53 deletions

View file

@ -192,7 +192,7 @@ module Middleman::Cli
logger.debug '== Checking for generated images' logger.debug '== Checking for generated images'
# Double-check for generated images # Double-check for generated images
@app.files.find_new_files((@source_dir + @app.config[:images_dir]).relative_path_from(@app.root_path)) @app.extensions[:file_watcher].api.find_new_files((@source_dir + @app.config[:images_dir]).relative_path_from(@app.root_path))
@app.sitemap.ensure_resource_list_updated! @app.sitemap.ensure_resource_list_updated!
# Sort paths to be built by the above order. This is primarily so Compass can # Sort paths to be built by the above order. This is primarily so Compass can

View file

@ -1,3 +1,9 @@
# File Change Notifier
Middleman::Extensions.register :file_watcher, auto_activate: :before_sitemap do
require 'middleman-core/core_extensions/file_watcher'
Middleman::CoreExtensions::FileWatcher
end
# Parse YAML from templates # Parse YAML from templates
Middleman::Extensions.register :front_matter, auto_activate: :before_sitemap do Middleman::Extensions.register :front_matter, auto_activate: :before_sitemap do
require 'middleman-core/core_extensions/front_matter' require 'middleman-core/core_extensions/front_matter'
@ -17,12 +23,6 @@ Middleman::Extensions.register :show_exceptions, auto_activate: :before_configur
Middleman::CoreExtensions::ShowExceptions Middleman::CoreExtensions::ShowExceptions
end end
# File Change Notifier
Middleman::Extensions.register :file_watcher, auto_activate: :before_sitemap do
require 'middleman-core/core_extensions/file_watcher'
Middleman::CoreExtensions::FileWatcher
end
# External helpers looks in the helpers/ folder for helper modules # External helpers looks in the helpers/ folder for helper modules
Middleman::Extensions.register :external_helpers, auto_activate: :before_configuration do Middleman::Extensions.register :external_helpers, auto_activate: :before_configuration do
require 'middleman-core/core_extensions/external_helpers' require 'middleman-core/core_extensions/external_helpers'

View file

@ -6,30 +6,23 @@ module Middleman
# The data extension parses YAML and JSON files in the `data/` directory # The data extension parses YAML and JSON files in the `data/` directory
# and makes them available to `config.rb`, templates and extensions # and makes them available to `config.rb`, templates and extensions
class Data < Extension class Data < Extension
# The regex which tells Middleman which files are for data
MATCHER = /[\w-]+\.(yml|yaml|json)$/
attr_reader :data_store attr_reader :data_store
def initialize(app, options_hash={}, &block)
super
@data_store = DataStore.new(app)
end
def before_configuration def before_configuration
@data_store = DataStore.new(app)
app.config.define_setting :data_dir, 'data', 'The directory data files are stored in' app.config.define_setting :data_dir, 'data', 'The directory data files are stored in'
app.add_to_config_context :data, &method(:data_store) app.add_to_config_context :data, &method(:data_store)
# The regex which tells Middleman which files are for data
data_file_matcher = /#{app.config[:data_dir]}\/(.*?)[\w-]+\.(yml|yaml|json)$/
# Setup data files before anything else so they are available when # Setup data files before anything else so they are available when
# parsing config.rb # parsing config.rb
app.files.changed MATCHER do |file| file_watcher.changed(data_file_matcher, &app.extensions[:data].data_store.method(:touch_file))
extensions[:data].data_store.touch_file(file) if file.start_with?("#{config[:data_dir]}/") file_watcher.deleted(data_file_matcher, &app.extensions[:data].data_store.method(:remove_file))
end
app.files.deleted MATCHER do |file| file_watcher.reload_path(app.config[:data_dir])
extensions[:data].data_store.remove_file(file) if file.start_with?("#{config[:data_dir]}/")
end
end end
helpers do helpers do

View file

@ -24,31 +24,19 @@ module Middleman
/^tmp\// /^tmp\//
] ]
def initialize(app, options_hash={}, &block) attr_reader :api
super
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
end
# Before parsing config, load the data/ directory # Before parsing config, load the data/ directory
def before_configuration def before_configuration
app.files.reload_path(app.config[:data_dir]) app.config.define_setting :file_watcher_ignore, IGNORE_LIST, 'Regexes for paths that should be ignored when they change.'
@api = API.new(app)
app.add_to_config_context :files, &method(:api)
end end
def after_configuration def after_configuration
app.config[:file_watcher_ignore] << %r{^#{app.config[:build_dir]}(\/|$)} app.config[:file_watcher_ignore] << %r{^#{app.config[:build_dir]}(\/|$)}
app.files.reload_path('.') @api.reload_path('.')
end
module InstanceMethods
# Access the file api
# @return [Middleman::CoreExtensions::FileWatcher::API]
def files
@files_api ||= API.new(self)
end
end end
# Core File Change API class # Core File Change API class

View file

@ -27,9 +27,8 @@ module Middleman::CoreExtensions
end end
def before_configuration def before_configuration
ext = self file_watcher.changed(&method(:clear_data))
app.files.changed { |file| ext.clear_data(file) } file_watcher.deleted(&method(:clear_data))
app.files.deleted { |file| ext.clear_data(file) }
end end
# Modify each resource to add data & options from frontmatter. # Modify each resource to add data & options from frontmatter.
@ -74,7 +73,7 @@ module Middleman::CoreExtensions
@cache[p] ||= begin @cache[p] ||= begin
data, content = frontmatter_and_content(p) data, content = frontmatter_and_content(p)
if app.files.exists?("#{path}.frontmatter") if file_watcher.exists?("#{path}.frontmatter")
external_data, _ = frontmatter_and_content("#{p}.frontmatter") external_data, _ = frontmatter_and_content("#{p}.frontmatter")
data = external_data.deep_merge(data) data = external_data.deep_merge(data)
end end
@ -155,7 +154,7 @@ module Middleman::CoreExtensions
data = {} data = {}
return [data, nil] if !app.files.exists?(full_path) || ::Middleman::Util.binary?(full_path) return [data, nil] if !file_watcher.exists?(full_path) || ::Middleman::Util.binary?(full_path)
content = File.read(full_path) content = File.read(full_path)

View file

@ -27,7 +27,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
end end
def after_configuration def after_configuration
app.files.reload_path(app.config[:locales_dir] || options[:data]) file_watcher.reload_path(app.config[:locales_dir] || options[:data])
@locales_glob = File.join(app.config[:locales_dir] || options[:data], '**', '*.{rb,yml,yaml}') @locales_glob = File.join(app.config[:locales_dir] || options[:data], '**', '*.{rb,yml,yaml}')
@locales_regex = convert_glob_to_regex(@locales_glob) @locales_regex = convert_glob_to_regex(@locales_glob)
@ -42,8 +42,8 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
# Don't output localizable files # Don't output localizable files
app.ignore File.join(options[:templates_dir], '**') app.ignore File.join(options[:templates_dir], '**')
app.files.changed(&method(:on_file_changed)) file_watcher.changed(&method(:on_file_changed))
app.files.deleted(&method(:on_file_changed)) file_watcher.deleted(&method(:on_file_changed))
end end
helpers do helpers do
@ -118,7 +118,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
if options[:langs] if options[:langs]
Array(options[:langs]).map(&:to_sym) Array(options[:langs]).map(&:to_sym)
else else
known_langs = app.files.known_paths.select do |p| known_langs = file_watcher.known_paths.select do |p|
p.to_s.match(@locales_regex) && (p.to_s.split(File::SEPARATOR).length == 2) p.to_s.match(@locales_regex) && (p.to_s.split(File::SEPARATOR).length == 2)
end end

View file

@ -178,6 +178,8 @@ module Middleman
# @return [void] # @return [void]
def_delegator :"::Middleman::Extension", :after_extension_activated def_delegator :"::Middleman::Extension", :after_extension_activated
def_delegator :"@app.extensions[:file_watcher]", :api, :file_watcher
# Extensions are instantiated when they are activated. # Extensions are instantiated when they are activated.
# @param [Class] klass The Middleman::Application class # @param [Class] klass The Middleman::Application class
# @param [Hash] options_hash The raw options hash. Subclasses should not manipulate this directly - it will be turned into {#options}. # @param [Hash] options_hash The raw options hash. Subclasses should not manipulate this directly - it will be turned into {#options}.

View file

@ -17,12 +17,12 @@ module Middleman
@app.before_configuration do @app.before_configuration do
# Register file change callback # Register file change callback
files.changed do |file| extensions[:file_watcher].api.changed do |file|
scoped_self.touch_file(file) scoped_self.touch_file(file)
end end
# Register file delete callback # Register file delete callback
files.deleted do |file| extensions[:file_watcher].api.deleted do |file|
scoped_self.remove_file(file) scoped_self.remove_file(file)
end end
end end

View file

@ -9,9 +9,9 @@ Then /^the file "([^\"]*)" is removed$/ do |path|
end end
Then /^the file "([^\"]*)" did change$/ do |path| Then /^the file "([^\"]*)" did change$/ do |path|
@server_inst.files.did_change(path) @server_inst.extensions[:file_watcher].api.did_change(path)
end end
Then /^the file "([^\"]*)" did delete$/ do |path| Then /^the file "([^\"]*)" did delete$/ do |path|
@server_inst.files.did_delete(path) @server_inst.extensions[:file_watcher].api.did_delete(path)
end end