From 26c6f453f3355327039a565e1183799ae7a1b9c5 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Thu, 26 Feb 2015 17:08:40 -0800 Subject: [PATCH] Add file watcher :only option --- middleman-cli/lib/middleman-cli/build.rb | 2 +- middleman-core/lib/middleman-core/builder.rb | 6 +++++- .../lib/middleman-core/core_extensions/data.rb | 2 +- .../lib/middleman-core/core_extensions/i18n.rb | 2 +- middleman-core/lib/middleman-core/extension.rb | 2 ++ middleman-core/lib/middleman-core/preview_server.rb | 4 +--- .../lib/middleman-core/sources/source_watcher.rb | 11 +++++++++-- 7 files changed, 20 insertions(+), 9 deletions(-) diff --git a/middleman-cli/lib/middleman-cli/build.rb b/middleman-cli/lib/middleman-cli/build.rb index 42510819..46e30f2e 100644 --- a/middleman-cli/lib/middleman-cli/build.rb +++ b/middleman-cli/lib/middleman-cli/build.rb @@ -59,7 +59,7 @@ module Middleman::Cli glob: options['glob'], clean: options['clean'], parallel: options['parallel']) - + builder.thor = self builder.on_build_event(&method(:on_event)) if builder.run! diff --git a/middleman-core/lib/middleman-core/builder.rb b/middleman-core/lib/middleman-core/builder.rb index 17cc589e..9ea50f0d 100644 --- a/middleman-core/lib/middleman-core/builder.rb +++ b/middleman-core/lib/middleman-core/builder.rb @@ -12,6 +12,9 @@ module Middleman # Make app & events available to `after_build` callbacks. attr_reader :app, :events + # Reference to the Thor class. + attr_accessor :thor + # Logger comes from App. def_delegator :@app, :logger @@ -130,7 +133,8 @@ module Middleman def write_tempfile(output_file, contents) file = Tempfile.new([ File.basename(output_file), - File.extname(output_file)]) + File.extname(output_file) + ]) file.binmode file.write(contents) file.close diff --git a/middleman-core/lib/middleman-core/core_extensions/data.rb b/middleman-core/lib/middleman-core/core_extensions/data.rb index 6ec89903..ba0cba70 100644 --- a/middleman-core/lib/middleman-core/core_extensions/data.rb +++ b/middleman-core/lib/middleman-core/core_extensions/data.rb @@ -29,7 +29,7 @@ module Middleman # Tell the file watcher to observe the :data_dir @watcher = app.files.watch :data, path: File.join(app.root, dir), - ignore: proc { |f| !DATA_FILE_MATCHER.match(f[:relative_path]) } + only: DATA_FILE_MATCHER # Setup data files before anything else so they are available when # parsing config.rb diff --git a/middleman-core/lib/middleman-core/core_extensions/i18n.rb b/middleman-core/lib/middleman-core/core_extensions/i18n.rb index 35795580..d8c72503 100644 --- a/middleman-core/lib/middleman-core/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-core/core_extensions/i18n.rb @@ -19,7 +19,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension # Tell the file watcher to observe the :data_dir app.files.watch :locales, path: File.join(app.root, locales_file_path), - ignore: proc { |f| !(/.*(rb|yml|yaml)$/.match(f[:relative_path])) } + only: /.*(rb|yml|yaml)$/ # Setup data files before anything else so they are available when # parsing config.rb diff --git a/middleman-core/lib/middleman-core/extension.rb b/middleman-core/lib/middleman-core/extension.rb index 203abafb..b900c430 100644 --- a/middleman-core/lib/middleman-core/extension.rb +++ b/middleman-core/lib/middleman-core/extension.rb @@ -283,6 +283,8 @@ module Middleman @app.after_build do |builder| if ext.method(:after_build).arity == 1 ext.after_build(builder) + elsif ext.method(:after_build).arity == 2 + ext.after_build(builder, builder.thor) else ext.after_build end diff --git a/middleman-core/lib/middleman-core/preview_server.rb b/middleman-core/lib/middleman-core/preview_server.rb index 5b77bbed..0514c58b 100644 --- a/middleman-core/lib/middleman-core/preview_server.rb +++ b/middleman-core/lib/middleman-core/preview_server.rb @@ -114,9 +114,7 @@ module Middleman # config.rb files.watch :reload, path: root, - ignored: proc { |file| - match_against.none? { |m| file[:relative_path].to_s.match(m) } - } + only: match_against end end diff --git a/middleman-core/lib/middleman-core/sources/source_watcher.rb b/middleman-core/lib/middleman-core/sources/source_watcher.rb index c6bf167b..e8fc2f62 100644 --- a/middleman-core/lib/middleman-core/sources/source_watcher.rb +++ b/middleman-core/lib/middleman-core/sources/source_watcher.rb @@ -47,6 +47,7 @@ module Middleman @validator = options.fetch(:validator, proc { true }) @ignored = options.fetch(:ignored, proc { false }) + @only = Array(options.fetch(:only, [])) @disable_watcher = app.build? || @parent.options.fetch(:disable_watcher, false) @force_polling = @parent.options.fetch(:force_polling, false) @@ -132,6 +133,8 @@ module Middleman @listener = ::Listen.to(@directory.to_s, config, &method(:on_listener_change)) @listener.start + + @listener.only(@only) unless @only.empty? end # Stop the listener. @@ -259,9 +262,13 @@ module Middleman # @return [Boolean] Contract IsA['Middleman::SourceFile'] => Bool def valid?(file) - @validator.call(file) && - !globally_ignored?(file) && + globally_valid = @validator.call(file) && !globally_ignored?(file) + + globally_valid && if @only.empty? !@ignored.call(file) + else + @only.any? { |reg| reg.match(file[:relative_path].to_s) } + end end # Convert a path to a file resprentation.