From c926596be7972389754a7344477476e4d522e1a1 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 8 Sep 2013 15:58:57 -0400 Subject: [PATCH] Print better messages for detached server. Mute output on detach. --- bin/jekyll | 2 ++ lib/jekyll/commands/serve.rb | 51 +++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/bin/jekyll b/bin/jekyll index 53ce267e..ee497c25 100755 --- a/bin/jekyll +++ b/bin/jekyll @@ -65,6 +65,7 @@ command :build do |c| c.option '-w', '--watch', 'Watch for changes and rebuild' c.option '--lsi', 'Use LSI for improved related posts' c.option '-D', '--drafts', 'Render posts in the _drafts folder' + c.option '-v', '--verbose', 'Print verbose output.' c.action do |args, options| options = normalize_options(options.__hash__) @@ -84,6 +85,7 @@ command :serve do |c| c.option '--lsi', 'Use LSI for improved related posts' c.option '-B', '--detach', 'Run the server in the background (detach)' c.option '-D', '--drafts', 'Render posts in the _drafts folder' + c.option '-v', '--verbose', 'Print verbose output.' c.option '-P', '--port [PORT]', 'Port to listen on' c.option '-H', '--host [HOST]', 'Host to bind to' diff --git a/lib/jekyll/commands/serve.rb b/lib/jekyll/commands/serve.rb index 82bbc947..9cec5457 100644 --- a/lib/jekyll/commands/serve.rb +++ b/lib/jekyll/commands/serve.rb @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- encoding: utf-8 -*- module Jekyll module Commands class Serve < Command @@ -10,32 +10,57 @@ module Jekyll FileUtils.mkdir_p(destination) - mime_types_file = File.expand_path('../mime.types', File.dirname(__FILE__)) - mime_types = WEBrick::HTTPUtils::load_mime_types(mime_types_file) - # recreate NondisclosureName under utf-8 circumstance fh_option = WEBrick::Config::FileHandler fh_option[:NondisclosureName] = ['.ht*','~*'] - s = HTTPServer.new( - :Port => options['port'], - :BindAddress => options['host'], - :MimeTypes => mime_types, - :DoNotReverseLookup => true - ) + s = HTTPServer.new(webrick_options(options)) s.mount(options['baseurl'], HTTPServlet::FileHandler, destination, fh_option) if options['detach'] # detach the server - pid = Process.fork {s.start} + pid = Process.fork { s.start } Process.detach(pid) - pid + Jekyll.logger.info "Server detatched with pid '#{pid}'.", "Run `kill -9 #{pid}' to stop the server." else # create a new server thread, then join it with current terminal - t = Thread.new { s.start } + t = Thread.new { + + s.start + } trap("INT") { s.shutdown } t.join() end end + + def self.webrick_options(config) + opts = { + :Port => config['port'], + :BindAddress => config['host'], + :MimeTypes => self.mime_types, + :DoNotReverseLookup => true, + :StartCallback => start_callback(config['detach']) + } + + if !config['verbose'] + opts.merge!({ + :AccessLog => [], + :Logger => Log::new([], Log::WARN) + }) + end + + opts + end + + def self.start_callback(detached) + unless detached + Proc.new { Jekyll.logger.info "Server running...", "press ctrl-c to stop." } + end + end + + def self.mime_types + mime_types_file = File.expand_path('../mime.types', File.dirname(__FILE__)) + WEBrick::HTTPUtils::load_mime_types(mime_types_file) + end end end end