File: directory_template_container.rb

package info (click to toggle)
ruby-rgen 0.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,428 kB
  • sloc: ruby: 11,344; xml: 1,368; yacc: 72; makefile: 10
file content (83 lines) | stat: -rw-r--r-- 2,161 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# RGen Framework
# (c) Martin Thiede, 2006

require 'rgen/template_language/template_container'
require 'rgen/template_language/template_helper'

module RGen
  
module TemplateLanguage
    
class DirectoryTemplateContainer
  include TemplateHelper
  
  def initialize(metamodel=nil, output_path=nil, parent=nil)
    @containers = {}
    @directoryContainers = {}
    @parent = parent
    @metamodel = metamodel
    @output_path = output_path
  end
  
  def load(dir)
    Dir.foreach(dir) { |f|
      qf = dir+"/"+f
      if !File.directory?(qf) && f =~ /^(.*)\.tpl$/
       (@containers[$1] = TemplateContainer.new(@metamodel, @output_path, self,qf)).load
      elsif File.directory?(qf) && f != "." && f != ".."
       (@directoryContainers[f] = DirectoryTemplateContainer.new(@metamodel, @output_path, self)).load(qf)
      end
    }
  end
  
  def expand(template, *all_args)
    if template =~ /^\//
      if @parent
        # pass to parent
        @parent.expand(template, *all_args)
      else
        # this is root
        _expand(template, *all_args)
      end
    elsif template =~ /^\.\.\/(.*)/
      if @parent
        # pass to parent
        @parent.expand($1, *all_args)
      else
        raise "No parent directory for root"
      end
    else
      _expand(template, *all_args)
    end
  end
  
  # Set indentation string.
  # Every generated line will be prependend with n times this string at an indentation level of n.
  # Defaults to "   " (3 spaces)
  def indentString=(str)
    @indentString = str
  end
  
  def indentString
    @indentString || (@parent && @parent.indentString) || "   "
  end
  
  private
  
  def _expand(template, *all_args)
    if template =~ /^\/?(\w+)::(\w.*)/ 
      raise "Template not found: #{$1}" unless @containers[$1]
      @containers[$1].expand($2, *all_args)
    elsif template =~ /^\/?(\w+)\/(\w.*)/ 
      raise "Template not found: #{$1}" unless @directoryContainers[$1]
      @directoryContainers[$1].expand($2, *all_args)
    else
      raise "Invalid template name: #{template}"
    end
  end
  
end

end
    
end