Simplyify local templates, add docs, enable mobile html5b

This commit is contained in:
Thomas Reynolds 2011-12-10 11:17:47 -08:00
parent 0dbd4c06c9
commit 6f7c2f881d
7 changed files with 78 additions and 35 deletions

View file

@ -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
#

View file

@ -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

View file

@ -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"

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)