Skip to content

Commit

Permalink
make mm-init templates extensible. closes middleman#18.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdreyno committed Apr 13, 2011
1 parent 4598e42 commit b29413b
Show file tree
Hide file tree
Showing 25 changed files with 75 additions and 23 deletions.
26 changes: 7 additions & 19 deletions bin/mm-init
@@ -1,37 +1,25 @@
#!/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
include Thor::Actions

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
Expand Down
2 changes: 1 addition & 1 deletion features/step_definitions/generator_steps.rb
Expand Up @@ -8,7 +8,7 @@

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)
Expand Down
6 changes: 3 additions & 3 deletions lib/middleman/server.rb
Expand Up @@ -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
Expand Down Expand Up @@ -154,15 +154,15 @@ def process_request(options={})

# 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
$stderr.puts "== Reading: Local config" if logging?
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"

Expand Down
31 changes: 31 additions & 0 deletions 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"
16 changes: 16 additions & 0 deletions 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)
13 changes: 13 additions & 0 deletions 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)
4 changes: 4 additions & 0 deletions lib/middleman/templates/html5/config.ru
@@ -0,0 +1,4 @@
require 'rubygems'
require 'middleman'

run Middleman::Server
File renamed without changes.

0 comments on commit b29413b

Please sign in to comment.