2014-07-03 04:04:34 +02:00
|
|
|
require 'middleman-core/contracts'
|
2014-07-16 03:01:45 +02:00
|
|
|
require 'middleman-core/sources'
|
2014-05-26 03:03:37 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
module Middleman
|
|
|
|
module CoreExtensions
|
2014-05-26 03:03:37 +02:00
|
|
|
# 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
|
|
|
|
|
2015-05-02 20:48:21 +02:00
|
|
|
# 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
|
2014-05-26 03:03:37 +02:00
|
|
|
def before_configuration
|
2016-01-20 21:20:27 +01:00
|
|
|
@sources.poll_once!
|
2014-05-26 03:03:37 +02:00
|
|
|
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
|
2014-05-26 03:03:37 +02:00
|
|
|
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])
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-07-16 03:01:45 +02:00
|
|
|
@sources.start!
|
2016-01-20 21:20:27 +01:00
|
|
|
@sources.poll_once!
|
2014-07-16 03:01:45 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-07-16 03:01:45 +02:00
|
|
|
protected
|
2014-03-19 05:17:50 +01:00
|
|
|
|
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)
|
2011-11-18 01:02:07 +01:00
|
|
|
end
|
|
|
|
end
|
2011-11-17 22:21:05 +01:00
|
|
|
end
|
2012-07-09 04:01:06 +02:00
|
|
|
end
|