lib/depengine/processor/template.rb in depengine-3.0.12 vs lib/depengine/processor/template.rb in depengine-3.0.13

- old
+ new

@@ -1,82 +1,97 @@ module Processor class Template include Processor - attr_accessor :basepath + attr_accessor :basepath, :outdir, :source, :content attr_writer :cdb # Render a template and write the output to a file or directory # structure under 'basepath' def parse_template(template_source, content, outdir, options={}) Helper.validates_presence_of basepath, "Basepath not set for template engine" # expand basepath - self.basepath = File.expand_path basepath + @basepath = File.expand_path basepath + @outdir = outdir + @source = File.join(@basepath, template_source) + @content = content + @excludes = options[:excludes] - if File.file?( File.join( basepath, template_source ) ) - source_root_path = File.join( basepath, File.dirname(template_source) ) - single_file = File.join( basepath, template_source ) + if File.file?(File.join(basepath, template_source)) + from_file else - # source is a directory - source_root_path = File.join( basepath, template_source) - single_file = nil + all_from_directory end - target_root_path = File.join( basepath, outdir ) - target_path = target_root_path + return self + end - Find.find( single_file || source_root_path ) do |path| + def from_file + if excluded? @source + $log.writer.debug "Skip template #{@source}" + return + end + target_dir = target_path_of(@source) + create_sub_dirs!(target_dir) + target_file = File.join(target_dir, File.basename(@source).gsub(".tpl","")) + File.open(target_file, "w") do |f| + f.write(parse(@source)) + end + end + + def all_from_directory + find_templates(@source).each do |t| + @source = t + from_file + end + end + + def find_templates(path_to_find_in) + templates = [] + Find.find(path_to_find_in) do |path| # next if we found ourself - next if path == source_root_path + next if path == path_to_find_in + next unless File.file?(path) and path.end_with?(".tpl") + templates << path + end + templates + end - sub_dir = File.join( File.dirname( path ).split(/\//) - \ - source_root_path.split(/\//) ) - target_path = File.join( target_root_path, sub_dir ) - target = File.join( target_path, File.basename( path ) ) + def target_path_of(path) + path = File.dirname(path) if File.file?(path) + sub_dir = File.join(path.split(/\//) - @basepath.split(/\//)) + File.join(@outdir, sub_dir) + end - if not options[:excludes].nil? - options[:excludes].each do |exclude| - if target.include? exclude - $log.writer.debug "Skip template #{target}" - Find.prune - next - end + def excluded?(target) + if @excludes + @excludes.each do |exclude| + if target.include? exclude + return true end end + end + return false + end - if File.directory? path - if not File.directory? target - FileUtils.mkdir_p( target ) - $log.writer.debug "Create directory #{target}" - end - else - target_file = File.join( target_path, \ - File.basename(target, ".tpl")) - begin - if not File.binary? path and not File.zero? path - writer = File.new(target_file, 'w') - writer.write(parse(path, content)) - writer.close if not writer.nil? - writer = nil - else - FileUtils.cp(path, File.expand_path(target_file)) - end - rescue Exception => e - $log.writer.error "Can not write output from template to #{target_file}" - $log.writer.error e.message - exit 1 - end - end + def create_sub_dirs!(target) + if not File.directory?(target) + FileUtils.mkdir_p(target) + $log.writer.debug "Create directory #{target}" end end + # This function returns the output of a parsed template, 'template' can be # a filename or a String. def self.parse(template, content) $log.writer.debug "Parse Template #{template}" - Template.new.parse(template, content) + t = Template.new + t.source = template + t.content = content + t.parse(template) end - def parse(template, content) - context = load_tags(content, @cdb) + def parse(template) + context = load_tags(@content, @cdb) begin parser = Radius::Parser.new(context, :tag_prefix => 't') template_content = "" result = "" if File.file? template