From 0ca6c37e5dd426876774587d4a418727849c0824 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sat, 5 Jul 2014 16:44:04 -0700 Subject: [PATCH] Remove App include in File Watcher --- middleman-cli/lib/middleman-cli/build.rb | 2 +- .../lib/middleman-core/core_extensions.rb | 12 +++++----- .../middleman-core/core_extensions/data.rb | 23 +++++++----------- .../core_extensions/file_watcher.rb | 24 +++++-------------- .../core_extensions/front_matter.rb | 9 ++++--- .../middleman-core/core_extensions/i18n.rb | 8 +++---- .../lib/middleman-core/extension.rb | 2 ++ .../sitemap/extensions/on_disk.rb | 4 ++-- .../step_definitions/middleman_steps.rb | 4 ++-- 9 files changed, 35 insertions(+), 53 deletions(-) diff --git a/middleman-cli/lib/middleman-cli/build.rb b/middleman-cli/lib/middleman-cli/build.rb index a08dd4d9..e6837604 100644 --- a/middleman-cli/lib/middleman-cli/build.rb +++ b/middleman-cli/lib/middleman-cli/build.rb @@ -192,7 +192,7 @@ module Middleman::Cli logger.debug '== Checking 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! # Sort paths to be built by the above order. This is primarily so Compass can diff --git a/middleman-core/lib/middleman-core/core_extensions.rb b/middleman-core/lib/middleman-core/core_extensions.rb index 1177eefe..ff7b0088 100644 --- a/middleman-core/lib/middleman-core/core_extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions.rb @@ -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 Middleman::Extensions.register :front_matter, auto_activate: :before_sitemap do require 'middleman-core/core_extensions/front_matter' @@ -17,12 +23,6 @@ Middleman::Extensions.register :show_exceptions, auto_activate: :before_configur Middleman::CoreExtensions::ShowExceptions 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 Middleman::Extensions.register :external_helpers, auto_activate: :before_configuration do require 'middleman-core/core_extensions/external_helpers' diff --git a/middleman-core/lib/middleman-core/core_extensions/data.rb b/middleman-core/lib/middleman-core/core_extensions/data.rb index 29bf3261..346d64f1 100644 --- a/middleman-core/lib/middleman-core/core_extensions/data.rb +++ b/middleman-core/lib/middleman-core/core_extensions/data.rb @@ -6,30 +6,23 @@ module Middleman # The data extension parses YAML and JSON files in the `data/` directory # and makes them available to `config.rb`, templates and extensions class Data < Extension - # The regex which tells Middleman which files are for data - MATCHER = /[\w-]+\.(yml|yaml|json)$/ - attr_reader :data_store - def initialize(app, options_hash={}, &block) - super - - @data_store = DataStore.new(app) - end - def before_configuration + @data_store = DataStore.new(app) app.config.define_setting :data_dir, 'data', 'The directory data files are stored in' + 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 # parsing config.rb - app.files.changed MATCHER do |file| - extensions[:data].data_store.touch_file(file) if file.start_with?("#{config[:data_dir]}/") - end + file_watcher.changed(data_file_matcher, &app.extensions[:data].data_store.method(:touch_file)) + file_watcher.deleted(data_file_matcher, &app.extensions[:data].data_store.method(:remove_file)) - app.files.deleted MATCHER do |file| - extensions[:data].data_store.remove_file(file) if file.start_with?("#{config[:data_dir]}/") - end + file_watcher.reload_path(app.config[:data_dir]) end helpers do diff --git a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb index 2b4e7002..8a3e0a9c 100644 --- a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb +++ b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb @@ -24,31 +24,19 @@ module Middleman /^tmp\// ] - def initialize(app, options_hash={}, &block) - 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 + attr_reader :api # Before parsing config, load the data/ directory 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 def after_configuration app.config[:file_watcher_ignore] << %r{^#{app.config[:build_dir]}(\/|$)} - app.files.reload_path('.') - end - - module InstanceMethods - # Access the file api - # @return [Middleman::CoreExtensions::FileWatcher::API] - def files - @files_api ||= API.new(self) - end + @api.reload_path('.') end # Core File Change API class diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index 13bb3361..712d5d6c 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -27,9 +27,8 @@ module Middleman::CoreExtensions end def before_configuration - ext = self - app.files.changed { |file| ext.clear_data(file) } - app.files.deleted { |file| ext.clear_data(file) } + file_watcher.changed(&method(:clear_data)) + file_watcher.deleted(&method(:clear_data)) end # Modify each resource to add data & options from frontmatter. @@ -74,7 +73,7 @@ module Middleman::CoreExtensions @cache[p] ||= begin 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") data = external_data.deep_merge(data) end @@ -155,7 +154,7 @@ module Middleman::CoreExtensions 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) diff --git a/middleman-core/lib/middleman-core/core_extensions/i18n.rb b/middleman-core/lib/middleman-core/core_extensions/i18n.rb index b33322a7..3764fdc1 100644 --- a/middleman-core/lib/middleman-core/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-core/core_extensions/i18n.rb @@ -27,7 +27,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension end 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_regex = convert_glob_to_regex(@locales_glob) @@ -42,8 +42,8 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension # Don't output localizable files app.ignore File.join(options[:templates_dir], '**') - app.files.changed(&method(:on_file_changed)) - app.files.deleted(&method(:on_file_changed)) + file_watcher.changed(&method(:on_file_changed)) + file_watcher.deleted(&method(:on_file_changed)) end helpers do @@ -118,7 +118,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension if options[:langs] Array(options[:langs]).map(&:to_sym) 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) end diff --git a/middleman-core/lib/middleman-core/extension.rb b/middleman-core/lib/middleman-core/extension.rb index d2823632..96186848 100644 --- a/middleman-core/lib/middleman-core/extension.rb +++ b/middleman-core/lib/middleman-core/extension.rb @@ -178,6 +178,8 @@ module Middleman # @return [void] def_delegator :"::Middleman::Extension", :after_extension_activated + def_delegator :"@app.extensions[:file_watcher]", :api, :file_watcher + # Extensions are instantiated when they are activated. # @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}. diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb index 379bb996..a12ecf44 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb @@ -17,12 +17,12 @@ module Middleman @app.before_configuration do # Register file change callback - files.changed do |file| + extensions[:file_watcher].api.changed do |file| scoped_self.touch_file(file) end # Register file delete callback - files.deleted do |file| + extensions[:file_watcher].api.deleted do |file| scoped_self.remove_file(file) end end diff --git a/middleman-core/lib/middleman-core/step_definitions/middleman_steps.rb b/middleman-core/lib/middleman-core/step_definitions/middleman_steps.rb index 70dbcf44..b2101df4 100644 --- a/middleman-core/lib/middleman-core/step_definitions/middleman_steps.rb +++ b/middleman-core/lib/middleman-core/step_definitions/middleman_steps.rb @@ -9,9 +9,9 @@ Then /^the file "([^\"]*)" is removed$/ do |path| end Then /^the file "([^\"]*)" did change$/ do |path| - @server_inst.files.did_change(path) + @server_inst.extensions[:file_watcher].api.did_change(path) end Then /^the file "([^\"]*)" did delete$/ do |path| - @server_inst.files.did_delete(path) + @server_inst.extensions[:file_watcher].api.did_delete(path) end