class Middleman::CoreExtensions::FileWatcher::API

Core File Change API class

def changed(matcher=nil, &block)

Returns:
  • (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)

Returns:
  • (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)

Returns:
  • (void) -

Parameters:
  • path (Pathname) -- The file that changed
def did_change(path)
  return if ignored?(path)
  puts "== File Change: #{path.relative_path_from(@app.root_path)}" if @app.logging?
  @known_paths << path
  self.run_callbacks(path, :changed)
end

def did_delete(path)

Returns:
  • (void) -

Parameters:
  • path (Pathname) -- The file that was deleted
def did_delete(path)
  return if ignored?(path)
  puts "== File Deletion: #{path.relative_path_from(@app.root_path)}" if @app.logging?
  @known_paths.delete(path)
  self.run_callbacks(path, :deleted)
end

def find_new_files(path)

Returns:
  • (void) -

Parameters:
  • path (Pathname) -- The path to reload
def find_new_files(path)
  reload_path(path, true)
end

def ignored?(path)

Returns:
  • (Boolean) -

Parameters:
  • path (Pathname) --
def ignored?(path)
  path = path.relative_path_from(@app.root_path).to_s if path.is_a? Pathname
  IGNORE_LIST.any? { |r| path.to_s.match(r) }
end

def initialize(app)

Initialize api and internal path cache
def initialize(app)
  @app = app
  @known_paths = Set.new
  
  @_changed = []
  @_deleted = []
end

def reload_path(path, only_new=false)

Returns:
  • (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)
  return unless path.exist?
  
  glob = "#{path}**/*"
  subset = @known_paths.select { |p| p.fnmatch(glob) }
  
  path.find do |filepath|
    full_path = path + filepath
    next if full_path.directory?
    
    if only_new
      next if subset.include?(full_path)
    else
      subset.delete(full_path)
    end
    
    self.did_change(full_path)
  end
  
  subset.each(&method(:did_delete)) unless only_new
end

def run_callbacks(path, callbacks_name)

Returns:
  • (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.relative_path_from(@app.root_path).to_s if path.is_a? Pathname
  self.send(callbacks_name).each do |callback, matcher|
    next if path.match(%r{^#{@app.build_dir}/})
    next unless matcher.nil? || path.match(matcher)
    @app.instance_exec(path, &callback)
  end
end