Add bin/jekyll2 and initial BuildCommand

The `BuildCommand` class is responsible for handling the building of the site.
It can also optionally watch for changes to files and regenerate the site if
needed.

The `Command` class holds any methods which are used by any command
implementation.
This commit is contained in:
Tom Bell 2012-12-18 20:34:09 +00:00
parent 6c0c5b6187
commit 14766497c8
4 changed files with 125 additions and 0 deletions

31
bin/jekyll2 Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env ruby
$:.unshift File.join(File.dirname(__FILE__), *%w{ .. lib })
require 'commander/import'
require 'jekyll'
# Details about Jekyll
program :name, 'jekyll'
program :version, Jekyll::VERSION
program :description, 'Jekyll is a blog-aware, static site generator in Ruby'
# Global options available to every command
global_option '-s', '--source DIR', String, 'Source directory'
global_option '-d', '--destination DIR', String, 'Destination directory'
# Build command
#
# Args:
# --source
# --destination
# --watch
command :build do |c|
c.syntax = 'jekyll build [options]'
c.description = 'Build...'
c.option '-w', '--watch', 'Watch for changes and rebuild'
c.action do |args, options|
options.default :watch => false
Jekyll::BuildCommand.process(options)
end
end

View File

@ -41,6 +41,9 @@ require 'jekyll/errors'
require 'jekyll/plugin' require 'jekyll/plugin'
require 'jekyll/converter' require 'jekyll/converter'
require 'jekyll/generator' require 'jekyll/generator'
require 'jekyll/command'
require_all 'jekyll/commands'
require_all 'jekyll/converters' require_all 'jekyll/converters'
require_all 'jekyll/generators' require_all 'jekyll/generators'
require_all 'jekyll/tags' require_all 'jekyll/tags'

14
lib/jekyll/command.rb Normal file
View File

@ -0,0 +1,14 @@
module Jekyll
class Command
def self.globs(source)
Dir.chdir(source) do
dirs = Dir['*'].select { |x| File.directory?(x) }
dirs -= ['_site']
dirs = dirs.map { |x| "#{x}/**/*" }
dirs += ['*']
end
end
end
end

View File

@ -0,0 +1,77 @@
module Jekyll
class BuildCommand < Command
def self.process(options)
opts = {}
options.__hash__.map do |k,v|
opts[k.to_s] = v
end
opts = Jekyll.configuration(opts)
site = Jekyll::Site.new(opts)
source = opts['source']
destination = opts['destination']
if opts['watch']
self.watch(site, source, destination)
else
self.build(site, source, destination)
end
end
# Private: Build the site from source into destination.
#
# site - A Jekyll::Site instance
# source - A String of the source path
# destination - A String of the destination path
#
# Returns nothing.
def self.build(site, source, destination)
puts "Building site: #{source} -> #{destination}"
begin
site.process
rescue Jekyll::FatalException => e
puts
puts "ERROR: YOUR SITE COULD NOT BE BUILT:"
puts "------------------------------------"
puts e.message
exit(1)
end
puts "Successfully generated site: #{source} -> #{destination}"
end
# Private: Watch for file changes and rebuild the site.
#
# site - A Jekyll::Site instance
# source - A String of the source path
# destination - A String of the destination path
#
# Returns nothing.
def self.watch(site, source, destination)
require 'directory_watcher'
puts "Auto-Regenerating enabled: #{source} -> #{destination}"
dw = DirectoryWatcher.new(source)
dw.interval = 1
dw.glob = self.globs(source)
dw.add_observer do |*args|
t = Time.now.strftime("%Y-%m-%d %H:%M:%S")
puts "[#{t} regeneration: #{args.size} files changed"
site.process
end
dw.start
trap("SIGINT") do
puts "Stopping auto-regeneration..."
exit 0
end
loop { sleep 1000 }
end
end
end