class Webby::AutoBuilder - webby-0.9.4 Documentation

# class Webby::AutoBuilder

The AutoBuilder class is used to monitor the content and layouts folders and to compile the resource files only when they are modified. If a layout is modified, then all resources that depend upon the layout are compiled.

### Attributes

logger[R]

### Public Class Methods

new click to toggle source

Create a new AutoBuilder class.

    # File lib/webby/auto_builder.rb, line 33
    def initialize
      @logger = Logging::Logger[self]

      @builder = Builder.new
      ::Webby.load_files

      @watcher = DirectoryWatcher.new '.', :interval => 2
      @watcher.add_observer self

      glob = []
      glob << File.join(::Webby.site.layout_dir, '**', '*')
      glob << File.join(::Webby.site.content_dir, '**', '*')
      @watcher.glob = glob

      @web_server = ::Webby.site.use_web_server ? WebServer.new : nil
    end

run click to toggle source

Creates a new AutoBuilder and sets it running. This method will only return when the user presses Ctrl-C.

    # File lib/webby/auto_builder.rb, line 22
    def self.run
      self.new.run
    end

### Public Instance Methods

run click to toggle source

Starts the DirectoryWatcher running and waits till the user presses Ctrl-C to stop the watcher thread.

    # File lib/webby/auto_builder.rb, line 80
    def run
      logger.info 'starting autobuild (Ctrl-C to stop)'

      Signal.trap('INT') {
        @watcher.stop
        @web_server.stop if @web_server
      }

      @watcher.start
      if @web_server
        @web_server.start
        sleep 0.25
        Launchy.open("http://localhost:#{::Webby.site.web_port}")
      end

      @watcher.join
      @web_server.join if @web_server
    end

update( *events ) click to toggle source

The update method is called by the DirectoryWatcher when files have been modified, added, or deleted. An array of events is passed to his method, and each event contains the event type and the path to the file.

    # File lib/webby/auto_builder.rb, line 57
    def update( *events )
      ary = events.find_all {|evt| evt.type != :removed}
      return if ary.empty?

      ary.each do |evt|
        logger.debug "changed #{evt.path}"
        next unless test ?f, evt.path
        next if evt.path =~ ::Webby.exclude
        Resources.new evt.path
      end

      logger.info 'running the build'
      @builder.run :load_files => false, :verbose => false
    rescue => err
      logger.error err
    end