lib/origen/application.rb in origen-0.44.0 vs lib/origen/application.rb in origen-0.50.0

- old
+ new

@@ -60,10 +60,37 @@ def respond_to?(*args) super || instance.respond_to?(*args) end + # Returns the application instance (i.e. main app or the plugin) that owns the given class/module + # (literal, string or symbol representation is accepted) or object instance. + # Returns nil if no matching Origen application can be found. + # + # Origen::Application.from_namespace(MyApp) # => <my_app instance> + # Origen::Application.from_namespace(MyApp::MyClass) # => <my_app instance> + # Origen::Application.from_namespace('MyApp::MyClass') # => <my_app instance> + # Origen::Application.from_namespace(<my_app::my_class instance>) # => <my_app instance> + def from_namespace(item) + unless item.is_a?(String) + if item.is_a?(Module) || item.is_a?(Class) || item.is_a?(Symbol) + item = item.to_s + else # Assume to be an object instance in this case + item = item.class.to_s + end + end + namespace = item.split('::').first + @apps_by_namespace ||= {} + @apps_by_namespace[namespace] ||= begin + return Origen.app if Origen.app.namespace == namespace + Origen.app.plugins.each do |plugin| + return plugin if plugin.namespace == namespace + end + nil + end + end + protected def method_missing(*args, &block) instance.send(*args, &block) end @@ -102,10 +129,67 @@ end end end end + # @api private + # + # Returns a lookup table for all block definitions (app/blocks) that the app contains + def blocks_files + # There seems to be some issue with this cache being corrupted when running the test suite + # in Travis, but don't believe that this is really an issue in practice and cannot replicate + # it locally. Therefore maintaining the cache of this potentially expensive operation except + # from when running in CI. + @blocks_files = nil if ENV['CONTINUOUS_INTEGRATION'] + @blocks_files ||= begin + files = {} + block_dir = Pathname.new(File.join(root, 'app', 'blocks')) + if block_dir.exist? + block_dir.children.each do |item| + if item.directory? + _add_block_files(files, block_dir, item) + end + end + end + files + end + end + + # @api private + def _add_block_files(files, block_dir, current_dir, sub_block = false) + fields = current_dir.relative_path_from(block_dir).to_s.split('/') + fields.delete('derivatives') + fields.delete('sub_blocks') + path = fields.join('/') + files[path] ||= {} + files[path][:_sub_block] = true if sub_block + Dir.glob(current_dir.join('*.rb')).each do |file| + file = Pathname.new(file) + type = file.basename('.rb').to_s.to_sym + unless type == :model || type == :controller + files[path][type] ||= [] + files[path][type] << file + end + end + derivatives = current_dir.join('derivatives') + if derivatives.exist? + derivatives.children.each do |item| + if item.directory? + _add_block_files(files, block_dir, item) + end + end + end + sub_blocks = current_dir.join('sub_blocks') + if sub_blocks.exist? + sub_blocks.children.each do |item| + if item.directory? + _add_block_files(files, block_dir, item, true) + end + end + end + end + def current_job current_jobs.last end def current_jobs @@ -764,10 +848,11 @@ def load_target!(options = {}) options = { force_debug: false }.merge(options) @on_create_called = false + @target_loading = true if options[:reload] @target_load_count = 0 else @target_load_count ||= 0 @target_load_count += 1 @@ -812,11 +897,11 @@ # will be associated with (and cleared out upon reload of) the current target listeners_for(:on_load_target).each(&:on_load_target) end listeners_for(:after_load_target).each(&:after_load_target) Origen.app.plugins.validate_production_status - # @target_instantiated = true + @target_loading = false end # Returns true if the on_create callback has already been called during a target load def on_create_called? !!@on_create_called @@ -898,10 +983,14 @@ def target_instantiated? @target_instantiated end + def target_loading? + @target_loading || false + end + # Prepends the application name to the fail message and throws a RuntimeError exception. # Very similar to the plain <code>fail</code> method with the addition of prepending the application name. # Prepended message: 'Fail in app.name: ' # If no message if provided, message is set to 'Fail in app.name' # @param message [String] Message to print with the exception. If the message option is nil, a default message will be used instead. @@ -954,10 +1043,10 @@ # class MyApplication < Origen::Application # require "my_backend" # in lib/my_backend # config.i18n.backend = MyBackend # end def add_lib_to_load_path! #:nodoc: - [root.join('lib'), root.join('vendor', 'lib')].each do |path| + [root.join('lib'), root.join('vendor', 'lib'), root.join('app', 'lib')].each do |path| $LOAD_PATH.unshift(path.to_s) if File.exist?(path) && !$LOAD_PATH.include?(path.to_s) end end end end