From 6f7c2f881d181aa3ddfc126796ed74634b60b404 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sat, 10 Dec 2011 11:17:47 -0800 Subject: [PATCH] Simplyify local templates, add docs, enable mobile html5b --- lib/middleman.rb | 6 +++ lib/middleman/cli.rb | 6 +-- lib/middleman/templates.rb | 73 +++++++++++++++++++----------- lib/middleman/templates/default.rb | 5 ++ lib/middleman/templates/html5.rb | 6 +++ lib/middleman/templates/local.rb | 11 ++--- lib/middleman/templates/mobile.rb | 6 +++ 7 files changed, 78 insertions(+), 35 deletions(-) diff --git a/lib/middleman.rb b/lib/middleman.rb index aeb4da78..8944a11e 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -156,6 +156,12 @@ module Middleman class << self + # Where to look for custom templates + # @returns [String] + def templates_path + File.join(File.expand_path("~/"), ".middleman") + end + # Automatically load extensions from available RubyGems # which contain the EXTENSION_FILE # diff --git a/lib/middleman/cli.rb b/lib/middleman/cli.rb index ac0ff6f5..ebdb0843 100644 --- a/lib/middleman/cli.rb +++ b/lib/middleman/cli.rb @@ -16,7 +16,7 @@ module Middleman end desc "init NAME [options]", "Create new project NAME" - available_templates = Middleman::Templates.registered_names.join(", ") + available_templates = Middleman::Templates.registered.keys.join(", ") method_option "template", :aliases => "-T", :default => "default", @@ -40,11 +40,11 @@ module Middleman :desc => 'Create a Gemfile and use Bundler to manage gems' def init(name) key = options[:template].to_sym - unless Middleman::Templates.registered_templates.has_key?(key) + unless Middleman::Templates.registered.has_key?(key) key = :default end - thor_group = Middleman::Templates.registered_templates[key] + thor_group = Middleman::Templates.registered[key] thor_group.new([name], options).invoke_all end diff --git a/lib/middleman/templates.rb b/lib/middleman/templates.rb index ec76adc1..4825833e 100644 --- a/lib/middleman/templates.rb +++ b/lib/middleman/templates.rb @@ -1,44 +1,62 @@ +# Use thor for template generation require "thor" require "thor/group" +# Templates namespace module Middleman::Templates - @@template_mappings = {} - def self.register(name, klass) - @@template_mappings[name] = klass - end - - def self.registered_names - @@template_mappings.keys - end - - def self.registered_templates - @@template_mappings + + # Static methods + class << self + + # Get list of registered templates and add new ones + # + # @param [Symbol] name The name of the template + # @param [Class] klass The class to be executed for this template + # @return [Hash] List of registered templates + def registered(*args) + @_template_mappings ||= {} + @_template_mappings[args[0]] = args[1] if args.length == 2 + @_template_mappings + end + + # Middleman::Templates.register(name, klass) + alias :register :registered end + # Base Template class. Handles basic options and paths. class Base < ::Thor::Group include Thor::Actions + # Required path for the new project to be generated argument :location, :type => :string - class_option :template, :default => "default" - class_option :css_dir, :default => "stylesheets" - class_option :js_dir, :default => "javascripts" - class_option :images_dir, :default => "images" - class_option :rack, :type => :boolean, :default => false - class_option :bundler, :type => :boolean, :default => false + # Name of the template being used to generate the project. + class_option :template, :default => "default" + + # What to call the directory which CSS will be searched for. + class_option :css_dir, :default => "stylesheets" + + # What to call the directory which JS will be searched for. + class_option :js_dir, :default => "javascripts" + + # What to call the directory which images will be searched for. + class_option :images_dir, :default => "images" + + # Output a config.ru file for Rack if --rack is passed + class_option :rack, :type => :boolean, :default => false def generate_rack - if options[:rack] - template "shared/config.ru", File.join(location, "config.ru") - end + return unless options[:rack] + template "shared/config.ru", File.join(location, "config.ru") end + # Output a Gemfile file for Bundler if --bundler is passed + class_option :bundler, :type => :boolean, :default => false def generate_bundler - if options[:bundler] - template "shared/Gemfile.tt", File.join(location, "Gemfile") - - say_status :run, "bundle install" - print `cd #{location} && "#{Gem.ruby}" -rubygems "#{Gem.bin_path('bundler', 'bundle')}" install` - end + return unless options[:bundler] + template "shared/Gemfile.tt", File.join(location, "Gemfile") + + say_status :run, "bundle install" + print `cd #{location} && "#{Gem.ruby}" -rubygems "#{Gem.bin_path('bundler', 'bundle')}" install` end end end @@ -49,5 +67,8 @@ require "middleman/templates/default" # HTML5 template require "middleman/templates/html5" +# HTML5 Mobile template +require "middleman/templates/mobile" + # Local templates require "middleman/templates/local" \ No newline at end of file diff --git a/lib/middleman/templates/default.rb b/lib/middleman/templates/default.rb index f38ff85e..9931aa41 100644 --- a/lib/middleman/templates/default.rb +++ b/lib/middleman/templates/default.rb @@ -1,8 +1,12 @@ +# Default Middleman template class Middleman::Templates::Default < Middleman::Templates::Base + + # Template files are relative to this file def self.source_root File.dirname(__FILE__) end + # Actually output the files def build_scaffold template "shared/config.tt", File.join(location, "config.rb") copy_file "default/source/index.html.erb", File.join(location, "source/index.html.erb") @@ -14,4 +18,5 @@ class Middleman::Templates::Default < Middleman::Templates::Base end end +# Register this template Middleman::Templates.register(:default, Middleman::Templates::Default) \ No newline at end of file diff --git a/lib/middleman/templates/html5.rb b/lib/middleman/templates/html5.rb index a3630e47..72f11f86 100644 --- a/lib/middleman/templates/html5.rb +++ b/lib/middleman/templates/html5.rb @@ -1,12 +1,17 @@ +# HTML5 Boilerplate template class Middleman::Templates::Html5 < Middleman::Templates::Base + + # Has different default paths class_option :css_dir, :default => "css" class_option :js_dir, :default => "js" class_option :images_dir, :default => "img" + # Templates are relative to this file def self.source_root File.dirname(__FILE__) end + # Output the files def build_scaffold template "shared/config.tt", File.join(location, "config.rb") directory "html5/source", File.join(location, "source") @@ -14,4 +19,5 @@ class Middleman::Templates::Html5 < Middleman::Templates::Base end end +# Register the template Middleman::Templates.register(:html5, Middleman::Templates::Html5) \ No newline at end of file diff --git a/lib/middleman/templates/local.rb b/lib/middleman/templates/local.rb index a34f639e..1ea76ad3 100644 --- a/lib/middleman/templates/local.rb +++ b/lib/middleman/templates/local.rb @@ -1,19 +1,18 @@ -module Middleman - def self.templates_path - File.join(File.expand_path("~/"), ".middleman") - end -end - +# Local templates class Middleman::Templates::Local < Middleman::Templates::Base + + # Look for templates in ~/.middleman def self.source_root Middleman.templates_path end + # Just copy from the template path def build_scaffold directory options[:template].to_s, location end end +# Iterate over the directories in the templates path and register each one. Dir[File.join(Middleman.templates_path, "*")].each do |dir| next unless File.directory?(dir) Middleman::Templates.register(File.basename(dir).to_sym, Middleman::Templates::Local) diff --git a/lib/middleman/templates/mobile.rb b/lib/middleman/templates/mobile.rb index 15ee842f..bc9e6e35 100644 --- a/lib/middleman/templates/mobile.rb +++ b/lib/middleman/templates/mobile.rb @@ -1,12 +1,17 @@ +# Mobile HTML5 Boilerplate class Middleman::Templates::Mobile < Middleman::Templates::Base + + # Slightly different paths class_option :css_dir, :default => "css" class_option :js_dir, :default => "js" class_option :images_dir, :default => "img" + # Template files are relative to this file def self.source_root File.dirname(__FILE__) end + # Output the files def build_scaffold template "shared/config.tt", File.join(location, "config.rb") directory "mobile/source", File.join(location, "source") @@ -14,4 +19,5 @@ class Middleman::Templates::Mobile < Middleman::Templates::Base end end +# Register the template Middleman::Templates.register(:mobile, Middleman::Templates::Mobile) \ No newline at end of file