Module: Baltix::Loader

Includes:
Log
Included in:
Rake, Source::Gem
Defined in:
lib/baltix/loader.rb

Defined Under Namespace

Modules: Certain, Cmake, GitVersionGen, Mast, Pom, Rookbook, Yaml

Constant Summary

Constants included from Log

Baltix::Log::DEFAULT_IO_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

default_prefix, io_name_parse, ios, level, #level_match, #log, prefix, prefix_for, setup, setup_kind

Class Method Details

.extended(kls) ⇒ Object



104
105
106
# File 'lib/baltix/loader.rb', line 104

def self.extended kls
   extended_list << kls
end

.extended_listObject



100
101
102
# File 'lib/baltix/loader.rb', line 100

def self.extended_list
   @extended_list ||= []
end

Instance Method Details

#app_file(file, &block) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/baltix/loader.rb', line 176

def app_file file, &block
   mods[file] ||= load_file(file)

   mod = mods[file].dup
   objects = mod.diff_ids[self]&.map {|id| ObjectSpace._id2ref(id) }

   debug("Object ids for '#{file}' are: #{mod.diff_ids[self].inspect}")
   debug("Objects for '#{file}' are: #{objects.inspect}")

   if block_given?
      objects = [yield(objects)].flatten.compact
   end
   mod.objects = objects || []

   mod
end

#load_file(file) ⇒ Object



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
170
171
172
173
174
# File 'lib/baltix/loader.rb', line 139

def load_file file
   debug("Loading file: #{file}")
   stdout = $stdout
   stderr = $stderr
   $stdout = $stderr = Tempfile.new('loader')

   pre_loaders.each do |(m, preload_method)|
      if file =~ m
         args = [file][0...preload_method.arity]
         preload_method[*args]
      end
   end

   module_name = "M" + Random.srand.to_s
   mod_code = <<-END
      module #{module_name}
         extend(::Baltix::Loader::Certain)
      end
   END

   mod = module_eval(mod_code)
   mod.load_file(file, type_hash)
   $stdout.rewind
   $stderr.rewind
   log = $stdout.readlines
   errlog = $stderr.readlines

   OpenStruct.new(mod: mod, log: log, errlog: errlog, object_hash: mod.object_hash, diff_ids: mod.object_ids)
rescue Exception => e
   warn(e.message)

   OpenStruct.new(mod: mod, object_hash: {}, log: log, errlog: errlog, diff_ids: [])
ensure
   $stderr = stderr
   $stdout = stdout
end

#modsObject



135
136
137
# File 'lib/baltix/loader.rb', line 135

def mods
   @@mods ||= {}
end

#pre_loadersObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/baltix/loader.rb', line 122

def pre_loaders
   @pre_loaders ||=
      Baltix::Loader.extended_list.map do |kls|
         begin
            kls.const_get('PRELOAD_MATCHER')&.map do |k, v|
               [k, v.is_a?(Symbol) && kls.singleton_method(v) || v]
            end.to_h || {}
         rescue
            {}
         end
      end.reduce {|res, hash| res.merge(hash) }
end

#type_hashObject



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/baltix/loader.rb', line 108

def type_hash
   @type_hash ||=
      Baltix::Loader.extended_list.map do |kls|
         type =
            begin
               kls.const_get('TYPE')
            rescue
               nil
            end

         [kls, type]
      end.select {|(_, type)| type }.to_h
end