lib/capitate/plugins/templates.rb in capitate-0.1.9 vs lib/capitate/plugins/templates.rb in capitate-0.2.1

- old
+ new

@@ -1,73 +1,47 @@ module Capitate::Plugins::Templates - # Get the absolute base templates path. - def gem_templates_root - File.expand_path(File.dirname(__FILE__) + "/../../templates") - end - - # Get full template path from relative path. + # Load template. If extension is erb will be evaluated with binding. # - # Something like <tt>monit/monit.cnf => /usr/lib/..../capitate/lib/templates/monit/monit.cnf</tt>. + # You can add to the list of places we search for templates by setting: # - # ==== Options - # +template_path+:: Relative path + # set :templates_dirs, [ "path/to/templates1", "/path/to/templates2" ] # - def gem_template_path(template_path) - File.join(gem_templates_root, template_path) - end - - # Load template. If extension is erb will be evaluated with binding. + # It looks for the template from: + # * the <tt>:templates_dirs</tt> setting (if set) + # * the current directory + # * the <tt>:project_root</tt> setting (if set) + # * the gem templates path # # ==== Options # +path+:: If starts with '/', absolute path, otherwise relative path to templates dir # +override_binding+:: Binding to override, otherwise uses current (task) binding # # ==== Examples # template.load("memcached/memcached.monitrc.erb") # put template.load("memcached/memcached.monitrc.erb"), "/tmp/memcached.monitrc" # def load(path, override_binding = nil) - template_paths = [ path, gem_template_path(path) ] - template_paths = template_paths.select { |file| File.exist?(file) } + template_dirs_found = template_dirs.select { |dir| File.exist?("#{dir}/#{path}") } - if template_paths.empty? - raise <<-EOS - - - Template not found: #{path} - - EOS + # Not found anywhere, throw error + if template_dirs_found.empty? + raise "\n\nTemplate not found: #{path}\n\n" end # Use first - template_path = template_paths.first + template_path = template_dirs_found.first + "/#{path}" template_data = IO.read(template_path) if File.extname(template_path) == ".erb" template = ERB.new(template_data) template_data = template.result(override_binding || binding) end template_data end - - # Load template from project. - # If extension is erb will be evaluated with binding. - # - # ==== Options - # +path+:: If starts with '/', absolute path, otherwise relative path to templates dir - # +override_binding+:: Binding to override, otherwise uses current (task) binding - # - # ==== Examples - # template.project("config/templates/sphinx.conf.erb") - # put template.project("config/templates/sphinx.conf.erb"), "#{shared_path}/config/sphinx.conf" - # - def project(path, override_binding = nil) - load(project_root + "/" + path, override_binding || binding) - end # Write template at (relative path) with binding to LOCAL destination path. # # ==== Options # +template_path+:: Path to template relative to templates path @@ -75,11 +49,11 @@ # +overwrite_binding+:: Binding # +overwrite+:: Force overwrite # +verbose+:: Verbose output # # ==== Examples - # template.write("config/templates/sphinx.conf.erb", binding, "config/sphinx.conf") + # template.write("config/templates/sphinx.conf.erb", "config/sphinx.conf") # def write(template_path, dest_path, override_binding = nil, overwrite = false, verbose = true) # This is gnarly! relative_dest_path = Pathname.new(File.expand_path(dest_path)).relative_path_from(Pathname.new(File.expand_path("."))) @@ -91,8 +65,39 @@ puts "%10s %-40s" % [ "create", relative_dest_path ] if verbose template_data = load(template_path, override_binding) File.open(dest_path, "w") { |file| file.puts(load(template_path, override_binding)) } end + +protected + + # Load all possible places for templates + def template_dirs + @template_dir ||= begin + template_dirs = [] + template_dirs += fetch(:templates_dirs) if exists?(:templates_dirs) + template_dirs << [ "." ] + template_dirs << project_root if exists?(:project_root) + template_dirs << gem_templates_root + template_dirs + end + end + + # Get the absolute base templates path. + def gem_templates_root + File.expand_path(File.dirname(__FILE__) + "/../../templates") + end + + # Get full template path from relative path. + # + # Something like <tt>monit/monit.cnf => /usr/lib/..../capitate/lib/templates/monit/monit.cnf</tt>. + # + # ==== Options + # +template_path+:: Relative path + # + def gem_template_path(template_path) + File.join(gem_templates_root, template_path) + end + end Capistrano.plugin :template, Capitate::Plugins::Templates \ No newline at end of file