lib/load_tracer.rb in load_tracer-0.2.0 vs lib/load_tracer.rb in load_tracer-0.3.0

- old
+ new

@@ -1,8 +1,9 @@ require 'binding_of_caller' require 'load_tracer/formatter/default' require 'load_tracer/formatter/dot' +require 'load_tracer/formatter/json' require 'load_tracer/version' module Kernel unless defined?(__original_require__) alias __original_require__ require @@ -40,54 +41,59 @@ class LoadTracer FileSpec = Struct.new(:name, :path, :dependencies, :reverse_dependencies, keyword_init: true) LOAD_METHODS = %i(require require_relative load autoload) - def self.trace(format: nil) - instance = new + def self.trace(format: nil, exclude_files: []) + instance = new(exclude_files: exclude_files) instance.tracer.enable { yield } instance.report(format: format) end - def initialize + def initialize(exclude_files: []) @dependencies = Hash.new { |hash, key| hash[key] = [] } @reverse_dependencies = Hash.new { |hash, key| hash[key] = [] } + @exclude_files = exclude_files @not_found_features = [] end def tracer - TracePoint.new(:return) do |tp| + TracePoint.new(:call) do |tp| next unless LOAD_METHODS.include?(tp.method_id) next if tp.defined_class != ::Kernel next if tp.path != __FILE__ - case tp.event - when :return - bl = caller_locations[1] - feature = get_feature(tp) + bl = caller_locations[1] + feature = get_feature(tp) - if bl.absolute_path.nil? - bl = find_caller_of_internal_library(feature) - end + if bl.absolute_path.nil? + bl = find_caller_of_internal_library(feature) + end - path = find_path(feature) || find_path(File.expand_path(feature, File.dirname(bl.path))) + next if @exclude_files.include?(File.basename(bl.absolute_path)) - if path.nil? - @not_found_features << feature - next - end + path = find_path(feature) || find_path(File.expand_path(feature, File.dirname(bl.path))) - @dependencies[bl.absolute_path] << path - @reverse_dependencies[path] << bl.absolute_path + if path.nil? + @not_found_features << feature + next end + + @dependencies[bl.absolute_path] << path + @reverse_dependencies[path] << bl.absolute_path end end def report(format:) case format when :dot DotFormatter.export( dependencies: @dependencies + ) + when :json + JsonFormatter.export( + dependencies: @dependencies, + reverse_dependencies: @reverse_dependencies ) else DefaultFormatter.export( dependencies: @dependencies, reverse_dependencies: @reverse_dependencies