Module: Bunto::Watcher
Instance Method Summary collapse
-
#build_listener(site, options) ⇒ Object
TODO: shouldn’t be public API.
- #config_files(options) ⇒ Object
- #custom_excludes(options) ⇒ Object
- #listen_handler(site) ⇒ Object
-
#listen_ignore_paths(options) ⇒ Object
Paths to ignore for the watch option.
- #sleep_forever ⇒ Object
- #to_exclude(options) ⇒ Object
- #watch(options) ⇒ Object
Instance Method Details
#build_listener(site, options) ⇒ Object
TODO: shouldn’t be public API
30 31 32 33 34 35 36 37 |
# File 'lib/bunto/watcher.rb', line 30 def build_listener(site, ) Listen.to( ['source'], :ignore => listen_ignore_paths(), :force_polling => ['force_polling'], &(listen_handler(site)) ) end |
#config_files(options) ⇒ Object
61 62 63 64 65 |
# File 'lib/bunto/watcher.rb', line 61 def config_files() %w(yml yaml toml).map do |ext| Bunto.sanitized_path(['source'], "_config.#{ext}") end end |
#custom_excludes(options) ⇒ Object
57 58 59 |
# File 'lib/bunto/watcher.rb', line 57 def custom_excludes() Array(['exclude']).map { |e| Bunto.sanitized_path(['source'], e) } end |
#listen_handler(site) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bunto/watcher.rb', line 39 def listen_handler(site) proc do |modified, added, removed| t = Time.now c = modified + added + removed n = c.length print Bunto.logger.("Regenerating:", "#{n} file(s) changed at #{t.strftime("%Y-%m-%d %H:%M:%S")} ") begin site.process puts "...done in #{Time.now - t} seconds." rescue => e puts "...error:" Bunto.logger.warn "Error:", e. Bunto.logger.warn "Error:", "Run bunto build --trace for more information." end end end |
#listen_ignore_paths(options) ⇒ Object
Paths to ignore for the watch option
options - A Hash of options passed to the command
Returns a list of relative paths from source that should be ignored
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/bunto/watcher.rb', line 80 def listen_ignore_paths() source = Pathname.new(['source']). paths = to_exclude() paths.map do |p| absolute_path = Pathname.new(p). next unless absolute_path.exist? begin relative_path = absolute_path.relative_path_from(source).to_s unless relative_path.start_with?('../') path_to_ignore = Regexp.new(Regexp.escape(relative_path)) Bunto.logger.debug "Watcher:", "Ignoring #{path_to_ignore}" path_to_ignore end rescue ArgumentError # Could not find a relative path end end.compact + [/\.bunto\-metadata/] end |
#sleep_forever ⇒ Object
100 101 102 |
# File 'lib/bunto/watcher.rb', line 100 def sleep_forever loop { sleep 1000 } end |
#to_exclude(options) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/bunto/watcher.rb', line 67 def to_exclude() [ config_files(), ['destination'], custom_excludes() ].flatten end |
#watch(options) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/bunto/watcher.rb', line 7 def watch() ENV["LISTEN_GEM_DEBUGGING"] ||= "1" if ['verbose'] site = Bunto::Site.new() listener = build_listener(site, ) listener.start Bunto.logger.info "Auto-regeneration:", "enabled for '#{options["source"]}'" unless ['serving'] trap("INT") do listener.stop puts " Halting auto-regeneration." exit 0 end sleep_forever end rescue ThreadError # You pressed Ctrl-C, oh my! end |