Simplyify local templates, add docs, enable mobile html5b
This commit is contained in:
parent
0dbd4c06c9
commit
6f7c2f881d
7 changed files with 78 additions and 35 deletions
|
@ -156,6 +156,12 @@ module Middleman
|
||||||
|
|
||||||
class << self
|
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
|
# Automatically load extensions from available RubyGems
|
||||||
# which contain the EXTENSION_FILE
|
# which contain the EXTENSION_FILE
|
||||||
#
|
#
|
||||||
|
|
|
@ -16,7 +16,7 @@ module Middleman
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "init NAME [options]", "Create new project NAME"
|
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",
|
method_option "template",
|
||||||
:aliases => "-T",
|
:aliases => "-T",
|
||||||
:default => "default",
|
:default => "default",
|
||||||
|
@ -40,11 +40,11 @@ module Middleman
|
||||||
:desc => 'Create a Gemfile and use Bundler to manage gems'
|
:desc => 'Create a Gemfile and use Bundler to manage gems'
|
||||||
def init(name)
|
def init(name)
|
||||||
key = options[:template].to_sym
|
key = options[:template].to_sym
|
||||||
unless Middleman::Templates.registered_templates.has_key?(key)
|
unless Middleman::Templates.registered.has_key?(key)
|
||||||
key = :default
|
key = :default
|
||||||
end
|
end
|
||||||
|
|
||||||
thor_group = Middleman::Templates.registered_templates[key]
|
thor_group = Middleman::Templates.registered[key]
|
||||||
thor_group.new([name], options).invoke_all
|
thor_group.new([name], options).invoke_all
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,44 +1,62 @@
|
||||||
|
# Use thor for template generation
|
||||||
require "thor"
|
require "thor"
|
||||||
require "thor/group"
|
require "thor/group"
|
||||||
|
|
||||||
|
# Templates namespace
|
||||||
module Middleman::Templates
|
module Middleman::Templates
|
||||||
@@template_mappings = {}
|
|
||||||
def self.register(name, klass)
|
# Static methods
|
||||||
@@template_mappings[name] = klass
|
class << self
|
||||||
end
|
|
||||||
|
# Get list of registered templates and add new ones
|
||||||
def self.registered_names
|
#
|
||||||
@@template_mappings.keys
|
# @param [Symbol] name The name of the template
|
||||||
end
|
# @param [Class] klass The class to be executed for this template
|
||||||
|
# @return [Hash] List of registered templates
|
||||||
def self.registered_templates
|
def registered(*args)
|
||||||
@@template_mappings
|
@_template_mappings ||= {}
|
||||||
|
@_template_mappings[args[0]] = args[1] if args.length == 2
|
||||||
|
@_template_mappings
|
||||||
|
end
|
||||||
|
|
||||||
|
# Middleman::Templates.register(name, klass)
|
||||||
|
alias :register :registered
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Base Template class. Handles basic options and paths.
|
||||||
class Base < ::Thor::Group
|
class Base < ::Thor::Group
|
||||||
include Thor::Actions
|
include Thor::Actions
|
||||||
|
|
||||||
|
# Required path for the new project to be generated
|
||||||
argument :location, :type => :string
|
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
|
def generate_rack
|
||||||
if options[:rack]
|
return unless options[:rack]
|
||||||
template "shared/config.ru", File.join(location, "config.ru")
|
template "shared/config.ru", File.join(location, "config.ru")
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Output a Gemfile file for Bundler if --bundler is passed
|
||||||
|
class_option :bundler, :type => :boolean, :default => false
|
||||||
def generate_bundler
|
def generate_bundler
|
||||||
if options[:bundler]
|
return unless options[:bundler]
|
||||||
template "shared/Gemfile.tt", File.join(location, "Gemfile")
|
template "shared/Gemfile.tt", File.join(location, "Gemfile")
|
||||||
|
|
||||||
say_status :run, "bundle install"
|
say_status :run, "bundle install"
|
||||||
print `cd #{location} && "#{Gem.ruby}" -rubygems "#{Gem.bin_path('bundler', 'bundle')}" install`
|
print `cd #{location} && "#{Gem.ruby}" -rubygems "#{Gem.bin_path('bundler', 'bundle')}" install`
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -49,5 +67,8 @@ require "middleman/templates/default"
|
||||||
# HTML5 template
|
# HTML5 template
|
||||||
require "middleman/templates/html5"
|
require "middleman/templates/html5"
|
||||||
|
|
||||||
|
# HTML5 Mobile template
|
||||||
|
require "middleman/templates/mobile"
|
||||||
|
|
||||||
# Local templates
|
# Local templates
|
||||||
require "middleman/templates/local"
|
require "middleman/templates/local"
|
|
@ -1,8 +1,12 @@
|
||||||
|
# Default Middleman template
|
||||||
class Middleman::Templates::Default < Middleman::Templates::Base
|
class Middleman::Templates::Default < Middleman::Templates::Base
|
||||||
|
|
||||||
|
# Template files are relative to this file
|
||||||
def self.source_root
|
def self.source_root
|
||||||
File.dirname(__FILE__)
|
File.dirname(__FILE__)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Actually output the files
|
||||||
def build_scaffold
|
def build_scaffold
|
||||||
template "shared/config.tt", File.join(location, "config.rb")
|
template "shared/config.tt", File.join(location, "config.rb")
|
||||||
copy_file "default/source/index.html.erb", File.join(location, "source/index.html.erb")
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Register this template
|
||||||
Middleman::Templates.register(:default, Middleman::Templates::Default)
|
Middleman::Templates.register(:default, Middleman::Templates::Default)
|
|
@ -1,12 +1,17 @@
|
||||||
|
# HTML5 Boilerplate template
|
||||||
class Middleman::Templates::Html5 < Middleman::Templates::Base
|
class Middleman::Templates::Html5 < Middleman::Templates::Base
|
||||||
|
|
||||||
|
# Has different default paths
|
||||||
class_option :css_dir, :default => "css"
|
class_option :css_dir, :default => "css"
|
||||||
class_option :js_dir, :default => "js"
|
class_option :js_dir, :default => "js"
|
||||||
class_option :images_dir, :default => "img"
|
class_option :images_dir, :default => "img"
|
||||||
|
|
||||||
|
# Templates are relative to this file
|
||||||
def self.source_root
|
def self.source_root
|
||||||
File.dirname(__FILE__)
|
File.dirname(__FILE__)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Output the files
|
||||||
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")
|
||||||
|
@ -14,4 +19,5 @@ class Middleman::Templates::Html5 < Middleman::Templates::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Register the template
|
||||||
Middleman::Templates.register(:html5, Middleman::Templates::Html5)
|
Middleman::Templates.register(:html5, Middleman::Templates::Html5)
|
|
@ -1,19 +1,18 @@
|
||||||
module Middleman
|
# Local templates
|
||||||
def self.templates_path
|
|
||||||
File.join(File.expand_path("~/"), ".middleman")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Middleman::Templates::Local < Middleman::Templates::Base
|
class Middleman::Templates::Local < Middleman::Templates::Base
|
||||||
|
|
||||||
|
# Look for templates in ~/.middleman
|
||||||
def self.source_root
|
def self.source_root
|
||||||
Middleman.templates_path
|
Middleman.templates_path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Just copy from the template path
|
||||||
def build_scaffold
|
def build_scaffold
|
||||||
directory options[:template].to_s, location
|
directory options[:template].to_s, location
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Iterate over the directories in the templates path and register each one.
|
||||||
Dir[File.join(Middleman.templates_path, "*")].each do |dir|
|
Dir[File.join(Middleman.templates_path, "*")].each do |dir|
|
||||||
next unless File.directory?(dir)
|
next unless File.directory?(dir)
|
||||||
Middleman::Templates.register(File.basename(dir).to_sym, Middleman::Templates::Local)
|
Middleman::Templates.register(File.basename(dir).to_sym, Middleman::Templates::Local)
|
||||||
|
|
|
@ -1,12 +1,17 @@
|
||||||
|
# Mobile HTML5 Boilerplate
|
||||||
class Middleman::Templates::Mobile < Middleman::Templates::Base
|
class Middleman::Templates::Mobile < Middleman::Templates::Base
|
||||||
|
|
||||||
|
# Slightly different paths
|
||||||
class_option :css_dir, :default => "css"
|
class_option :css_dir, :default => "css"
|
||||||
class_option :js_dir, :default => "js"
|
class_option :js_dir, :default => "js"
|
||||||
class_option :images_dir, :default => "img"
|
class_option :images_dir, :default => "img"
|
||||||
|
|
||||||
|
# Template files are relative to this file
|
||||||
def self.source_root
|
def self.source_root
|
||||||
File.dirname(__FILE__)
|
File.dirname(__FILE__)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Output the files
|
||||||
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")
|
||||||
|
@ -14,4 +19,5 @@ class Middleman::Templates::Mobile < Middleman::Templates::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Register the template
|
||||||
Middleman::Templates.register(:mobile, Middleman::Templates::Mobile)
|
Middleman::Templates.register(:mobile, Middleman::Templates::Mobile)
|
Loading…
Add table
Reference in a new issue