Add initial serve command

The `ServeCommand` will let you serve your site locally for development. You
can specify `--port`, `--host` and `--baseurl` options if you wish to change the
defaults.

Additionally the `BuildCommand` will be called before the processing of the
serve command, this makes sure that the site is actually built. This means you
are able to pass the `--watch` option to auto-regenerate your site, even while
serving it locally.
This commit is contained in:
Tom Bell 2012-12-18 21:00:24 +00:00
parent 14766497c8
commit 3b4feb41f0
3 changed files with 75 additions and 13 deletions

View File

@ -29,3 +29,32 @@ command :build do |c|
Jekyll::BuildCommand.process(options) Jekyll::BuildCommand.process(options)
end end
end end
# Serve command
#
# Args:
# --source
# --destination
# --watch
#
# --port
# --host
# --baseurl
command :serve do |c|
c.syntax = 'jekyll serve [options]'
c.description = 'Serve...'
c.option '-w', '--watch', 'Watch for changes and rebuild'
c.option '-p', '--port [PORT]', 'Port to listen on'
c.option '-h', '--host [HOST]', 'Host to bind to'
c.option '-b', '--baseurl [URL]', 'Base URL'
c.action do |args, options|
options.default :watch => false,
:port => '4000',
:host => '0.0.0.0',
:baseurl => '/',
:serving => true
Jekyll::BuildCommand.process(options)
Jekyll::ServeCommand.process(options)
end
end

View File

@ -14,20 +14,21 @@ module Jekyll
destination = opts['destination'] destination = opts['destination']
if opts['watch'] if opts['watch']
self.watch(site, source, destination) self.watch(site, opts)
else else
self.build(site, source, destination) self.build(site, opts)
end end
end end
# Private: Build the site from source into destination. # Private: Build the site from source into destination.
# #
# site - A Jekyll::Site instance # site - A Jekyll::Site instance
# source - A String of the source path # options - A Hash of options passed to the command
# destination - A String of the destination path
# #
# Returns nothing. # Returns nothing.
def self.build(site, source, destination) def self.build(site, options)
source = options['source']
destination = options['destination']
puts "Building site: #{source} -> #{destination}" puts "Building site: #{source} -> #{destination}"
begin begin
site.process site.process
@ -44,13 +45,15 @@ module Jekyll
# Private: Watch for file changes and rebuild the site. # Private: Watch for file changes and rebuild the site.
# #
# site - A Jekyll::Site instance # site - A Jekyll::Site instance
# source - A String of the source path # options - A Hash of options passed to the command
# destination - A String of the destination path
# #
# Returns nothing. # Returns nothing.
def self.watch(site, source, destination) def self.watch(site, options)
require 'directory_watcher' require 'directory_watcher'
source = options['source']
destination = options['destination']
puts "Auto-Regenerating enabled: #{source} -> #{destination}" puts "Auto-Regenerating enabled: #{source} -> #{destination}"
dw = DirectoryWatcher.new(source) dw = DirectoryWatcher.new(source)
@ -65,12 +68,14 @@ module Jekyll
dw.start dw.start
trap("SIGINT") do unless options['serving']
loop { sleep 1000 }
trap("INT") do
puts "Stopping auto-regeneration..." puts "Stopping auto-regeneration..."
exit 0 exit 0
end end
end
loop { sleep 1000 }
end end
end end

View File

@ -0,0 +1,28 @@
module Jekyll
class ServeCommand < Command
def self.process(options)
require 'webrick'
include WEBrick
destination = options.destination
FileUtils.mkdir_p(destination)
mime_types = WEBrick::HTTPUtils::DefaultMimeTypes
mime_types.store 'js', 'application/javascript'
s = HTTPServer.new(
:Port => options.port,
:BindAddress => options.host,
:MimeTypes => mime_types
)
s.mount(options.baseurl, HTTPServlet::FileHandler, destination)
t = Thread.new { s.start }
trap("INT") { s.shutdown }
t.join()
end
end
end