Add file watcher :only option

This commit is contained in:
Thomas Reynolds 2015-02-26 17:08:40 -08:00
parent c94e5d0f4d
commit 26c6f453f3
7 changed files with 20 additions and 9 deletions

View file

@ -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!

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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.