Class: Bake::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/bake/model/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLoader

Returns a new instance of Loader.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bake/model/loader.rb', line 22

def initialize
  @env = RGen::Environment.new

  fcm = RGen::Util::FileCacheMap.new(".bake", ".cache")
  fcm.version_info = Version.number

  if !Bake.options.dry
    @DumpFileCache = RGen::Fragment::DumpFileCache.new(fcm)
  else
    @DumpFileCache = nil
  end

  @model = RGen::Fragment::FragmentedModel.new(:env => @env)

  @globalFilterStrMap = {
    Bake::Metamodel::StartupSteps => "STARTUP",
    Bake::Metamodel::PreSteps => "PRE",
    Bake::Metamodel::PostSteps => "POST",
    Bake::Metamodel::ExitSteps => "EXIT",
    Bake::Metamodel::CleanSteps => "CLEAN"
  }
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



20
21
22
# File 'lib/bake/model/loader.rb', line 20

def model
  @model
end

Instance Method Details

#applyFilterOnArray(a) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/bake/model/loader.rb', line 102

def applyFilterOnArray(a)
  toRemove = []
  a.each do |elem|
    if filterElement?(elem)
      toRemove << elem
    else
      applyFilterOnElement(elem)
    end
  end
  toRemove.each { |r| r.parent = nil }
end

#applyFilterOnElement(elem) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/bake/model/loader.rb', line 114

def applyFilterOnElement(elem)
  return if elem.nil?
  elem.class.ecore.eAllReferences.each do |f|
   next unless f.containment
   begin
     childData = elem.getGeneric(f.name)
   rescue Exception => ex
     next
   end
   next if childData.nil?
   if (Array === childData)
     applyFilterOnArray(childData)
   elsif Metamodel::ModelElement === childData
     if filterElement?(childData)
       childData.parent = nil
     else
       applyFilterOnElement(childData)
     end
   end
  end
end

#filterElement?(elem) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bake/model/loader.rb', line 79

def filterElement?(elem)
  return false if Bake::Metamodel::Project === elem

  # 1st prio: explicit single filter
  if elem.filter != ""
    return true if  Bake.options.exclude_filter.include?elem.filter
    return false if Bake.options.include_filter.include?elem.filter
  end

  # 2nd prio: explicit global filter
  if defined?(elem.parent)
    globalFilterStr = @globalFilterStrMap[elem.parent.class]
    if (globalFilterStr)
        return true if  Bake.options.exclude_filter.include?globalFilterStr
        return false if Bake.options.include_filter.include?globalFilterStr
    end
  end

  # 3nd prio: default
  return true if elem.default == "off"
  false
end

#load(filename) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/bake/model/loader.rb', line 136

def load(filename)
  sumErrors = 0

  if not File.exists?filename
    Bake.formatter.printError("Error: #{filename} does not exist")
    ExitHelper.exit(1)
  end

  frag = nil
  if not Bake.options.nocache
    frag = load_internal(filename) # regular load
    frag = nil if frag.root_elements.length > 0 and filename != frag.root_elements[0].file_name
  end

  if frag.nil?
    def @DumpFileCache.load(fragment)
      :invalid
    end
    frag = load_internal(filename, !Bake.options.nocache)
  end

  frag.data[:problems].each do |p|
    Bake.formatter.printError(p.message, p.file, p.line)
  end

  if frag.data[:problems].length > 0
    ExitHelper.exit(1)
  end

  applyFilterOnArray(frag.root_elements)

  return frag

end

#load_internal(filename, silent = false) ⇒ Object



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
# File 'lib/bake/model/loader.rb', line 45

def load_internal(filename, silent = false)
  silent = false if Bake.options.debug
  loader = RText::DefaultLoader.new(
    Bake::Language,
    @model,
    :file_provider => proc { [filename] },
    :cache => @DumpFileCache)
  loader.load(:before_load => proc {|fragment, kind|
    case kind
    when :load_update_cache
      if Bake.options.verbose >= 3
        puts "Loading and caching #{fragment.location}" unless silent
      else
        puts "Loading #{fragment.location}" unless silent
      end
    when :load_cached
      if Bake.options.verbose >= 3
        puts "Loading cached #{fragment.location}" unless silent
      else
        puts "Loading #{fragment.location}" unless silent
      end
    when :load
      puts "Loading #{fragment.location}" unless silent
    else
      Bake.formatter.printError("Error: Could not load #{fragment.location}")
      ExitHelper.exit(1)
    end
  })

  frag = @model.fragments[0]
  @model.remove_fragment(frag)
  frag
end