Class: Processor::Erb_template

Inherits:
Object
  • Object
show all
Includes:
Processor
Defined in:
lib/depengine/processor/erb_template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Processor

check_filelist, chmod, copy, #load_tags, mkdir, mklink, remove, tgz_file, untgz_file, unzip_file

Instance Attribute Details

#basepathObject

Returns the value of attribute basepath.



4
5
6
# File 'lib/depengine/processor/erb_template.rb', line 4

def basepath
  @basepath
end

#cdb=(value) ⇒ Object (writeonly)

Sets the attribute cdb

Parameters:

  • value

    the value to set the attribute cdb to.



5
6
7
# File 'lib/depengine/processor/erb_template.rb', line 5

def cdb=(value)
  @cdb = value
end

Class Method Details

.parse(template, content) ⇒ Object

This function returns the output of a parsed template, ‘template’ can be a filename or a String.



73
74
75
76
# File 'lib/depengine/processor/erb_template.rb', line 73

def self.parse(template, content)
  $log.writer.debug "Parse Template #{template}"
  Template.new.parse(template, content)
end

Instance Method Details

#parse(template, content) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/depengine/processor/erb_template.rb', line 78

def parse(template, content)
  context = load_tags(content, @cdb)
  begin
    parser           = Radius::Parser.new(context, :tag_prefix => 't')  
    template_content = ""
    result           = ""
    if File.file? template
      read_mode        = ""
      if RUBY_VERSION < '1.9'
        read_mode = 'r'
      else
        read_mode = 'rb'
      end
      File.open(template, read_mode) do |f|
        c = ""
        f.ungetc c unless (c = f.getc)=="\uFEFF" # remove BOM, if present
        while line = f.gets
          template_content << line
        end
      end
    else
      template_content = template
    end
 
    result          = parser.parse(template_content) 
    nested_result_1 = ""
    nested_result_2 = result
    nested_loop     = 0
    while( nested_result_1 != nested_result_2 )
      if nested_loop >= 100
        $log.writer.error "Template stack overflow, too many nested tags"
        exit 1
      end
      nested_result_1 = parser.parse(nested_result_2)
      nested_result_2 = parser.parse(nested_result_1)

      nested_loop = nested_loop + 1
   end
   result = nested_result_2
  rescue Exception => e
    $log.writer.error "Error in template processing: #{template}"
    $log.writer.error e.message
    exit 1
  end
  
  result
end

#parse_template(template_source, content, outdir, options = {}) ⇒ Object

Render a template and write the output to a file or directory structure under ‘basepath’



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/depengine/processor/erb_template.rb', line 9

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

  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 )
  else
    # source is a directory
    source_root_path = File.join( basepath, template_source)
    single_file      = nil
  end
  target_root_path = File.join( basepath, outdir )
  target_path      = target_root_path

  Find.find( single_file || source_root_path ) do |path|
    # next if we found ourself
    next if path == source_root_path
      
    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 ) )

    if not options[:excludes].nil?
      options[:excludes].each do |exclude|
        if target.include? exclude
          $log.writer.debug "Skip template #{target}"
          Find.prune
          next
        end
      end
    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, ".erb"))
      begin
        if not File.binary? path and not File.zero? path
          writer = File.new(target_file, 'w')
          # writer.write(parse(path, content))
          writer.write(ERB.new(File.read(path)).result())
          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
  end
end