Module: OrigenTesters::Generator

Overview

This module should be included in all test program component generators and provides the required integration with the Flow.create and Resources.create methods

Defined Under Namespace

Modules: ClassMethods Classes: IdentityMap, Placeholder, TestNumberer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#output_directoryObject

Returns the value of attribute output_directory.



17
18
19
# File 'lib/origen_testers/generator.rb', line 17

def output_directory
  @output_directory
end

Class Method Details

.execute_source(file) ⇒ Object



20
21
22
# File 'lib/origen_testers/generator.rb', line 20

def self.execute_source(file)
  load file
end

.original_reference_fileObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
# File 'lib/origen_testers/generator.rb', line 30

def self.original_reference_file
  !!@original_reference_file
end

.original_reference_file=(val) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
# File 'lib/origen_testers/generator.rb', line 25

def self.original_reference_file=(val)
  @original_reference_file = val
end

Instance Method Details

#close(options = {}) ⇒ Object

Expands and inserts all render statements that have been encountered



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/origen_testers/generator.rb', line 45

def close(options = {})
  Origen.profile "closing #{filename}" do
    base_collection = collection
    base_collection.each_with_index do |item, i|
      if item.is_a? Placeholder
        if item.type == :render
          txt = ''
          Origen.file_handler.preserve_current_file do
            Origen.file_handler.default_extension = file_extension
            placeholder = compiler.render(item.file, item.options)
            txt = compiler.insert(placeholder).chomp
          end
          base_collection[i] = txt
        else
          fail 'Unknown placeholder encountered!'
        end
      end
    end
    @collection = base_collection.flatten.compact
    on_close(options)
  end
end

#collectionObject

All generators must implement a collection method that returns an array containing the generated items



148
149
150
# File 'lib/origen_testers/generator.rb', line 148

def collection
  @collection ||= []
end

#collection=(array) ⇒ Object



152
153
154
# File 'lib/origen_testers/generator.rb', line 152

def collection=(array)
  @collection = array
end

#compilerObject



92
93
94
# File 'lib/origen_testers/generator.rb', line 92

def compiler
  Origen.generator.compiler
end

#current_dirObject

Returns the directory of the current source file being generated



73
74
75
76
77
78
79
# File 'lib/origen_testers/generator.rb', line 73

def current_dir
  if file_pipeline.empty?
    Origen.file_handler.base_directory
  else
    Pathname.new(file_pipeline.last).dirname
  end
end

#dont_diff=(val) ⇒ Object



142
143
144
# File 'lib/origen_testers/generator.rb', line 142

def dont_diff=(val)
  @dont_diff = val
end

#file_extensionObject



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/origen_testers/generator.rb', line 156

def file_extension
  if defined? self.class::OUTPUT_EXTENSION
    self.class::OUTPUT_EXTENSION
  elsif defined? self.class::TEMPLATE
    p = Pathname.new(self.class::TEMPLATE)
    ext = p.basename('.erb').extname
    ext.empty? ? 'txt' : ext
  else
    'txt'
  end
end

#file_pipelineObject



68
69
70
# File 'lib/origen_testers/generator.rb', line 68

def file_pipeline
  @@file_pipeline ||= []
end

#filename(options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/origen_testers/generator.rb', line 105

def filename(options = {})
  options = {
    include_extension: true
  }.merge(options)
  # Allow generators to override this and fully define the filename if they want
  return fully_formatted_filename if try(:fully_formatted_filename)
  name = (@filename || Origen.file_handler.current_file.basename('.rb')).to_s
  name[0] = '' if name[0] == '_'
  if Origen.config.program_prefix
    unless name =~ /^#{Origen.config.program_prefix}/i
      name = "#{Origen.config.program_prefix}_#{name}"
    end
  end
  f = Pathname.new(name).basename
  ext = f.extname.empty? ? file_extension : f.extname
  body = f.basename(".#{ext}").to_s
  body.gsub!('_resources', '')
  if defined? self.class::OUTPUT_PREFIX
    # Unless the prefix is already in the name
    unless body =~ /#{self.class::OUTPUT_PREFIX}$/i
      body = "#{self.class::OUTPUT_PREFIX}_#{body}"
    end
  end
  if defined? self.class::OUTPUT_POSTFIX
    # Unless the postfix is already in the name
    unless body =~ /#{self.class::OUTPUT_POSTFIX}$/i
      body = "#{body}_#{self.class::OUTPUT_POSTFIX}"
    end
  end
  ext = ".#{ext}" unless ext =~ /^\./
  if options[:include_extension]
    "#{body}#{ext}"
  else
    "#{body}"
  end
end

#filename=(name) ⇒ Object



96
97
98
# File 'lib/origen_testers/generator.rb', line 96

def filename=(name)
  @filename = name
end

#finalize(options = {}) ⇒ Object

Redefine this in the parent which includes this module if you want anything to occur after all tests have been generated but before file writing starts.



89
90
# File 'lib/origen_testers/generator.rb', line 89

def finalize(options = {})
end

#identity_mapObject

:nodoc:



323
324
325
# File 'lib/origen_testers/generator.rb', line 323

def identity_map # :nodoc:
  Origen.interface.identity_map
end

#import(file, options = {}) ⇒ Object

Import sub-program



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/origen_testers/generator.rb', line 257

def import(file, options = {})
  options = {
    name: nil
  }.merge(options)
  file = Pathname.new(file).absolute? ? file : "#{current_dir}/#{file}"
  file = Origen.file_handler.add_rb_to(file)  # add .rb to filename if missing
  orig_file = "#{file}"     # capture original filename possibly without pre-pended underscore
  file_w_underscore = Origen.file_handler.add_underscore_to(file) # determine filename definitely with pre-pended underscore

  # first check filename definitely with underscore
  file = Origen.file_handler.clean_path_to(file_w_underscore, allow_missing: true)  # allow for file missing
  if file.nil?   # check if file missing
    # if so, check original filename that possibly is without underscore
    file = Origen.file_handler.clean_path_to(orig_file, allow_missing: true)  # allow for file missing
    if file.nil?
      # give error if could not find either file
      fail "Could not find file to import: #{orig_file}" if file.nil?  # give error if neither option above is found
    end
  end

  OrigenTesters::Flow.name_stack << options[:name]

  base_collection = collection
  @collection = []
  Origen.generator.option_pipeline << options
  file_pipeline << file
  ::OrigenTesters::Generator.execute_source(file)
  file_pipeline.pop
  base_collection << @collection
  @collection = base_collection.flatten
end

#inhibit_outputObject

When called on a generator no output files will be created from it



35
36
37
# File 'lib/origen_testers/generator.rb', line 35

def inhibit_output
  @inhibit_output = true
end

#nameObject Also known as: id



100
101
102
# File 'lib/origen_testers/generator.rb', line 100

def name
  @filename.to_sym
end

#on_close(options = {}) ⇒ Object

Redefine this in the parent which includes this module if you want anything to occur after closing the generator (expanding all render/import statements) but before writing to a file.



84
85
# File 'lib/origen_testers/generator.rb', line 84

def on_close(options = {})
end

#output_fileObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/origen_testers/generator.rb', line 218

def output_file
  # If an explicit output directory has been set, use it
  if output_directory
    p = Pathname.new("#{output_directory}/#{filename}")
  # Otherwise resolve one
  else
    if respond_to? :subdirectory
      p = Pathname.new("#{Origen.file_handler.output_directory}/#{subdirectory}/#{filename}")
    else
      p = Pathname.new("#{Origen.file_handler.output_directory}/#{filename}")
    end
  end
  FileUtils.mkdir_p p.dirname.to_s unless p.dirname.exist?
  p
end

#output_inhibited?Boolean

Returns true if the output files from this generator will be inhibited

Returns:

  • (Boolean)


40
41
42
# File 'lib/origen_testers/generator.rb', line 40

def output_inhibited?
  @inhibit_output
end

#platformObject



327
328
329
# File 'lib/origen_testers/generator.rb', line 327

def platform
  Origen.interface.platform
end

#reference_fileObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/origen_testers/generator.rb', line 234

def reference_file
  if OrigenTesters::Generator.original_reference_file
    Pathname.new("#{Origen.file_handler.reference_directory}/#{filename}")
  else
    # If an explicit output directory has been set, use it
    if output_directory
      sub = Pathname.new(output_directory).relative_path_from(Origen.file_handler.output_directory)
      dir = File.join(Origen.file_handler.reference_directory, sub)
      FileUtils.mkdir_p(dir)
      Pathname.new(File.join(dir, filename))
    else
      if respond_to? :subdirectory
        dir = File.join(Origen.file_handler.reference_directory, subdirectory)
        FileUtils.mkdir_p(dir)
        Pathname.new(File.join(dir, filename))
      else
        Pathname.new("#{Origen.file_handler.reference_directory}/#{filename}")
      end
    end
  end
end

#render(file, options = {}) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/origen_testers/generator.rb', line 289

def render(file, options = {})
  # Since the flow is now handled via ATP, render the string immediately
  # for insertion into the AST
  if try(:is_the_flow?)
    val = nil
    Origen.file_handler.preserve_current_file do
      Origen.file_handler.default_extension = file_extension
      full_path = Origen.file_handler.clean_path_to(file, allow_missing: true)
      full_path ||= Origen.file_handler.clean_path_to_sub_template(file)
      placeholder = compiler.render(full_path, options)
      val = compiler.insert(placeholder).chomp
    end
    val
  else
    if options.delete(:_inline)
      super Origen.file_handler.clean_path_to_sub_template(file), options
    else
      collection << Placeholder.new(:render, file, options)
    end
  end
end

#set_flow_description(desc) ⇒ Object



319
320
321
# File 'lib/origen_testers/generator.rb', line 319

def set_flow_description(desc)
  Origen.interface.descriptions.add_for_flow(output_file, desc)
end

#statsObject



311
312
313
# File 'lib/origen_testers/generator.rb', line 311

def stats
  Origen.app.stats
end

#to_be_written?Boolean

Returns:

  • (Boolean)


315
316
317
# File 'lib/origen_testers/generator.rb', line 315

def to_be_written?
  true
end

#write_from_template(options = {}) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/origen_testers/generator.rb', line 179

def write_from_template(options = {})
  return unless Origen.interface.write?
  options = {
    quiet:     false,
    skip_diff: false
  }.merge(options)
  unless output_inhibited?
    # If this is not the first time we have written to the current output file
    # then append to it, otherwise clear it and start from scratch.
    # The use of a class variable to store the opened files means that it will be
    # shared by all generators in this run.
    @@opened_files ||= []
    if @@opened_files.include?(output_file) && !Origen.tester.is_a?(OrigenTesters::Doc)
      @append = true
      Origen.file_handler.preserve_state do
        File.open(output_file, 'a') do |out|
          content = compiler.insert(ERB.new(File.read(self.class::TEMPLATE), 0, Origen.config.erb_trim_mode).result(binding))
          out.puts content unless content.empty?
        end
      end
      Origen.log.info "Appending... #{output_file.basename}" unless options[:quiet]
    else
      @append = false
      Origen.file_handler.preserve_state do
        File.open(output_file, 'w') do |out|
          out.puts compiler.insert(ERB.new(File.read(self.class::TEMPLATE), 0, Origen.config.erb_trim_mode).result(binding))
        end
      end
      @@opened_files << output_file
      Origen.log.info "Writing... #{output_file.basename}" unless options[:quiet]
    end
    if !@dont_diff && !options[:skip_diff] && !options[:quiet]
      check_for_changes(output_file, reference_file,
                        compile_job:  true,
                        comment_char: Origen.app.tester.program_comment_char)
    end
  end
end

#write_to_file(options = {}) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/origen_testers/generator.rb', line 168

def write_to_file(options = {})
  unless output_inhibited?
    if defined? self.class::TEMPLATE || Origen.tester.is_a?(OrigenTesters::Doc)
      write_from_template(options)
    else
      fail "Don't know how to write without a template!"
    end
    stats.completed_files += 1
  end
end