Class: Webby::AutoBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/webby/auto_builder.rb

Overview

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.

Defined Under Namespace

Classes: WebServer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAutoBuilder

call-seq:

AutoBuilder.new

Create a new AutoBuilder class.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger



26
27
28
# File 'lib/webby/auto_builder.rb', line 26

def logger
  @logger
end

Class Method Details

.runObject

call-seq:

AutoBuilder.run

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



22
23
24
# File 'lib/webby/auto_builder.rb', line 22

def self.run
  self.new.run
end

Instance Method Details

#runObject

call-seq:

run

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object

call-seq:

update( *events )

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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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