Module: Origen::Tester::Generator

Extended by:
ActiveSupport::Concern
Included in:
Doc::Generator::Flow, J750::Generator::Flow, J750::Generator::Patgroups, J750::Generator::Patsets, J750::Generator::TestInstances, Ultraflex::Generator::Flow, Ultraflex::Generator::Patgroups, Ultraflex::Generator::Patsets, Ultraflex::Generator::TestInstances, V93K::Generator::Flow, V93K::Generator::PatternMaster
Defined in:
lib/origen/tester/generator.rb,
lib/origen/tester/generator/placeholder.rb,
lib/origen/tester/generator/identity_map.rb,
lib/origen/tester/generator/test_numberer.rb,
lib/origen/tester/generator/flow_control_api.rb

Defined Under Namespace

Modules: ClassMethods, FlowControlAPI Classes: IdentityMap, Placeholder, TestNumberer

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) execute_source(file)

The program source files are executed by eval to allow the tester to filter the source contents before executing. For examples the doc tester replaces all comments with a method call containing each comment so that they can be captured.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/origen/tester/generator.rb', line 21

def self.execute_source(file)
  if Origen.tester.doc? && Origen.interface_loaded? && Origen.interface.respond_to?(:filter_source)
    File.open(file) do |f|
      src = f.read
      src = Origen.interface.filter_source(src)
      # With source file wrapping here to ensure that any calls to Origen.app! within
      # the source code will evaluate to the correct app instance
      Origen.with_source_file(file) do
        eval(src, global_binding)
      end
    end
  else
    load file
  end
end

Instance Method Details

- (Object) close(options = {})

Expands and inserts all render statements that have been encountered



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/origen/tester/generator.rb', line 48

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

- (Object) collection

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



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

def collection
  @collection ||= []
end

- (Object) collection=(array)



141
142
143
# File 'lib/origen/tester/generator.rb', line 141

def collection=(array)
  @collection = array
end

- (Object) compiler



95
96
97
# File 'lib/origen/tester/generator.rb', line 95

def compiler
  Origen.generator.compiler
end

- (Object) current_dir

Returns the directory of the current source file being generated



76
77
78
79
80
81
82
# File 'lib/origen/tester/generator.rb', line 76

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

- (Object) dont_diff=(val)



131
132
133
# File 'lib/origen/tester/generator.rb', line 131

def dont_diff=(val)
  @dont_diff = val
end

- (Object) file_extension



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/origen/tester/generator.rb', line 145

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

- (Object) file_pipeline



71
72
73
# File 'lib/origen/tester/generator.rb', line 71

def file_pipeline
  @@file_pipeline ||= []
end

- (Object) filename(options = {})



103
104
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
# File 'lib/origen/tester/generator.rb', line 103

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_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

- (Object) filename=(name)



99
100
101
# File 'lib/origen/tester/generator.rb', line 99

def filename=(name)
  @filename = name
end

- (Object) finalize(_options = {})

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.



92
93
# File 'lib/origen/tester/generator.rb', line 92

def finalize(_options = {})
end

- (Object) identity_map

:nodoc:



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

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

- (Object) import(file, options = {})



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/origen/tester/generator.rb', line 224

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
  Origen::Tester::Generator.execute_source(file)
  file_pipeline.pop
  base_collection << @collection
  @collection = base_collection.flatten
end

- (Object) inhibit_output

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



38
39
40
# File 'lib/origen/tester/generator.rb', line 38

def inhibit_output
  @inhibit_output = true
end

- (Object) on_close(_options = {})

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.



87
88
# File 'lib/origen/tester/generator.rb', line 87

def on_close(_options = {})
end

- (Object) output_file



216
217
218
# File 'lib/origen/tester/generator.rb', line 216

def output_file
  Pathname.new("#{Origen.file_handler.output_directory}/#{filename}")
end

- (Boolean) output_inhibited?

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

Returns:

  • (Boolean)


43
44
45
# File 'lib/origen/tester/generator.rb', line 43

def output_inhibited?
  @inhibit_output
end

- (Object) reference_file



220
221
222
# File 'lib/origen/tester/generator.rb', line 220

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

- (Object) render(file, options = {})



237
238
239
240
241
242
243
# File 'lib/origen/tester/generator.rb', line 237

def render(file, options = {})
  if options.delete(:_inline)
    super Origen.file_handler.clean_path_to_sub_template(file), options
  else
    collection << Placeholder.new(:render, file, options)
  end
end

- (Object) set_flow_description(desc)



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

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

- (Object) stats



245
246
247
# File 'lib/origen/tester/generator.rb', line 245

def stats
  Origen.app.stats
end

- (Boolean) to_be_written?

Returns:

  • (Boolean)


249
250
251
# File 'lib/origen/tester/generator.rb', line 249

def to_be_written?
  true
end

- (Object) write_from_template(options = {})



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
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/origen/tester/generator.rb', line 168

def write_from_template(options = {})
  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 appen 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?(Origen::Tester::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
        if Origen.tester.is_a?(Origen::Tester::Doc)
          if options[:return_model]
            Origen::Tester::Doc.model.add_flow(filename(include_extension: false), to_yaml)
          else
            Origen.file_handler.open_for_write(output_file) do |f|
              f.puts YAML.dump(to_yaml(include_descriptions: false))
            end
          end
        else
          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
      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

- (Object) write_to_file(options = {})



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

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