Class: Origen::Generator::Compiler

Inherits:
Object
  • Object
show all
Includes:
Helpers, Comparator, Renderer
Defined in:
lib/origen/generator/compiler.rb

Overview

:nodoc: all

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Renderer

#current_pipeline, #insert, #insert_content, #options, #pipeline, #placeholder, #render, #temporary_file

Methods included from Comparator

#check_for_changes, #relative_path_to

Instance Attribute Details

#current_fileObject (readonly)

During a compile this will return the current top-level file being compiled

Examples:

Origen.generator.compiler.current_file   # => Pathname


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

def current_file
  @current_file
end

Instance Method Details

#_get_binding(opts, &block) ⇒ 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.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/origen/generator/compiler.rb', line 173

def _get_binding(opts, &block)
  # Important, don't declare any local variable called options here,
  # the scope of this method will be the default for any templates and
  # we want options to refer to the global options method
  b = opts[:binding] || opts[:scope] || binding
  # If an object has been supplied as the scope, then do some tricks
  # to get a hold of its internal scope
  unless b.is_a?(Binding)
    b.define_singleton_method :_get_binding do |local_opts, &_block|
      options = local_opts
      binding
    end
    # Here the global options, the ones visible right now, are passed to into the method defined above,
    # they will get assigned to the local variable called option and that is what the template will
    # be able to see
    b = b._get_binding(options, &block)
  end
  b
end

#buffer_name_for(file) ⇒ Object

Returns the ERB buffer name for the given file, something like “@my_file_name”



202
203
204
205
206
207
# File 'lib/origen/generator/compiler.rb', line 202

def buffer_name_for(file)
  expected_filename = file.basename.to_s.chomp('.erb')
  expected_filename.gsub!('-', '_') if expected_filename.match(/-/)
  expected_filename.gsub!('.', '_') if expected_filename.match(/./)
  @current_buffer = '@_' + expected_filename
end

#check_for_differences(a, b, file) ⇒ Object



227
228
229
230
231
232
# File 'lib/origen/generator/compiler.rb', line 227

def check_for_differences(a, b, file)
  if check_for_changes(a, b, comment_char: ["'", 'logprint'], quiet: true, compile_job: true)
    puts "*** CHANGE DETECTED *** To rollback:  #{Origen.config.copy_command} #{display_path_to(b)} #{display_path_to(a)}"
    "#{Origen.config.diff_command} #{display_path_to(a)} #{display_path_to(b)} & #{ENV['EDITOR']} #{file.cleanpath} &"
  end
end

#compile(file_or_dir, options = {}) ⇒ Object

Compile all files found under the source directory, non-erb files will be copied to the destination un-altered



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/origen/generator/compiler.rb', line 40

def compile(file_or_dir, options = {})
  options = {
    check_for_changes:  true,
    sub_template:       false,
    collect_stats:      true,
    ignore_blank_lines: true
  }.merge(options)
  @scope = options[:scope]
  # Doing here so the output_directory (requiring target load) doesn't get hit if
  # it is already defined
  options[:output_directory] ||= output_directory
  @check_for_changes = options[:check_for_changes]
  @options = options
  if options[:sub_template]
    block = options.delete(:block)
    if is_erb?(file_or_dir)
      run_erb(file_or_dir, options, &block)
    else
      f = File.open(file_or_dir)
      content = f.read
      f.close
      insert(content)
    end
  else
    Origen.file_handler.resolve_files(file_or_dir, ignore_with_prefix: '_', import: :template) do |file|
      compile_file(file, options)
    end
  end
end

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

Compile the supplied file if it is an erb template writing the compiled version to the destination directory. If the file is not an erb template it is simply copied un-altered to the destination directory. File must be an absolute path to the file.



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
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/origen/generator/compiler.rb', line 100

def compile_file(file, options = {})
  @current_file = Pathname.new(file)
  # This is used when templates are compiled through a test program, but can
  # be problematic when used to compile files standalone. In practice this may
  # not be an issue except when testing Origen and generating and compiling within
  # the same thread, but clearing this here doesn't seem to do any harm.
  Origen.file_handler.default_extension = nil
  begin
    Origen.log.info "Compiling... #{relative_path_to(file)}" unless options[:quiet]
  rescue
    Origen.log.info "Compiling... #{file}" unless options[:quiet]
  end
  Origen.log.info "  Created... #{relative_path_to(output_file(file, options))}" unless options[:quiet]
  stats.completed_files += 1 if options[:collect_stats]
  if is_erb?(file)
    output = run_erb(file, options)
    f = output_file(file, options).to_s
    if output.is_a?(Pathname)
      FileUtils.mv output.to_s, f
    else
      File.open(f, 'w') { |out| out.puts output }
    end
  else # Just copy it across
    out = output_file(file, options)
    # Delete the target if it already exists, this prevents permission denied errors when copying
    FileUtils.rm_f(out.to_s) if File.exist?(out.to_s)
    FileUtils.cp(file.to_s, out.dirname.to_s)
  end
  if options[:zip]
    `gzip -f -9 #{output_file(file, options)}`
  else
    if @check_for_changes
      check_for_changes(output_file(file, options), reference_file(file, options),
                        comment_char: Origen.app.tester ? Origen.app.tester.program_comment_char : nil,
                        compile_job: true, ignore_blank_lines: options[:ignore_blank_lines])
    end
  end
end

#compile_inline(file, options = {}) ⇒ 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.

Where compile will place the compiled content in an output file, this method will return it as a string to the caller (i.e. without creating an output file)

It expects an absolute path to a single template file as the file argument.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/origen/generator/compiler.rb', line 25

def compile_inline(file, options = {})
  initial_options = options.merge({})
  options = {
    check_for_changes: false,
    sub_template:      false,
    collect_stats:     false,
    initial_options:   initial_options
  }.merge(options)
  @scope = options[:scope]
  file = Pathname.new(file) unless options[:string]
  run_erb(file, options).strip
end

#current_bufferObject



193
194
195
# File 'lib/origen/generator/compiler.rb', line 193

def current_buffer
  (@scope || self).instance_variable_get(@current_buffer || '@_anonymous')
end

#current_buffer=(text) ⇒ Object



197
198
199
# File 'lib/origen/generator/compiler.rb', line 197

def current_buffer=(text)
  (@scope || self).instance_variable_set(@current_buffer || '@_anonymous', text)
end

#display_path_to(file) ⇒ Object



221
222
223
224
225
# File 'lib/origen/generator/compiler.rb', line 221

def display_path_to(file)
  p = relative_path_to(file).to_s
  p.gsub!('/', '\\') if Origen.running_on_windows?
  p
end

#is_erb?(file) ⇒ Boolean

Returns true if the supplied file name has a .erb extension

Returns:

  • (Boolean)


235
236
237
# File 'lib/origen/generator/compiler.rb', line 235

def is_erb?(file)
  !!(file.to_s =~ /.erb$/) || !Origen.config.compile_only_dot_erb_files
end

#merge(file_or_dir, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/origen/generator/compiler.rb', line 70

def merge(file_or_dir, options = {})
  # Compile an up to date reference
  compile(file_or_dir_path, check_for_changes: false, output_directory: merge_reference_directory)
  diffs = []
  Origen.file_handler.resolve_files(file_or_dir, ignore_with_prefix: '_') do |file|
    diffs << merge_file(file, options)
  end
  diffs.compact!
  puts ''
  if diffs.size > 0
    puts 'The following differences are present in the compiled files and must be resolved manually:'
    puts ''
    diffs.each do |diff|
      puts diff
    end
    puts ''
  else
    puts 'Merged successfully!'
  end
end

#merge_file(file, _options = {}) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/origen/generator/compiler.rb', line 209

def merge_file(file, _options = {})
  file = Pathname.new(file)
  Origen.log.info "Merging... #{file.basename}"
  if is_erb?(file) && File.exist?(output_file(file))
    check_for_differences(output_file(file), merge_ref_file(file), file)
  elsif File.exist?(output_file(file))
    if check_for_differences(output_file(file), merge_ref_file(file), file)
      FileUtils.cp(output_file(file), file.dirname.to_s)
    end
  end
end

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



282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/origen/generator/compiler.rb', line 282

def merge_ref_file(file, options = {})
  options = {
    directory: merge_reference_directory
  }.merge(options)
  # return @merge_ref_file if @merge_ref_file
  sub_dir = Origen.file_handler.sub_dir_of(file).to_s
  sub_dir = nil if sub_dir == '.'
  filename = file.basename.to_s.gsub('.erb', '')
  # filename.gsub!('target', $target.id) if filename =~ /target/ && $target.id
  output = Pathname.new("#{options[:directory]}#{sub_dir ? '/' + sub_dir : ''}/#{filename}")
  FileUtils.mkdir_p(output.dirname.to_s) unless File.exist?(output.dirname.to_s)
  # @merge_ref_file = output
  output
end

#merge_reference_directoryObject



247
248
249
# File 'lib/origen/generator/compiler.rb', line 247

def merge_reference_directory
  "#{Origen.root}/.merge_ref"
end

#output_directoryObject



239
240
241
# File 'lib/origen/generator/compiler.rb', line 239

def output_directory
  Origen.file_handler.output_directory
end

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

Returns the output file corresponding to the given source file, the destination directory will be created if it doesn’t exist.



253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/origen/generator/compiler.rb', line 253

def output_file(file, options = {})
  options = {
    output_directory: output_directory
  }.merge(options)
  # return @output_file if @output_file
  sub_dir = options[:output_sub_dir] || Origen.file_handler.sub_dir_of(file).to_s
  sub_dir = nil if sub_dir == '.'
  filename = options[:output_file_name] || file.basename.to_s.gsub('.erb', '')
  # filename.gsub!('target', $target.id) if filename =~ /target/ && $target.id
  output = Pathname.new("#{options[:output_directory]}#{sub_dir ? '/' + sub_dir : ''}/#{filename}")
  FileUtils.mkdir_p(output.dirname.to_s) unless File.exist?(output.dirname.to_s)
  # @output_file = output
  output
end

#reference_directoryObject



243
244
245
# File 'lib/origen/generator/compiler.rb', line 243

def reference_directory
  Origen.file_handler.reference_directory
end

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

Returns the reference file corresponding to the given source file, the destination directory will be created if it doesn’t exist.



270
271
272
273
274
275
276
277
278
279
280
# File 'lib/origen/generator/compiler.rb', line 270

def reference_file(file, options = {})
  # return @reference_file if @reference_file
  sub_dir = Origen.file_handler.sub_dir_of(file).to_s
  sub_dir = nil if sub_dir == '.'
  filename = options[:output_file_name] || file.basename.to_s.gsub('.erb', '')
  # filename.gsub!('target', $target.id) if filename =~ /target/ && $target.id
  reference = Pathname.new("#{reference_directory}#{sub_dir ? '/' + sub_dir : ''}/#{filename}")
  FileUtils.mkdir_p(reference.dirname.to_s) unless File.exist?(reference.dirname.to_s)
  # @reference_file = reference
  reference
end

#run_erb(file, opts = {}, &block) ⇒ 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
# File 'lib/origen/generator/compiler.rb', line 139

def run_erb(file, opts = {}, &block)
  # Refresh the target to start all settings from scratch each time
  # This is an easy way to reset all registered values
  if opts[:preserve_target]
    options[:preserve_target] = opts.delete(:preserve_target)
  end
  Origen.app.reload_target! unless options[:preserve_target]
  # Record the current file, this can be used to resolve any relative path
  # references in the file about to be compiled
  Origen.file_handler.current_file = file
  # Make the file and options available to the template
  if opts[:initial_options] || opts[:options]
    options.merge!(opts.delete(:initial_options) || opts.delete(:options))
  end
  options[:file] = file
  options[:top_level_file] = current_file
  b = _get_binding(opts, &block)
  if opts[:string]
    content = file
    @current_buffer = '@_string_template'
    buffer = @current_buffer
  else
    content = File.read(file.to_s)
    buffer = buffer_name_for(file)
  end
  if block_given?
    content = ERB.new(content, 0, '%<>', buffer).result(b)
  else
    content = ERB.new(content, 0, Origen.config.erb_trim_mode, buffer).result(b)
  end
  insert(content)
end

#statsObject



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

def stats
  Origen.app.stats
end