From e03f48085a0937bb41d7717243f497da82bd1972 Mon Sep 17 00:00:00 2001 From: Tom Bell Date: Sat, 19 Jan 2013 22:36:46 +0000 Subject: [PATCH] Update command classes moving into a module --- bin/jekyll | 8 +-- lib/jekyll/commands/build.rb | 128 ++++++++++++++++----------------- lib/jekyll/commands/migrate.rb | 78 ++++++++++---------- lib/jekyll/commands/serve.rb | 38 +++++----- 4 files changed, 126 insertions(+), 126 deletions(-) diff --git a/bin/jekyll b/bin/jekyll index 309ffc91..0034f75e 100755 --- a/bin/jekyll +++ b/bin/jekyll @@ -27,7 +27,7 @@ command :build do |c| c.action do |args, options| options.defaults :serving => false options = Jekyll.configuration(options.__hash__) - Jekyll::BuildCommand.process(options) + Jekyll::Commands::Build.process(options) end end @@ -49,8 +49,8 @@ command :serve do |c| :serving => true options = Jekyll.configuration(options.__hash__) - Jekyll::BuildCommand.process(options) - Jekyll::ServeCommand.process(options) + Jekyll::Commands::Build.process(options) + Jekyll::Commands::Serve.process(options) end end @@ -65,6 +65,6 @@ command :import do |c| c.option '--host', 'Host address to use when migrating' c.action do |args, options| - Jekyll::MigrateCommand.process(args.first, options) + Jekyll::Commands::Migrate.process(args.first, options) end end diff --git a/lib/jekyll/commands/build.rb b/lib/jekyll/commands/build.rb index 27461101..0bb3c4f1 100644 --- a/lib/jekyll/commands/build.rb +++ b/lib/jekyll/commands/build.rb @@ -1,76 +1,76 @@ module Jekyll + module Commands + class Build < Command + def self.process(options) + site = Jekyll::Site.new(options) - class BuildCommand < Command - def self.process(options) - site = Jekyll::Site.new(options) + source = options['source'] + destination = options['destination'] - source = options['source'] - destination = options['destination'] - - if options['watch'] - self.watch(site, options) - else - self.build(site, options) - end - end - - # Private: Build the site from source into destination. - # - # site - A Jekyll::Site instance - # options - A Hash of options passed to the command - # - # Returns nothing. - def self.build(site, options) - source = options['source'] - destination = options['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 - # options - A Hash of options passed to the command - # - # Returns nothing. - def self.watch(site, options) - require 'directory_watcher' - - source = options['source'] - destination = options['destination'] - - 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 + if options['watch'] + self.watch(site, options) + else + self.build(site, options) + end end - dw.start + # Private: Build the site from source into destination. + # + # site - A Jekyll::Site instance + # options - A Hash of options passed to the command + # + # Returns nothing. + def self.build(site, options) + source = options['source'] + destination = options['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 - unless options['serving'] - trap("INT") do - puts "Stopping auto-regeneration..." - exit 0 + # Private: Watch for file changes and rebuild the site. + # + # site - A Jekyll::Site instance + # options - A Hash of options passed to the command + # + # Returns nothing. + def self.watch(site, options) + require 'directory_watcher' + + source = options['source'] + destination = options['destination'] + + 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 - loop { sleep 1000 } + dw.start + + unless options['serving'] + trap("INT") do + puts "Stopping auto-regeneration..." + exit 0 + end + + loop { sleep 1000 } + end end end end - end diff --git a/lib/jekyll/commands/migrate.rb b/lib/jekyll/commands/migrate.rb index 26937357..21c49037 100644 --- a/lib/jekyll/commands/migrate.rb +++ b/lib/jekyll/commands/migrate.rb @@ -1,47 +1,47 @@ module Jekyll + module Commands + class Migrate < Command + MIGRATORS = { + :csv => 'CSV', + :drupal => 'Drupal', + :enki => 'Enki', + :mephisto => 'Mephisto', + :mt => 'MT', + :posterous => 'Posterous', + :textpattern => 'TextPattern', + :tumblr => 'Tumblr', + :typo => 'Typo', + :wordpressdotcom => 'WordpressDotCom', + :wordpress => 'WordPress' + } - class MigrateCommand < Command - MIGRATORS = { - :csv => 'CSV', - :drupal => 'Drupal', - :enki => 'Enki', - :mephisto => 'Mephisto', - :mt => 'MT', - :posterous => 'Posterous', - :textpattern => 'TextPattern', - :tumblr => 'Tumblr', - :typo => 'Typo', - :wordpressdotcom => 'WordpressDotCom', - :wordpress => 'WordPress' - } + def self.process(migrator, options) + abort 'missing argument. Please specify a migrator' if migrator.nil? + migrator = migrator.downcase - def self.process(migrator, options) - abort 'missing argument. Please specify a migrator' if migrator.nil? - migrator = migrator.downcase - - cmd_options = [] - [ :file, :dbname, :user, :pass, :host, :site ].each do |p| - cmd_options << "\"#{options[p]}\"" unless options[p].nil? - end - - - if MIGRATORS.keys.include?(migrator) - app_root = File.expand_path( - File.join(File.dirname(__FILE__), '..', '..', '..') - ) - - require "#{app_root}/lib/jekyll/migrators/#{migrator}" - - if Jekyll.const_defiend?(MIGRATORS[migrator.to_sym]) - puts 'Importing...' - migrator_class = Jekyll.const_get(MIGRATORS[migrator.to_sym]) - migrator_class.process(*cmd_options) - exit 0 + cmd_options = [] + [ :file, :dbname, :user, :pass, :host, :site ].each do |p| + cmd_options << "\"#{options[p]}\"" unless options[p].nil? end - end - abort 'invalid migrator. Please specify a valid migrator' + + if MIGRATORS.keys.include?(migrator) + app_root = File.expand_path( + File.join(File.dirname(__FILE__), '..', '..', '..') + ) + + require "#{app_root}/lib/jekyll/migrators/#{migrator}" + + if Jekyll.const_defiend?(MIGRATORS[migrator.to_sym]) + puts 'Importing...' + migrator_class = Jekyll.const_get(MIGRATORS[migrator.to_sym]) + migrator_class.process(*cmd_options) + exit 0 + end + end + + abort 'invalid migrator. Please specify a valid migrator' + end end end - end diff --git a/lib/jekyll/commands/serve.rb b/lib/jekyll/commands/serve.rb index df717bea..1ee58380 100644 --- a/lib/jekyll/commands/serve.rb +++ b/lib/jekyll/commands/serve.rb @@ -1,28 +1,28 @@ module Jekyll + module Commands + class Serve < Command + def self.process(options) + require 'webrick' + include WEBrick - class ServeCommand < Command - def self.process(options) - require 'webrick' - include WEBrick + destination = options['destination'] - destination = options['destination'] + FileUtils.mkdir_p(destination) - FileUtils.mkdir_p(destination) + mime_types = WEBrick::HTTPUtils::DefaultMimeTypes + mime_types.store 'js', 'application/javascript' - mime_types = WEBrick::HTTPUtils::DefaultMimeTypes - mime_types.store 'js', 'application/javascript' + s = HTTPServer.new( + :Port => options['port'], + :BindAddress => options['host'], + :MimeTypes => mime_types + ) - 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() + s.mount(options['baseurl'], HTTPServlet::FileHandler, destination) + t = Thread.new { s.start } + trap("INT") { s.shutdown } + t.join() + end end end - end