diff --git a/middleman-templates/lib/middleman-templates.rb b/middleman-templates/lib/middleman-templates.rb index d6827b4a..2e8a343c 100644 --- a/middleman-templates/lib/middleman-templates.rb +++ b/middleman-templates/lib/middleman-templates.rb @@ -9,10 +9,8 @@ require 'thor/group' # Templates Module module Middleman module Templates - # Static methods class << self - # Get list of registered templates and add new ones # # Middleman::Templates.register(:ext_name, klass) @@ -20,14 +18,14 @@ module Middleman # @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 register(name=nil, klass=nil) + def register(name = nil, klass = nil) @_template_mappings ||= {} @_template_mappings[name] = klass if name && klass @_template_mappings end # Middleman::Templates.register(name, klass) - alias :registered :register + alias_method :registered, :register end # Base Template class. Handles basic options and paths. @@ -46,13 +44,13 @@ module Middleman end # 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. - class_option :template, :default => 'default' + class_option :template, default: 'default' # 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 # @return [void] @@ -61,7 +59,8 @@ module Middleman template 'shared/config.ru', File.join(location, 'config.ru') 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 # @return [void] @@ -74,7 +73,7 @@ module Middleman end # 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 # @return [void] @@ -86,4 +85,19 @@ module Middleman end end -Dir.glob(File.expand_path("../middleman-templates/*.rb", __FILE__), &method(:require)) \ No newline at end of file +# 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 diff --git a/middleman-templates/lib/middleman-templates/default.rb b/middleman-templates/lib/middleman-templates/default.rb index 7497f98f..3eb1ff52 100644 --- a/middleman-templates/lib/middleman-templates/default.rb +++ b/middleman-templates/lib/middleman-templates/default.rb @@ -1,15 +1,8 @@ # Default Middleman template class Middleman::Templates::Default < Middleman::Templates::Base - - 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' + 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' # Template files are relative to this file # @return [String] @@ -17,7 +10,7 @@ class Middleman::Templates::Default < Middleman::Templates::Base File.dirname(__FILE__) end - # Actually output the files + # Output the files # @return [void] def build_scaffold! template 'shared/config.tt', File.join(location, 'config.rb') diff --git a/middleman-templates/lib/middleman-templates/empty.rb b/middleman-templates/lib/middleman-templates/empty.rb index e726f1c2..18ac3106 100644 --- a/middleman-templates/lib/middleman-templates/empty.rb +++ b/middleman-templates/lib/middleman-templates/empty.rb @@ -1,21 +1,17 @@ # A barebones template with nothing much in it class Middleman::Templates::Empty < Middleman::Templates::Base - # Template files are relative to this file # @return [String] def self.source_root File.dirname(__FILE__) end - def self.gemfile_template - 'empty/Gemfile.tt' - end - - # Actually output the files + # Output the files # @return [void] 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') + create_file File.join(location, 'source', '.gitkeep') unless options[:'skip-git'] end end diff --git a/middleman-templates/lib/middleman-templates/empty/Gemfile.tt b/middleman-templates/lib/middleman-templates/empty/Gemfile.tt deleted file mode 100644 index 6d51559a..00000000 --- a/middleman-templates/lib/middleman-templates/empty/Gemfile.tt +++ /dev/null @@ -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] \ No newline at end of file diff --git a/middleman-templates/lib/middleman-templates/html5.rb b/middleman-templates/lib/middleman-templates/html5.rb index 6e8391cb..de0bf25d 100644 --- a/middleman-templates/lib/middleman-templates/html5.rb +++ b/middleman-templates/lib/middleman-templates/html5.rb @@ -1,17 +1,11 @@ # HTML5 Boilerplate template 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', - :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 + # Template files are relative to this file # @return [String] def self.source_root File.dirname(__FILE__) @@ -22,9 +16,8 @@ class Middleman::Templates::Html5 < Middleman::Templates::Base def build_scaffold! template 'shared/config.tt', File.join(location, 'config.rb') directory 'html5/source', File.join(location, 'source') - empty_directory File.join(location, 'source') end end -# Register the template +# Register this template Middleman::Templates.register(:html5, Middleman::Templates::Html5) diff --git a/middleman-templates/lib/middleman-templates/local.rb b/middleman-templates/lib/middleman-templates/local.rb index c5ba9c79..d9a9711a 100644 --- a/middleman-templates/lib/middleman-templates/local.rb +++ b/middleman-templates/lib/middleman-templates/local.rb @@ -1,6 +1,5 @@ # Local templates class Middleman::Templates::Local < Middleman::Templates::Base - # Look for templates in ~/.middleman # @return [String] def self.source_root @@ -14,15 +13,5 @@ class Middleman::Templates::Local < Middleman::Templates::Base end end -# Iterate over the 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 File.exists?(template_file) - require template_file - else - Middleman::Templates.register(File.basename(dir).to_sym, Middleman::Templates::Local) - end -end +# Register this template +Middleman::Templates.register(:local, Middleman::Templates::Local) diff --git a/middleman-templates/lib/middleman-templates/mobile.rb b/middleman-templates/lib/middleman-templates/mobile.rb index 8e7e3530..39186271 100644 --- a/middleman-templates/lib/middleman-templates/mobile.rb +++ b/middleman-templates/lib/middleman-templates/mobile.rb @@ -1,10 +1,9 @@ # 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' + 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' # Template files are relative to this file # @return [String] @@ -17,9 +16,8 @@ class Middleman::Templates::Mobile < Middleman::Templates::Base def build_scaffold! template 'shared/config.tt', File.join(location, 'config.rb') directory 'mobile/source', File.join(location, 'source') - empty_directory File.join(location, 'source') end end -# Register the template +# Register this template Middleman::Templates.register(:mobile, Middleman::Templates::Mobile) diff --git a/middleman-templates/lib/middleman-templates/shared/config.ru b/middleman-templates/lib/middleman-templates/shared/config.ru index e091b492..9bff44ef 100644 --- a/middleman-templates/lib/middleman-templates/shared/config.ru +++ b/middleman-templates/lib/middleman-templates/shared/config.ru @@ -1,4 +1,4 @@ require 'rubygems' require 'middleman/rack' -run Middleman.server \ No newline at end of file +run Middleman.server