|
| 1 | +require "bundler/gem_tasks" |
| 2 | +require "jekyll" |
| 3 | +require "listen" |
| 4 | + |
| 5 | +def listen_ignore_paths(base, options) |
| 6 | + [ |
| 7 | + /_config\.ya?ml/, |
| 8 | + /_site/, |
| 9 | + /\.jekyll-metadata/ |
| 10 | + ] |
| 11 | +end |
| 12 | + |
| 13 | +def listen_handler(base, options) |
| 14 | + site = Jekyll::Site.new(options) |
| 15 | + Jekyll::Command.process_site(site) |
| 16 | + proc do |modified, added, removed| |
| 17 | + t = Time.now |
| 18 | + c = modified + added + removed |
| 19 | + n = c.length |
| 20 | + relative_paths = c.map{ |p| Pathname.new(p).relative_path_from(base).to_s } |
| 21 | + print Jekyll.logger.message("Regenerating:", "#{relative_paths.join(", ")} changed... ") |
| 22 | + begin |
| 23 | + Jekyll::Command.process_site(site) |
| 24 | + puts "regenerated in #{Time.now - t} seconds." |
| 25 | + rescue => e |
| 26 | + puts "error:" |
| 27 | + Jekyll.logger.warn "Error:", e.message |
| 28 | + Jekyll.logger.warn "Error:", "Run jekyll build --trace for more information." |
| 29 | + end |
| 30 | + end |
| 31 | +end |
| 32 | + |
| 33 | +task :preview do |
| 34 | + base = Pathname.new('.').expand_path |
| 35 | + options = { |
| 36 | + "source" => base.join('test').to_s, |
| 37 | + "destination" => base.join('test/_site').to_s, |
| 38 | + "force_polling" => false, |
| 39 | + "serving" => true, |
| 40 | + "theme" => "minimal-mistakes-jekyll" |
| 41 | + } |
| 42 | + |
| 43 | + options = Jekyll.configuration(options) |
| 44 | + |
| 45 | + ENV["LISTEN_GEM_DEBUGGING"] = "1" |
| 46 | + listener = Listen.to( |
| 47 | + base.join("_data"), |
| 48 | + base.join("_includes"), |
| 49 | + base.join("_layouts"), |
| 50 | + base.join("_sass"), |
| 51 | + base.join("assets"), |
| 52 | + options["source"], |
| 53 | + :ignore => listen_ignore_paths(base, options), |
| 54 | + :force_polling => options['force_polling'], |
| 55 | + &(listen_handler(base, options)) |
| 56 | + ) |
| 57 | + |
| 58 | + begin |
| 59 | + listener.start |
| 60 | + Jekyll.logger.info "Auto-regeneration:", "enabled for '#{options["source"]}'" |
| 61 | + |
| 62 | + unless options['serving'] |
| 63 | + trap("INT") do |
| 64 | + listener.stop |
| 65 | + puts " Halting auto-regeneration." |
| 66 | + exit 0 |
| 67 | + end |
| 68 | + |
| 69 | + loop { sleep 1000 } |
| 70 | + end |
| 71 | + rescue ThreadError |
| 72 | + # You pressed Ctrl-C, oh my! |
| 73 | + end |
| 74 | + |
| 75 | + Jekyll::Commands::Serve.process(options) |
| 76 | +end |
0 commit comments