From b29413bcd3d8ce4717f7452327e50848c5f9fda0 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Tue, 12 Apr 2011 20:32:13 -0700 Subject: [PATCH] make mm-init templates extensible. closes #18. --- bin/mm-init | 26 ++++----------- features/step_definitions/generator_steps.rb | 2 +- lib/middleman/server.rb | 6 ++-- lib/middleman/templates.rb | 31 ++++++++++++++++++ lib/middleman/templates/default.rb | 16 +++++++++ lib/middleman/templates/html5.rb | 13 ++++++++ lib/middleman/templates/html5/config.ru | 4 +++ .../{html5boilerplate => html5}/config.tt | 0 .../public/404.html | 0 .../public/apple-touch-icon.png | Bin .../public/crossdomain.xml | 0 .../public/css/handheld.css | 0 .../public/css/style.css | 0 .../public/favicon.ico | Bin .../public/humans.txt | 0 .../public/images/.gitignore | 0 .../public/index.html | 0 .../public/js/libs/dd_belatedpng.js | 0 .../public/js/libs/jquery-1.5.0.js | 0 .../public/js/libs/jquery-1.5.0.min.js | 0 .../public/js/libs/modernizr-1.6.min.js | 0 .../public/js/mylibs/.gitignore | 0 .../public/js/plugins.js | 0 .../public/js/script.js | 0 .../public/robots.txt | 0 25 files changed, 75 insertions(+), 23 deletions(-) create mode 100644 lib/middleman/templates.rb create mode 100644 lib/middleman/templates/default.rb create mode 100644 lib/middleman/templates/html5.rb create mode 100644 lib/middleman/templates/html5/config.ru rename lib/middleman/templates/{html5boilerplate => html5}/config.tt (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/404.html (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/apple-touch-icon.png (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/crossdomain.xml (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/css/handheld.css (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/css/style.css (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/favicon.ico (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/humans.txt (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/images/.gitignore (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/index.html (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/js/libs/dd_belatedpng.js (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/js/libs/jquery-1.5.0.js (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/js/libs/jquery-1.5.0.min.js (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/js/libs/modernizr-1.6.min.js (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/js/mylibs/.gitignore (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/js/plugins.js (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/js/script.js (100%) rename lib/middleman/templates/{html5boilerplate => html5}/public/robots.txt (100%) diff --git a/bin/mm-init b/bin/mm-init index 3e59770a..3a6c112b 100755 --- a/bin/mm-init +++ b/bin/mm-init @@ -1,7 +1,6 @@ #!/usr/bin/env ruby require File.join(File.dirname(File.dirname(__FILE__)), 'lib', 'middleman') -require "thor" -require "thor/group" +require "middleman/templates" module Middleman class Generator < ::Thor::Group @@ -9,29 +8,18 @@ module Middleman argument :location, :type => :string, :desc => "New project location" - class_option :template, :aliases => "-T", :default => "default", :desc => 'Optionally use a pre-defined project template: html5, default' - - def self.source_root - File.join(File.dirname(__FILE__), '..', 'lib', 'middleman', 'templates') - end + available_templates = Middleman::Templates.registered_names.join(", ") + class_option :template, :aliases => "-T", :default => "default", :desc => "Optionally use a pre-defined project template: #{available_templates}" class_option :css_dir, :default => "stylesheets", :desc => 'The path to the css files' class_option :js_dir, :default => "javascripts", :desc => 'The path to the javascript files' class_option :images_dir, :default => "images", :desc => 'The path to the image files' def create_project - if options[:template] == "html5" - template "html5boilerplate/config.tt", File.join(location, "config.rb") - directory "html5boilerplate/public", File.join(location, "public") - empty_directory File.join(location, "views") - else - template "default/config.tt", File.join(location, "config.rb") - template "default/config.ru", File.join(location, "config.ru") - directory "default/views", File.join(location, "views") - empty_directory File.join(location, "public", options[:css_dir]) - empty_directory File.join(location, "public", options[:js_dir]) - empty_directory File.join(location, "public", options[:images_dir]) - end + key = options[:template].to_sym + key = :default unless Middleman::Templates.registered_templates.has_key?(key) + + Middleman::Templates.registered_templates[key].start end end end diff --git a/features/step_definitions/generator_steps.rb b/features/step_definitions/generator_steps.rb index 5e000e39..d90adaf8 100644 --- a/features/step_definitions/generator_steps.rb +++ b/features/step_definitions/generator_steps.rb @@ -8,7 +8,7 @@ end Then /^template files should exist at "([^\"]*)"$/ do |dirname| target = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "fixtures", dirname) - template_glob = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "lib", "middleman", "template", "*/**/*") + template_glob = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "lib", "middleman", "templates", "default", "*/**/*") Dir[template_glob].each do |f| next if File.directory?(f) diff --git a/lib/middleman/server.rb b/lib/middleman/server.rb index 03130a57..2458f911 100644 --- a/lib/middleman/server.rb +++ b/lib/middleman/server.rb @@ -9,7 +9,7 @@ module Middleman class Server < Sinatra::Base # Basic Sinatra config set :app_file, __FILE__ - #set :root, ENV["MM_DIR"] || Dir.pwd + set :root, ENV["MM_DIR"] || Dir.pwd set :reload, false set :sessions, false set :logging, false @@ -154,7 +154,7 @@ require "middleman/assets" # The Rack App class Middleman::Server - def self.new(*args, &block) + def self.new(*args, &block) # Check for and evaluate local configuration local_config = File.join(self.root, "config.rb") if File.exists? local_config @@ -162,7 +162,7 @@ class Middleman::Server Middleman::Server.class_eval File.read(local_config) set :app_file, File.expand_path(local_config) end - + use ::Rack::ConditionalGet use ::Rack::Static, :urls => ["/#{self.images_dir}"], :root => "public" diff --git a/lib/middleman/templates.rb b/lib/middleman/templates.rb new file mode 100644 index 00000000..45d683f3 --- /dev/null +++ b/lib/middleman/templates.rb @@ -0,0 +1,31 @@ +require "thor" +require "thor/group" + +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 + end + + class Base < ::Thor::Group + include Thor::Actions + + argument :location, :type => :string + class_option :css_dir, :default => "stylesheets", :desc => 'The path to the css files' + class_option :js_dir, :default => "javascripts", :desc => 'The path to the javascript files' + class_option :images_dir, :default => "images", :desc => 'The path to the image files' + end +end + +# Default template +require "middleman/templates/default" +# HTML5 template +require "middleman/templates/html5" \ No newline at end of file diff --git a/lib/middleman/templates/default.rb b/lib/middleman/templates/default.rb new file mode 100644 index 00000000..f6b6dc2b --- /dev/null +++ b/lib/middleman/templates/default.rb @@ -0,0 +1,16 @@ +class Middleman::Templates::Default < Middleman::Templates::Base + def self.source_root + File.join(File.dirname(__FILE__), 'default') + end + + def build_scaffold + template "config.tt", File.join(location, "config.rb") + template "config.ru", File.join(location, "config.ru") + directory "views", File.join(location, "views") + empty_directory File.join(location, "public", options[:css_dir]) + empty_directory File.join(location, "public", options[:js_dir]) + empty_directory File.join(location, "public", options[:images_dir]) + end +end + +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 new file mode 100644 index 00000000..0b5eea24 --- /dev/null +++ b/lib/middleman/templates/html5.rb @@ -0,0 +1,13 @@ +class Middleman::Templates::Html5 < Middleman::Templates::Base + def self.source_root + File.join(File.dirname(__FILE__), 'html5') + end + + def build_scaffold + template "config.tt", File.join(location, "config.rb") + directory "public", File.join(location, "public") + empty_directory File.join(location, "views") + end +end + +Middleman::Templates.register(:html5, Middleman::Templates::Html5) \ No newline at end of file diff --git a/lib/middleman/templates/html5/config.ru b/lib/middleman/templates/html5/config.ru new file mode 100644 index 00000000..9820e4d9 --- /dev/null +++ b/lib/middleman/templates/html5/config.ru @@ -0,0 +1,4 @@ +require 'rubygems' +require 'middleman' + +run Middleman::Server \ No newline at end of file diff --git a/lib/middleman/templates/html5boilerplate/config.tt b/lib/middleman/templates/html5/config.tt similarity index 100% rename from lib/middleman/templates/html5boilerplate/config.tt rename to lib/middleman/templates/html5/config.tt diff --git a/lib/middleman/templates/html5boilerplate/public/404.html b/lib/middleman/templates/html5/public/404.html similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/404.html rename to lib/middleman/templates/html5/public/404.html diff --git a/lib/middleman/templates/html5boilerplate/public/apple-touch-icon.png b/lib/middleman/templates/html5/public/apple-touch-icon.png similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/apple-touch-icon.png rename to lib/middleman/templates/html5/public/apple-touch-icon.png diff --git a/lib/middleman/templates/html5boilerplate/public/crossdomain.xml b/lib/middleman/templates/html5/public/crossdomain.xml similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/crossdomain.xml rename to lib/middleman/templates/html5/public/crossdomain.xml diff --git a/lib/middleman/templates/html5boilerplate/public/css/handheld.css b/lib/middleman/templates/html5/public/css/handheld.css similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/css/handheld.css rename to lib/middleman/templates/html5/public/css/handheld.css diff --git a/lib/middleman/templates/html5boilerplate/public/css/style.css b/lib/middleman/templates/html5/public/css/style.css similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/css/style.css rename to lib/middleman/templates/html5/public/css/style.css diff --git a/lib/middleman/templates/html5boilerplate/public/favicon.ico b/lib/middleman/templates/html5/public/favicon.ico similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/favicon.ico rename to lib/middleman/templates/html5/public/favicon.ico diff --git a/lib/middleman/templates/html5boilerplate/public/humans.txt b/lib/middleman/templates/html5/public/humans.txt similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/humans.txt rename to lib/middleman/templates/html5/public/humans.txt diff --git a/lib/middleman/templates/html5boilerplate/public/images/.gitignore b/lib/middleman/templates/html5/public/images/.gitignore similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/images/.gitignore rename to lib/middleman/templates/html5/public/images/.gitignore diff --git a/lib/middleman/templates/html5boilerplate/public/index.html b/lib/middleman/templates/html5/public/index.html similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/index.html rename to lib/middleman/templates/html5/public/index.html diff --git a/lib/middleman/templates/html5boilerplate/public/js/libs/dd_belatedpng.js b/lib/middleman/templates/html5/public/js/libs/dd_belatedpng.js similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/js/libs/dd_belatedpng.js rename to lib/middleman/templates/html5/public/js/libs/dd_belatedpng.js diff --git a/lib/middleman/templates/html5boilerplate/public/js/libs/jquery-1.5.0.js b/lib/middleman/templates/html5/public/js/libs/jquery-1.5.0.js similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/js/libs/jquery-1.5.0.js rename to lib/middleman/templates/html5/public/js/libs/jquery-1.5.0.js diff --git a/lib/middleman/templates/html5boilerplate/public/js/libs/jquery-1.5.0.min.js b/lib/middleman/templates/html5/public/js/libs/jquery-1.5.0.min.js similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/js/libs/jquery-1.5.0.min.js rename to lib/middleman/templates/html5/public/js/libs/jquery-1.5.0.min.js diff --git a/lib/middleman/templates/html5boilerplate/public/js/libs/modernizr-1.6.min.js b/lib/middleman/templates/html5/public/js/libs/modernizr-1.6.min.js similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/js/libs/modernizr-1.6.min.js rename to lib/middleman/templates/html5/public/js/libs/modernizr-1.6.min.js diff --git a/lib/middleman/templates/html5boilerplate/public/js/mylibs/.gitignore b/lib/middleman/templates/html5/public/js/mylibs/.gitignore similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/js/mylibs/.gitignore rename to lib/middleman/templates/html5/public/js/mylibs/.gitignore diff --git a/lib/middleman/templates/html5boilerplate/public/js/plugins.js b/lib/middleman/templates/html5/public/js/plugins.js similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/js/plugins.js rename to lib/middleman/templates/html5/public/js/plugins.js diff --git a/lib/middleman/templates/html5boilerplate/public/js/script.js b/lib/middleman/templates/html5/public/js/script.js similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/js/script.js rename to lib/middleman/templates/html5/public/js/script.js diff --git a/lib/middleman/templates/html5boilerplate/public/robots.txt b/lib/middleman/templates/html5/public/robots.txt similarity index 100% rename from lib/middleman/templates/html5boilerplate/public/robots.txt rename to lib/middleman/templates/html5/public/robots.txt