2014-04-29 20:43:05 +02:00
|
|
|
# rubocop:disable FileName
|
|
|
|
|
2014-03-11 12:07:55 +01:00
|
|
|
# Setup our load paths
|
|
|
|
libdir = File.expand_path(File.dirname(__FILE__))
|
|
|
|
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
|
|
|
|
|
|
|
|
# Require Thor since that's what the whole CLI is built around
|
|
|
|
require 'thor'
|
|
|
|
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)
|
|
|
|
#
|
|
|
|
# @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
|
2014-04-29 19:50:21 +02:00
|
|
|
def register(name=nil, klass=nil)
|
2014-03-11 12:07:55 +01:00
|
|
|
@_template_mappings ||= {}
|
|
|
|
@_template_mappings[name] = klass if name && klass
|
|
|
|
@_template_mappings
|
|
|
|
end
|
|
|
|
|
|
|
|
# Middleman::Templates.register(name, klass)
|
2014-03-13 11:57:19 +01:00
|
|
|
alias_method :registered, :register
|
2014-03-11 12:07:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Base Template class. Handles basic options and paths.
|
|
|
|
class Base < ::Thor::Group
|
|
|
|
include Thor::Actions
|
|
|
|
|
|
|
|
def initialize(names, options)
|
|
|
|
super
|
|
|
|
source_paths << File.join(File.dirname(__FILE__), 'middleman-templates')
|
|
|
|
end
|
|
|
|
|
|
|
|
# The gemfile template to use. Individual templates can define this class
|
|
|
|
# method to override the template path.
|
|
|
|
def self.gemfile_template
|
|
|
|
'shared/Gemfile.tt'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Required path for the new project to be generated
|
2014-03-13 11:57:19 +01:00
|
|
|
argument :location, type: :string
|
2014-03-11 12:07:55 +01:00
|
|
|
|
|
|
|
# Name of the template being used to generate the project.
|
2014-03-13 11:57:19 +01:00
|
|
|
class_option :template, default: 'default'
|
2014-03-11 12:07:55 +01:00
|
|
|
|
|
|
|
# Output a config.ru file for Rack if --rack is passed
|
2014-03-13 11:57:19 +01:00
|
|
|
class_option :rack, type: :boolean, default: false
|
2014-03-11 12:07:55 +01:00
|
|
|
|
|
|
|
# Write a Rack config.ru file for project
|
|
|
|
# @return [void]
|
|
|
|
def generate_rack!
|
|
|
|
return unless options[:rack]
|
|
|
|
template 'shared/config.ru', File.join(location, 'config.ru')
|
|
|
|
end
|
|
|
|
|
2014-03-13 11:57:19 +01:00
|
|
|
# Do not run bundle install
|
|
|
|
class_option :'skip-bundle', type: :boolean, default: false
|
2014-03-11 12:07:55 +01:00
|
|
|
|
|
|
|
# Write a Bundler Gemfile file for project
|
|
|
|
# @return [void]
|
|
|
|
def generate_bundler!
|
2014-03-13 11:05:30 +01:00
|
|
|
template self.class.gemfile_template, File.join(location, 'Gemfile')
|
2014-03-11 12:07:55 +01:00
|
|
|
return if options[:'skip-bundle']
|
|
|
|
inside(location) do
|
2014-03-13 10:46:49 +01:00
|
|
|
run('bundle install')
|
2014-03-11 12:07:55 +01:00
|
|
|
end unless ENV['TEST']
|
|
|
|
end
|
|
|
|
|
|
|
|
# Output a .gitignore file
|
2014-03-13 11:57:19 +01:00
|
|
|
class_option :'skip-git', type: :boolean, default: false
|
2014-03-11 12:07:55 +01:00
|
|
|
|
|
|
|
# Write a .gitignore file for project
|
|
|
|
# @return [void]
|
|
|
|
def generate_gitignore!
|
|
|
|
return if options[:'skip-git']
|
|
|
|
copy_file 'shared/gitignore', File.join(location, '.gitignore')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-13 11:57:19 +01:00
|
|
|
# Register all official templates
|
|
|
|
Dir.glob(File.expand_path('../middleman-templates/*.rb', __FILE__), &method(:require))
|
|
|
|
|
2014-03-25 09:21:53 +01:00
|
|
|
# Sometimes HOME doesn't exist, in which case there's no point to local templates
|
|
|
|
if ENV['HOME']
|
|
|
|
# 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)
|
2014-04-29 19:50:21 +02:00
|
|
|
if File.exist?(template_file)
|
2014-03-25 09:21:53 +01:00
|
|
|
require template_file
|
|
|
|
else
|
|
|
|
Middleman::Templates.register(File.basename(dir).to_sym, Middleman::Templates::Local)
|
|
|
|
end
|
2014-03-13 11:57:19 +01:00
|
|
|
end
|
2014-04-29 19:50:21 +02:00
|
|
|
end
|