class Locomotive::Wagon::Listen

Public Class Methods

start(path, cache, options) click to toggle source
# File lib/locomotive/wagon/tools/listen.rb, line 12
def self.start(path, cache, options)
  new(path, cache, options).tap do |instance|
    puts 'Listener polling is on.' if options[:force_polling]
    instance.apply_definitions
  end
end

Public Instance Methods

apply_definitions() click to toggle source
# File lib/locomotive/wagon/tools/listen.rb, line 19
def apply_definitions
  self.definitions.each do |definition|
    self.apply(definition)
  end
end

Protected Instance Methods

apply(definition) click to toggle source
# File lib/locomotive/wagon/tools/listen.rb, line 37
def apply(definition)
  reloader  = build_reloader([*definition.last])
  filter    = definition[1]
  _path     = File.expand_path(File.join(self.path, definition.first))

  listener = ::Listen.to(_path, {
    only:           filter,
    force_polling:  options[:force_polling]
  }, &reloader)

  # non blocking listener
  listener.start
end
build_reloader(resources) click to toggle source
# File lib/locomotive/wagon/tools/listen.rb, line 51
def build_reloader(resources)
  Proc.new do |modified, added, removed|
    resources.each do |resource|
      Locomotive::Common::Logger.info "service=listen action=reload resource=#{resource} timestamp=#{Time.now}"

      clear_cache_for(resource, modified + added + removed)
    end
  end
end
clear_cache_for(resource, files) click to toggle source
# File lib/locomotive/wagon/tools/listen.rb, line 61
def clear_cache_for(resource, files)
  keys = case resource
  when :site  then '_sites'
  when :content_entries
    files.map { |f| "site_1_content_type_#{File.basename(f, '.yml')}_content_entries" }
  else
    "site_1_#{resource}"
  end

  [*keys].each { |key| cache.delete(key) }
end
definitions() click to toggle source
# File lib/locomotive/wagon/tools/listen.rb, line 27
def definitions
  [
    ['config', /\.yml/, [:site, :content_types, :pages, :snippets, :sections, :content_entries, :translations]],
    ['app/views', %r{(pages|snippets|sections)/(.+\.liquid).*}, [:pages, :snippets, :sections]],
    ['app/content_types', /\.yml/, [:content_types, :content_entries]],
    ['data', /\.(yml|json)/, [:pages, :content_entries]],
    ['public', %r{((stylesheets|javascripts)/(.+\.(css|js))).*}, []]
  ]
end