class Middleman::CoreExtensions::FileWatcher::API
Core File Change API class
def changed(matcher=nil, &block)
-
(Array
-)
Parameters:
-
matcher
(nil, Regexp
) -- A Regexp to match the change path against
def changed(matcher=nil, &block) @_changed << [block, matcher] if block_given? @_changed end
def deleted(matcher=nil, &block)
-
(Array
-)
Parameters:
-
matcher
(nil, Regexp
) -- A Regexp to match the deleted path against
def deleted(matcher=nil, &block) @_deleted << [block, matcher] if block_given? @_deleted end
def did_change(path)
-
(void)
-
Parameters:
-
path
(Pathname
) -- The file that changed
def did_change(path) path = Pathname(path) return if ignored?(path) logger.debug "== File Change: #{path}" @known_paths << path self.run_callbacks(path, :changed) end
def did_delete(path)
-
(void)
-
Parameters:
-
path
(Pathname
) -- The file that was deleted
def did_delete(path) path = Pathname(path) return if ignored?(path) logger.debug "== File Deletion: #{path}" @known_paths.delete(path) self.run_callbacks(path, :deleted) end
def exists?(path)
def exists?(path) p = Pathname(path) p = p.relative_path_from(Pathname(@app.root)) if !p.relative? @known_paths.include?(p) end
def find_new_files(path)
-
(void)
-
Parameters:
-
path
(Pathname
) -- The path to reload
def find_new_files(path) reload_path(path, true) end
def ignored?(path)
-
(Boolean)
-
Parameters:
-
path
(Pathname
) --
def ignored?(path) path = path.to_s app.config[:file_watcher_ignore].any? { |r| path =~ r } end
def initialize(app)
def initialize(app) @app = app @known_paths = Set.new @_changed = [] @_deleted = [] end
def reload_path(path, only_new=false)
-
(void)
-
Parameters:
-
only_new
(Boolean
) -- Whether we only look for new files -
path
(Pathname
) -- The path to reload
def reload_path(path, only_new=false) # chdir into the root directory so Pathname can work with relative paths Dir.chdir @app.root_path do path = Pathname(path) return unless path.exist? glob = (path + "**").to_s subset = @known_paths.select { |p| p.fnmatch(glob) } ::Middleman::Util.all_files_under(path).each do |filepath| next if only_new && subset.include?(filepath) subset.delete(filepath) did_change(filepath) end subset.each(&method(:did_delete)) unless only_new end end
def run_callbacks(path, callbacks_name)
-
(void)
-
Parameters:
-
callbacks_name
(Symbol
) -- The name of the callbacks method -
path
(Pathname
) -- The file that was changed
def run_callbacks(path, callbacks_name) path = path.to_s self.send(callbacks_name).each do |callback, matcher| next unless matcher.nil? || path.match(matcher) @app.instance_exec(path, &callback) end end