Class Webby::AutoBuilder
In: lib/webby/auto_builder.rb
Parent: Object

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.

Methods

new   run   run   update  

Classes and Modules

Class Webby::AutoBuilder::WebServer

Attributes

logger  [R] 

Public Class methods

Create a new AutoBuilder class.

[Source]

# 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

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

[Source]

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

Public Instance methods

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

[Source]

# 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

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.

[Source]

# 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

[Validate]