make mm-init templates extensible. closes #18.
This commit is contained in:
parent
4598e424b8
commit
b29413bcd3
26
bin/mm-init
26
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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
31
lib/middleman/templates.rb
Normal file
31
lib/middleman/templates.rb
Normal file
|
@ -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"
|
16
lib/middleman/templates/default.rb
Normal file
16
lib/middleman/templates/default.rb
Normal file
|
@ -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)
|
13
lib/middleman/templates/html5.rb
Normal file
13
lib/middleman/templates/html5.rb
Normal file
|
@ -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)
|
4
lib/middleman/templates/html5/config.ru
Normal file
4
lib/middleman/templates/html5/config.ru
Normal file
|
@ -0,0 +1,4 @@
|
|||
require 'rubygems'
|
||||
require 'middleman'
|
||||
|
||||
run Middleman::Server
|
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
Loading…
Reference in a new issue