Skip to content

Commit

Permalink
Made the autobuilder smarter. It now only reloads those resources that
Browse files Browse the repository at this point in the history
have actually changed (instead of clearing the database and reloading
everything).
  • Loading branch information
TwP committed Oct 14, 2007
1 parent dabe1db commit 7d1606f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
13 changes: 12 additions & 1 deletion lib/webby.rb
Expand Up @@ -12,7 +12,7 @@

module Webby

VERSION = '0.4.0' # :nodoc:
VERSION = '0.5.0' # :nodoc:

# Path to the Webby package
PATH = ::File.expand_path(::File.join(::File.dirname(__FILE__), '..'))
Expand Down Expand Up @@ -62,6 +62,17 @@ def self.page_defaults
}
end

# call-seq
# Webby.exclude => regexp
#
# Returns a regular expression used to exclude resources from the content
# directory from being processed by Webby. This same regular expression is
# also used to exclude layouts.
#
def self.exclude
@exclude ||= Regexp.new(config['exclude'].join('|'))
end

end # module Webby


Expand Down
14 changes: 11 additions & 3 deletions lib/webby/auto_builder.rb
Expand Up @@ -27,15 +27,16 @@ def self.run
# Create a new AutoBuilder class.
#
def initialize
@log = Logging::Logger[self]

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

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

@log = Logging::Logger[self]
end

# call-seq:
Expand All @@ -49,7 +50,14 @@ def update( *events )
ary = events.find_all {|evt| evt.type != :removed}
return if ary.empty?

Builder.run
ary.each do |evt|
@log.debug "changed #{evt.path}"
next unless test ?f, evt.path
next if evt.path =~ ::Webby.exclude
Resource.new evt.path
end

@builder.run :load_files => false
rescue => err
@log.error err
end
Expand Down
21 changes: 12 additions & 9 deletions lib/webby/builder.rb
Expand Up @@ -58,7 +58,7 @@ def initialize
end

# call-seq:
# run( :rebuild => false )
# run( :rebuild => false, :load_files => true )
#
# Runs the Webby builder by loading in the layout files from the
# <code>layouts/</code> folder and the content from the
Expand All @@ -80,14 +80,15 @@ def initialize
# more recently than the output file.
#
def run( opts = {} )
Resource.clear
opts[:load_files] = true unless opts.has_key?(:load_files)

unless test(?d, output_dir)
@log.info "creating #{output_dir}"
FileUtils.mkdir output_dir
end

load_files
load_files if opts[:load_files]
loop_check

Resource.pages.each do |page|
next unless page.dirty? or opts[:rebuild]
Expand Down Expand Up @@ -120,18 +121,20 @@ def run( opts = {} )
# folder and create a new Resource object for each file found there.
#
def load_files
excl = Regexp.new exclude.join('|')

::Find.find(layout_dir, content_dir) do |path|
next unless test ?f, path
next if path =~ excl
next if path =~ ::Webby.exclude
Resource.new path
end
end

# Loop over all the layout resources looking for circular reference -- a
# layout that eventually refers back to itself. These are bad. Raise an
# error if one is detected.
#
def loop_check
layouts = Resource.layouts

# look for loops in the layout references -- i.e. a layout
# eventually refers back to itself
layouts.each do |lyt|
stack = []
while lyt
Expand All @@ -146,7 +149,7 @@ def load_files
end # each
end

%w(output_dir layout_dir content_dir exclude).each do |key|
%w(output_dir layout_dir content_dir).each do |key|
self.class_eval <<-CODE
def #{key}( ) ::Webby.config['#{key}'] end
CODE
Expand Down
7 changes: 6 additions & 1 deletion lib/webby/pages_db.rb
Expand Up @@ -18,7 +18,12 @@ def initialize
# add( resource )
#
def add( page )
@db[page.dir] << page
ary = @db[page.dir]

# make sure we don't duplicate pages
ary.delete page if ary.include? page
ary << page

self
end
alias :<< :add
Expand Down
10 changes: 5 additions & 5 deletions lib/webby/resource.rb
Expand Up @@ -214,24 +214,24 @@ def dirty?
return @mdata['dirty'] if @mdata.has_key? 'dirty'

# if the destination file does not exist, then we are dirty
return @mdata['dirty'] = true unless test ?e, destination
return true unless test ?e, destination

# if this file's mtime is larger than the destination file's
# mtime, then we are dirty
@mdata['dirty'] = @mtime > File.mtime(destination)
return @mdata['dirty'] if is_static? or @mdata['dirty']
dirty = @mtime > File.mtime(destination)
return dirty if is_static? or dirty

# check to see if the layout is dirty, and it it is then we
# are dirty, too
if @mdata.has_key? 'layout'
lyt = self.class.layouts.find_by_name @mdata['layout']
unless lyt.nil?
return @mdata['dirty'] = true if lyt.dirty?
return true if lyt.dirty?
end
end

# if we got here, then we are not dirty
@mdata['dirty'] = false
false
end

# call-seq:
Expand Down

0 comments on commit 7d1606f

Please sign in to comment.