Skip to content

Commit

Permalink
Merge pull request #1589 from dchest/better-watch
Browse files Browse the repository at this point in the history
Replace directory_watcher with listen.
  • Loading branch information
mattr- committed Oct 24, 2013
2 parents 32c7530 + 12ba0a5 commit 2287eed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion jekyll.gemspec
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |s|

s.add_runtime_dependency('liquid', "~> 2.5.2")
s.add_runtime_dependency('classifier', "~> 1.3")
s.add_runtime_dependency('directory_watcher', "~> 1.4.1")
s.add_runtime_dependency('listen', "~> 2.0")
s.add_runtime_dependency('maruku', "~> 0.6.0")
s.add_runtime_dependency('pygments.rb', "~> 0.5.0")
s.add_runtime_dependency('commander', "~> 4.1.3")
Expand Down
23 changes: 15 additions & 8 deletions lib/jekyll/commands/build.rb
Expand Up @@ -31,27 +31,34 @@ def self.build(site, options)
#
# Returns nothing.
def self.watch(site, options)
require 'directory_watcher'
require 'listen'

source = options['source']
destination = options['destination']

Jekyll.logger.info "Auto-regeneration:", "enabled"
begin
ignored = Regexp.new(Regexp.escape(Pathname.new(destination)
.relative_path_from(Pathname.new(source))
.to_path))
rescue ArgumentError
# Destination is outside the source, no need to ignore it.
ignored = nil
end

dw = DirectoryWatcher.new(source, :glob => self.globs(source, destination), :pre_load => true)
dw.interval = 1
Jekyll.logger.info "Auto-regeneration:", "enabled"

dw.add_observer do |*args|
listener = Listen.to(source, ignore: ignored) do |modified, added, removed|
t = Time.now.strftime("%Y-%m-%d %H:%M:%S")
print Jekyll.logger.formatted_topic("Regenerating:") + "#{args.size} files at #{t} "
n = modified.length + added.length + removed.length
print Jekyll.logger.formatted_topic("Regenerating:") + "#{n} files at #{t} "
self.process_site(site)
puts "...done."
end

dw.start
listener.start

unless options['serving']
trap("INT") do
listener.stop
puts " Halting auto-regeneration."
exit 0
end
Expand Down

0 comments on commit 2287eed

Please sign in to comment.