Sha256: 34b485bb84f4e47e24c34d7825289172b384318394ba3f1016e3f8872fa5d5b6

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

require 'listen'

module Locomotive::Builder
  class Listen

    attr_accessor :reader

    def self.instance
      @@instance = new
    end

    def start(reader)
      self.reader = reader

      self.definitions.each do |definition|
        self.apply(definition)
      end
    end

    def definitions
      [
        ['config', /\.yml/, [:site, :content_types, :pages, :snippets, :content_entries, :translations]],
        ['app/views', /\.liquid/, [:pages, :snippets]],
        ['app/content_types', /\.yml/, [:content_types, :content_entries]],
        ['data', /\.yml/, :content_entries]
      ]
    end

    protected

    def apply(definition)
      reloader = Proc.new do |modified, added, removed|
        resources = [*definition.last]
        names     = resources.map { |n| "\"#{n}\"" }.join(', ')

        Locomotive::Builder::Logger.info "* Reloaded #{names} at #{Time.now}"

        begin
          reader.reload(resources)
        rescue Exception => e
          Locomotive::Builder::MounterException.new('Unable to reload', e)
        end
      end

      filter  = definition[1]
      path    = File.join(self.reader.mounting_point.path, definition.first)
      path    = File.expand_path(path)

      listener = ::Listen.to(path).filter(filter).change(&reloader)

      # non blocking listener
      listener.start(false)
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
locomotivecms_builder-1.0.0.alpha8 lib/locomotive/builder/listen.rb
locomotivecms_builder-1.0.0.alpha7 lib/locomotive/builder/listen.rb
locomotivecms_builder-1.0.0.alpha6 lib/locomotive/builder/listen.rb
locomotivecms_builder-1.0.0.alpha5 lib/locomotive/builder/listen.rb
locomotivecms_builder-1.0.0.alpha4 lib/locomotive/builder/listen.rb
locomotivecms_builder-1.0.0.alpha3 lib/locomotive/builder/listen.rb