diff --git a/bin/jekyll2 b/bin/jekyll2 new file mode 100755 index 00000000..ce85f13b --- /dev/null +++ b/bin/jekyll2 @@ -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 diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 87984ecf..058fd762 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -41,6 +41,9 @@ require 'jekyll/errors' require 'jekyll/plugin' require 'jekyll/converter' require 'jekyll/generator' +require 'jekyll/command' + +require_all 'jekyll/commands' require_all 'jekyll/converters' require_all 'jekyll/generators' require_all 'jekyll/tags' diff --git a/lib/jekyll/command.rb b/lib/jekyll/command.rb new file mode 100644 index 00000000..340d457d --- /dev/null +++ b/lib/jekyll/command.rb @@ -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 diff --git a/lib/jekyll/commands/build.rb b/lib/jekyll/commands/build.rb new file mode 100644 index 00000000..074f5d49 --- /dev/null +++ b/lib/jekyll/commands/build.rb @@ -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