Class: Origen::Generator::Compiler
- Includes:
- Helpers, Comparator, Renderer
- Defined in:
- lib/origen/generator/compiler.rb
Overview
:nodoc: all
Instance Attribute Summary collapse
-
#current_file ⇒ Object
readonly
During a compile this will return the current top-level file being compiled.
Instance Method Summary collapse
- #_get_binding(opts, &block) ⇒ Object private
-
#buffer_name_for(file) ⇒ Object
Returns the ERB buffer name for the given file, something like “@my_file_name”.
- #check_for_differences(a, b, file) ⇒ Object
-
#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.
-
#compile_file(file, options = {}) ⇒ Object
Compile the supplied file if it is an erb template writing the compiled version to the destination directory.
-
#compile_inline(file, options = {}) ⇒ Object
private
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).
- #current_buffer ⇒ Object
- #current_buffer=(text) ⇒ Object
- #display_path_to(file) ⇒ Object
-
#is_erb?(file) ⇒ Boolean
Returns true if the supplied file name has a .erb extension.
- #merge(file_or_dir, options = {}) ⇒ Object
- #merge_file(file, _options = {}) ⇒ Object
- #merge_ref_file(file, options = {}) ⇒ Object
- #merge_reference_directory ⇒ Object
- #output_directory ⇒ Object
-
#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.
- #reference_directory ⇒ Object
-
#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.
- #run_erb(file, opts = {}, &block) ⇒ Object
- #stats ⇒ Object
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_file ⇒ Object (readonly)
During a compile this will return the current top-level file being compiled
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 192 193 |
# 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| # rubocop:disable Lint/UselessAssignment = local_opts # rubocop:enable Lint/UselessAssignment 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(, &block) end b end |
#buffer_name_for(file) ⇒ Object
Returns the ERB buffer name for the given file, something like “@my_file_name”
204 205 206 207 208 209 |
# File 'lib/origen/generator/compiler.rb', line 204 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
229 230 231 232 233 234 |
# File 'lib/origen/generator/compiler.rb', line 229 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, = {}) = { check_for_changes: true, sub_template: false, collect_stats: true, ignore_blank_lines: true }.merge() @scope = [:scope] # Doing here so the output_directory (requiring target load) doesn't get hit if # it is already defined [:output_directory] ||= output_directory @check_for_changes = [:check_for_changes] @options = if [:sub_template] block = .delete(:block) if is_erb?(file_or_dir) run_erb(file_or_dir, , &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, ) 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, = {}) @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 [:quiet] rescue Origen.log.info "Compiling... #{file}" unless [:quiet] end Origen.log.info " Created... #{relative_path_to(output_file(file, ))}" unless [:quiet] stats.completed_files += 1 if [:collect_stats] if is_erb?(file) output = run_erb(file, ) f = output_file(file, ).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, ) # 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 [:zip] `gzip -f -9 #{output_file(file, )}` else if @check_for_changes check_for_changes(output_file(file, ), reference_file(file, ), comment_char: Origen.app.tester ? Origen.app.tester.program_comment_char : nil, compile_job: true, ignore_blank_lines: [: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, = {}) = .merge({}) = { check_for_changes: false, sub_template: false, collect_stats: false, initial_options: }.merge() @scope = [:scope] file = Pathname.new(file) unless [:string] run_erb(file, ).strip end |
#current_buffer ⇒ Object
195 196 197 |
# File 'lib/origen/generator/compiler.rb', line 195 def current_buffer (@scope || self).instance_variable_get(@current_buffer || '@_anonymous') end |
#current_buffer=(text) ⇒ Object
199 200 201 |
# File 'lib/origen/generator/compiler.rb', line 199 def current_buffer=(text) (@scope || self).instance_variable_set(@current_buffer || '@_anonymous', text) end |
#display_path_to(file) ⇒ Object
223 224 225 226 227 |
# File 'lib/origen/generator/compiler.rb', line 223 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
237 238 239 |
# File 'lib/origen/generator/compiler.rb', line 237 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, = {}) # 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, ) 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
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/origen/generator/compiler.rb', line 211 def merge_file(file, = {}) 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
284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/origen/generator/compiler.rb', line 284 def merge_ref_file(file, = {}) = { directory: merge_reference_directory }.merge() # 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("#{[: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_directory ⇒ Object
249 250 251 |
# File 'lib/origen/generator/compiler.rb', line 249 def merge_reference_directory "#{Origen.root}/.merge_ref" end |
#output_directory ⇒ Object
241 242 243 |
# File 'lib/origen/generator/compiler.rb', line 241 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.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/origen/generator/compiler.rb', line 255 def output_file(file, = {}) = { output_directory: output_directory }.merge() # return @output_file if @output_file sub_dir = [:output_sub_dir] || Origen.file_handler.sub_dir_of(file).to_s sub_dir = nil if sub_dir == '.' filename = [:output_file_name] || file.basename.to_s.gsub('.erb', '') # filename.gsub!('target', $target.id) if filename =~ /target/ && $target.id output = Pathname.new("#{[: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_directory ⇒ Object
245 246 247 |
# File 'lib/origen/generator/compiler.rb', line 245 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.
272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/origen/generator/compiler.rb', line 272 def reference_file(file, = {}) # return @reference_file if @reference_file sub_dir = Origen.file_handler.sub_dir_of(file).to_s sub_dir = nil if sub_dir == '.' filename = [: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] [:preserve_target] = opts.delete(:preserve_target) end Origen.app.reload_target! unless [: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] .merge!(opts.delete(:initial_options) || opts.delete(:options)) end [:file] = file [: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 |