middleman/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb

83 lines
2.1 KiB
Ruby
Raw Normal View History

2014-07-03 04:04:34 +02:00
require 'middleman-core/contracts'
2014-07-16 03:01:45 +02:00
require 'middleman-core/sources'
module Middleman
module CoreExtensions
# API for watching file change events
class FileWatcher < Extension
2014-07-16 03:01:45 +02:00
# All defined sources.
2015-04-24 19:26:42 +02:00
Contract IsA['Middleman::Sources']
2014-07-16 03:01:45 +02:00
attr_reader :sources
# Make the internal `sources` method available as `app.files`
expose_to_application files: :sources
# Make the internal `sources` method available in config as `files`
expose_to_config files: :sources
2014-07-16 03:01:45 +02:00
# The default list of ignores.
IGNORES = {
emacs_files: /(^|\/)\.?#/,
tilde_files: /~$/,
ds_store: /\.DS_Store$/,
git: /(^|\/)\.git(ignore|modules|\/)/
2016-01-14 20:21:42 +01:00
}.freeze
2014-07-16 03:01:45 +02:00
# Setup the extension.
2014-03-26 00:54:16 +01:00
def initialize(app, config={}, &block)
super
2014-07-16 03:01:45 +02:00
# Setup source collection.
2016-10-27 15:10:28 +02:00
@sources = ::Middleman::Sources.new(app)
2014-07-16 03:01:45 +02:00
# Add default ignores.
IGNORES.each do |key, value|
@sources.ignore key, :all, value
end
# Watch current source.
start_watching(app.config[:source])
2014-03-26 00:54:16 +01:00
end
2014-07-16 03:01:45 +02:00
# Before we config, find initial files.
#
# @return [void]
2015-04-24 19:26:42 +02:00
Contract Any
def before_configuration
@sources.poll_once!
end
2013-06-24 00:15:43 +02:00
2014-07-16 03:01:45 +02:00
# After we config, find new files since config can change paths.
#
# @return [void]
2015-04-24 19:26:42 +02:00
Contract Any
def after_configuration
2016-10-27 15:10:28 +02:00
@watcher.update_config(
disable_watcher: app.config[:watcher_disable],
force_polling: app.config[:watcher_force_polling],
latency: app.config[:watcher_latency],
wait_for_delay: app.config[:watcher_wait_for_delay]
)
2014-07-16 03:01:45 +02:00
if @original_source_dir != app.config[:source]
@watcher.update_path(app.config[:source])
end
2014-07-16 03:01:45 +02:00
@sources.start!
@sources.poll_once!
2014-07-16 03:01:45 +02:00
end
2014-07-16 03:01:45 +02:00
protected
2014-07-16 03:01:45 +02:00
# Watch the source directory.
#
# @return [void]
Contract String => Any
def start_watching(dir)
@original_source_dir = dir
@watcher = @sources.watch :source, path: File.join(app.root, dir)
end
end
end
2012-07-09 04:01:06 +02:00
end