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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.execute_source(file) ⇒ Object



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

def self.execute_source(file)
  load file
end

Instance Method Details

#close(options = {}) ⇒ Object

Expands and inserts all render statements that have been encountered



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/origen_testers/generator.rb', line 33

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



133
134
135
# File 'lib/origen_testers/generator.rb', line 133

def collection
  @collection ||= []
end

#collection=(array) ⇒ Object



137
138
139
# File 'lib/origen_testers/generator.rb', line 137

def collection=(array)
  @collection = array
end

#compilerObject



80
81
82
# File 'lib/origen_testers/generator.rb', line 80

def compiler
  Origen.generator.compiler
end

#current_dirObject

Returns the directory of the current source file being generated



61
62
63
64
65
66
67
# File 'lib/origen_testers/generator.rb', line 61

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



127
128
129
# File 'lib/origen_testers/generator.rb', line 127

def dont_diff=(val)
  @dont_diff = val
end

#file_extensionObject



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/origen_testers/generator.rb', line 141

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



56
57
58
# File 'lib/origen_testers/generator.rb', line 56

def file_pipeline
  @@file_pipeline ||= []
end

#filename(options = {}) ⇒ Object



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
125
# File 'lib/origen_testers/generator.rb', line 93

def filename(options = {})
  options = {
    include_extension: true
  }.merge(options)
  name = (@filename || Origen.file_handler.current_file.basename('.rb')).to_s
  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 fixfix 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



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

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.



77
78
# File 'lib/origen_testers/generator.rb', line 77

def finalize(options = {})
end

#identity_mapObject

:nodoc:



265
266
267
# File 'lib/origen_testers/generator.rb', line 265

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

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



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

def import(file, options = {})
  file = Pathname.new(file).absolute? ? file : "#{current_dir}/#{file}"
  file = Origen.file_handler.clean_path_to_sub_program(file)
  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



23
24
25
# File 'lib/origen_testers/generator.rb', line 23

def inhibit_output
  @inhibit_output = true
end

#nameObject Also known as: id



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

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.



72
73
# File 'lib/origen_testers/generator.rb', line 72

def on_close(options = {})
end

#output_fileObject



204
205
206
207
208
209
210
211
212
# File 'lib/origen_testers/generator.rb', line 204

def output_file
  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
  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)


28
29
30
# File 'lib/origen_testers/generator.rb', line 28

def output_inhibited?
  @inhibit_output
end

#platformObject



269
270
271
# File 'lib/origen_testers/generator.rb', line 269

def platform
  Origen.interface.platform
end

#reference_fileObject



214
215
216
# File 'lib/origen_testers/generator.rb', line 214

def reference_file
  Pathname.new("#{Origen.file_handler.reference_directory}/#{filename}")
end

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



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

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



261
262
263
# File 'lib/origen_testers/generator.rb', line 261

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

#statsObject



253
254
255
# File 'lib/origen_testers/generator.rb', line 253

def stats
  Origen.app.stats
end

#to_be_written?Boolean

Returns:

  • (Boolean)


257
258
259
# File 'lib/origen_testers/generator.rb', line 257

def to_be_written?
  true
end

#write_from_template(options = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/origen_testers/generator.rb', line 165

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



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/origen_testers/generator.rb', line 153

def write_to_file(options = {})
  c = caller[0]
  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