Merge pull request #1201 from middleman/templates-rejig

Templates rejig
This commit is contained in:
Ben Hollis 2014-03-15 19:59:46 -07:00
commit 04d94d9b60
8 changed files with 44 additions and 67 deletions

View file

@ -9,10 +9,8 @@ require 'thor/group'
# Templates Module # Templates Module
module Middleman module Middleman
module Templates module Templates
# Static methods # Static methods
class << self class << self
# Get list of registered templates and add new ones # Get list of registered templates and add new ones
# #
# Middleman::Templates.register(:ext_name, klass) # Middleman::Templates.register(:ext_name, klass)
@ -20,14 +18,14 @@ module Middleman
# @param [Symbol] name The name of the template # @param [Symbol] name The name of the template
# @param [Class] klass The class to be executed for this template # @param [Class] klass The class to be executed for this template
# @return [Hash] List of registered templates # @return [Hash] List of registered templates
def register(name=nil, klass=nil) def register(name = nil, klass = nil)
@_template_mappings ||= {} @_template_mappings ||= {}
@_template_mappings[name] = klass if name && klass @_template_mappings[name] = klass if name && klass
@_template_mappings @_template_mappings
end end
# Middleman::Templates.register(name, klass) # Middleman::Templates.register(name, klass)
alias :registered :register alias_method :registered, :register
end end
# Base Template class. Handles basic options and paths. # Base Template class. Handles basic options and paths.
@ -46,13 +44,13 @@ module Middleman
end end
# Required path for the new project to be generated # Required path for the new project to be generated
argument :location, :type => :string argument :location, type: :string
# Name of the template being used to generate the project. # Name of the template being used to generate the project.
class_option :template, :default => 'default' class_option :template, default: 'default'
# Output a config.ru file for Rack if --rack is passed # Output a config.ru file for Rack if --rack is passed
class_option :rack, :type => :boolean, :default => false class_option :rack, type: :boolean, default: false
# Write a Rack config.ru file for project # Write a Rack config.ru file for project
# @return [void] # @return [void]
@ -61,7 +59,8 @@ module Middleman
template 'shared/config.ru', File.join(location, 'config.ru') template 'shared/config.ru', File.join(location, 'config.ru')
end end
class_option :'skip-bundle', :type => :boolean, :default => false # Do not run bundle install
class_option :'skip-bundle', type: :boolean, default: false
# Write a Bundler Gemfile file for project # Write a Bundler Gemfile file for project
# @return [void] # @return [void]
@ -74,7 +73,7 @@ module Middleman
end end
# Output a .gitignore file # Output a .gitignore file
class_option :'skip-git', :type => :boolean, :default => false class_option :'skip-git', type: :boolean, default: false
# Write a .gitignore file for project # Write a .gitignore file for project
# @return [void] # @return [void]
@ -86,4 +85,19 @@ module Middleman
end end
end end
Dir.glob(File.expand_path("../middleman-templates/*.rb", __FILE__), &method(:require)) # Register all official templates
Dir.glob(File.expand_path('../middleman-templates/*.rb', __FILE__), &method(:require))
# Iterate over directories in the templates path and register each one.
Dir[File.join(Middleman::Templates::Local.source_root, '*')].each do |dir|
next unless File.directory?(dir)
template_file = File.join(dir, 'template.rb')
# If a template.rb file is found require it (therefore registering the template)
# else register the folder as a Local template (which when built, just copies the folder)
if File.exists?(template_file)
require template_file
else
Middleman::Templates.register(File.basename(dir).to_sym, Middleman::Templates::Local)
end
end

View file

@ -1,15 +1,8 @@
# Default Middleman template # Default Middleman template
class Middleman::Templates::Default < Middleman::Templates::Base class Middleman::Templates::Default < Middleman::Templates::Base
class_option :css_dir, default: 'stylesheets', desc: 'The path to the css files'
class_option 'css_dir', class_option :js_dir, default: 'javascripts', desc: 'The path to the javascript files'
:default => 'stylesheets', class_option :images_dir, default: 'images', desc: 'The path to the image files'
: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'
# Template files are relative to this file # Template files are relative to this file
# @return [String] # @return [String]
@ -17,7 +10,7 @@ class Middleman::Templates::Default < Middleman::Templates::Base
File.dirname(__FILE__) File.dirname(__FILE__)
end end
# Actually output the files # Output the files
# @return [void] # @return [void]
def build_scaffold! def build_scaffold!
template 'shared/config.tt', File.join(location, 'config.rb') template 'shared/config.tt', File.join(location, 'config.rb')

View file

@ -1,21 +1,17 @@
# A barebones template with nothing much in it # A barebones template with nothing much in it
class Middleman::Templates::Empty < Middleman::Templates::Base class Middleman::Templates::Empty < Middleman::Templates::Base
# Template files are relative to this file # Template files are relative to this file
# @return [String] # @return [String]
def self.source_root def self.source_root
File.dirname(__FILE__) File.dirname(__FILE__)
end end
def self.gemfile_template # Output the files
'empty/Gemfile.tt'
end
# Actually output the files
# @return [void] # @return [void]
def build_scaffold! def build_scaffold!
create_file File.join(location, 'config.rb'), "\n" template 'shared/config.tt', File.join(location, 'config.rb')
empty_directory File.join(location, 'source') empty_directory File.join(location, 'source')
create_file File.join(location, 'source', '.gitkeep') unless options[:'skip-git']
end end
end end

View file

@ -1,6 +0,0 @@
source 'https://rubygems.org'
gem "middleman", "~><%= Middleman::VERSION %>"
# For faster file watcher updates on Windows:
gem "wdm", "~> 0.1.0", :platforms => [:mswin, :mingw]

View file

@ -1,17 +1,11 @@
# HTML5 Boilerplate template # HTML5 Boilerplate template
class Middleman::Templates::Html5 < Middleman::Templates::Base class Middleman::Templates::Html5 < Middleman::Templates::Base
# Slightly different paths
class_option :css_dir, default: 'css', desc: 'The path to the css files'
class_option :js_dir, default: 'js', desc: 'The path to the javascript files'
class_option :images_dir, default: 'img', desc: 'The path to the image files'
class_option 'css_dir', # Template files are relative to this file
:default => 'css',
:desc => 'The path to the css files'
class_option 'js_dir',
:default => 'js',
:desc => 'The path to the javascript files'
class_option 'images_dir',
:default => 'img',
:desc => 'The path to the image files'
# Templates are relative to this file
# @return [String] # @return [String]
def self.source_root def self.source_root
File.dirname(__FILE__) File.dirname(__FILE__)
@ -22,9 +16,8 @@ class Middleman::Templates::Html5 < Middleman::Templates::Base
def build_scaffold! def build_scaffold!
template 'shared/config.tt', File.join(location, 'config.rb') template 'shared/config.tt', File.join(location, 'config.rb')
directory 'html5/source', File.join(location, 'source') directory 'html5/source', File.join(location, 'source')
empty_directory File.join(location, 'source')
end end
end end
# Register the template # Register this template
Middleman::Templates.register(:html5, Middleman::Templates::Html5) Middleman::Templates.register(:html5, Middleman::Templates::Html5)

View file

@ -1,6 +1,5 @@
# Local templates # Local templates
class Middleman::Templates::Local < Middleman::Templates::Base class Middleman::Templates::Local < Middleman::Templates::Base
# Look for templates in ~/.middleman # Look for templates in ~/.middleman
# @return [String] # @return [String]
def self.source_root def self.source_root
@ -14,15 +13,5 @@ class Middleman::Templates::Local < Middleman::Templates::Base
end end
end end
# Iterate over the directories in the templates path and register each one. # Register this template
Dir[File.join(Middleman::Templates::Local.source_root, '*')].each do |dir| Middleman::Templates.register(:local, Middleman::Templates::Local)
next unless File.directory?(dir)
template_file = File.join(dir, 'template.rb')
if File.exists?(template_file)
require template_file
else
Middleman::Templates.register(File.basename(dir).to_sym, Middleman::Templates::Local)
end
end

View file

@ -1,10 +1,9 @@
# Mobile HTML5 Boilerplate # Mobile HTML5 Boilerplate
class Middleman::Templates::Mobile < Middleman::Templates::Base class Middleman::Templates::Mobile < Middleman::Templates::Base
# Slightly different paths # Slightly different paths
class_option :css_dir, :default => 'css' class_option :css_dir, default: 'css', desc: 'The path to the css files'
class_option :js_dir, :default => 'js' class_option :js_dir, default: 'js', desc: 'The path to the javascript files'
class_option :images_dir, :default => 'img' class_option :images_dir, default: 'img', desc: 'The path to the image files'
# Template files are relative to this file # Template files are relative to this file
# @return [String] # @return [String]
@ -17,9 +16,8 @@ class Middleman::Templates::Mobile < Middleman::Templates::Base
def build_scaffold! def build_scaffold!
template 'shared/config.tt', File.join(location, 'config.rb') template 'shared/config.tt', File.join(location, 'config.rb')
directory 'mobile/source', File.join(location, 'source') directory 'mobile/source', File.join(location, 'source')
empty_directory File.join(location, 'source')
end end
end end
# Register the template # Register this template
Middleman::Templates.register(:mobile, Middleman::Templates::Mobile) Middleman::Templates.register(:mobile, Middleman::Templates::Mobile)

View file

@ -1,4 +1,4 @@
require 'rubygems' require 'rubygems'
require 'middleman/rack' require 'middleman/rack'
run Middleman.server run Middleman.server