Sha256: 296bdf5ea7062cf02993db46796eebf50577bf33a43d5d33249c8ba85f3363c4

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

module Massimo
  class Watcher
    class << self
      def start(site)
        Massimo::UI.say 'massimo is watching your files for changes', :growl => true
        self.new(site).run
      end
    end
    
    def initialize(site)
      @site  = site
      @files = []
    end
    
    # Runs a loop, processing the Site whenever files have changed.
    def run
      begin
        loop do
          process
          sleep 0.5
        end
      rescue Interrupt
        exit
      end
    end
    
    # Processes the Site if any of the files have changed.
    def process
      if changed?
        Massimo::UI.report_errors do
          Massimo::UI.say 'massimo has noticed a change'
          @site.process
          Massimo::UI.say 'massimo has built your site', :growl => true
        end
      end
    end
    
    # Determine if any of the Site's files have changed.
    def changed?
      @files != files
    end
    
    protected
    
    def files
      @files = Dir[*glob].map { |file| File.mtime(file) }
    end
    
    def glob
      glob  = @site.resources.map(&:path)
      glob << @site.config.path_for(:lib)
      glob << @site.config.path_for(:helpers)
      glob.map! { |path| File.join(path, '**/*.*') }
      glob
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
massimo-0.7.3 lib/massimo/watcher.rb
massimo-0.7.2 lib/massimo/watcher.rb
massimo-0.7.1 lib/massimo/watcher.rb