Skip to content

Instantly share code, notes, and snippets.

@onemanstartup
Created February 11, 2013 11:00
Show Gist options
  • Save onemanstartup/4753851 to your computer and use it in GitHub Desktop.
Save onemanstartup/4753851 to your computer and use it in GitHub Desktop.
templating in thor
# Default Middleman template
class Fusion < Thor::Group
include Thor::Actions
argument :location, :type => :string
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 => "i",
:desc => 'The path to the image files'
# Template files are relative to this file
# @return [String]
def self.source_root
File.dirname(__FILE__)
end
# Output a config.ru file for Rack if --rack is passed
class_option :rack, :type => :boolean, :default => false
# 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
# Output a .gitignore file
class_option :'git', :type => :boolean, :default => false
# Write a .gitignore file for project
# @return [void]
def generate_gitignore!
return unless options[:'git']
copy_file "shared/gitignore", File.join(location, ".gitignore")
end
def analytics
if true # yes?("analytics?")
@analytics = true
if false #yes?("Google Analytics?")
@ga = true
else
@ga = false
end
if true #yes?("Yandex metrika?")
@ya = true
@ya_id = 18371548 #ask("Yandex metrika ID?")
else
@ya = false
end
template "template/source/partials/analytics.tt", File.join(location, "source/partials/analytics.html.erb")
else
@analytics = false
end
end
def livereload
@livereload = false
end
# Actually output the files
# @return [void]
def build_scaffold!
if true #yes?("Middleman?")
@middleman = true
empty_directory File.join(location, "source", "helpers")
copy_file "template/source/helpers/site_helpers.rb", File.join(location, "helpers/site_helpers.rb")
if false #yes?("Middleman Admin?")
@admin = true
empty_directory File.join(location, "source", "lib")
directory "template/source/lib/middlemanager", "#{location}/lib/middlemanager"
else
@admin = false
end
else
@middleman = false
end
template "template/source/layouts/layout.tt", File.join(location, "source/layouts/layout.erb")
empty_directory File.join(location, "source", options[:css_dir])
empty_directory File.join(location, "source", options[:js_dir])
empty_directory File.join(location, "source", options[:images_dir])
end
def localizable
if true #yes?("RU/ENG localizable?")
@localizable = true
directory "template/locales", "#{location}/locales"
if @middleman
empty_directory File.join(location, "source", "localizable")
copy_file "template/source/pages/index.html.erb", File.join(location, "source/localizable/index.html.erb")
end
copy_file "template/source/partials/lang_switch.html.erb", File.join(location, "source/partials/lang_switch.html.erb")
copy_file "template/source/partials/navigation.html.erb", File.join(location, "source/partials/navigation.html.erb")
copy_file "template/source/i/lang_icon.png", File.join(location, "source/#{options[:images_dir]}/lang_icon.png")
else
@localizable = false
copy_file "template/source/pages/index.html.erb", File.join(location, "source/index.html.erb")
end
end
def blog
@blog = true
empty_directory File.join(location, "source", "blog")
if @localizable
copy_file "template/source/pages/blog.html.erb", File.join(location, "source/localizable/blog.html.erb")
else
copy_file "template/source/pages/blog.html.erb", File.join(location, "source/blog.html.erb")
end
copy_file "template/source/feed.xml.builder", File.join(location, "source/feed.xml.builder")
end
def stylus
if true #yes?("Use Stylus?")
@stylus = true
# Make a copy of the MITLICENSE file at the source root
directory "template/source/css/stylus", "#{location}/source/#{options[:css_dir]}/stylus"
template "template/source/css/main.tt", File.join(location, "source/#{options[:css_dir]}/main.css.styl")
else
@stylus = false
say "Shame on you", :red
end
directory "template/source/js", "#{location}/source/#{options[:js_dir]}"
end
def deploy
if false #yes?("Deploing?")
@deploy = true
copy_file "template/source/Capfile", File.join(location, "source/Capfile")
empty_directory File.join(location, "source", "config")
@application_name = ask("App name?")
@domain = ask("Domain?")
template "template/source/config/deploy.tt", File.join(location, "source/config/deploy.rb")
else
@deploy = false
end
end
class_option :'bundle', :type => :boolean, :default => false
class_option :'gemfile', :type => :boolean, :default => true
# Write a Bundler Gemfile file for project
# @return [void]
def generate_bundler!
return unless options[:'gemfile']
template "shared/Gemfile.tt", File.join(location, "Gemfile")
return unless options[:'bundle']
inside(location) do
`bundle`
end unless ENV["TEST"]
end
def create_config
if @middleman
template "shared/config.tt", File.join(location, "config.rb")
end
end
def partials
template "template/source/partials/header.html.erb.tt", File.join(location, "source/partials/header.html.erb")
copy_file "template/source/partials/meta.html.erb", File.join(location, "source/partials/meta.html.erb")
end
def disqus
if true #yes?("Disqus comments?")
@disqus = true
@disqus_name = "theantquest" # ask("Domain?")
template "template/source/partials/disqus.html.erb.tt", File.join(location, "source/partials/disqus.html.erb")
else
@disqus = false
end
end
def images
copy_file "template/source/i/logo.gif", File.join(location, "source/#{options[:images_dir]}/logo.gif")
copy_file "template/source/i/bg.png", File.join(location, "source/#{options[:images_dir]}/bg.png")
copy_file "template/source/i/bg_2x.png", File.join(location, "source/#{options[:images_dir]}/bg_2x.png")
end
end
# Register this template
#Middleman::Templates.register(:fusion, Middleman::Templates::Fusion)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment