Class: Datadog::CI::SourceCode::StaticDependenciesExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ci/source_code/static_dependencies_extractor.rb

Overview

StaticDependenciesExtractor extracts static constant dependencies from Ruby bytecode.

For each ISeq (compiled Ruby code), it:

  1. Extracts the source file path

  2. Filters by root_path and ignored_path

  3. Scans bytecode for constant references

  4. Resolves constants to their source file locations

  5. Filters dependency paths by root_path and ignored_path

Examples:

extractor = StaticDependenciesExtractor.new("/app", "/app/vendor")
iseq = RubyVM::InstructionSequence.of(some_method)
extractor.extract(iseq)
deps = extractor.dependencies_map
# => { "/app/foo.rb" => { "/app/bar.rb" => true } }

Defined Under Namespace

Classes: BytecodeScanner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path, ignored_path = nil) ⇒ StaticDependenciesExtractor

Initialize a new StaticDependenciesExtractor.

Parameters:

  • Only process files under this path

  • (defaults to: nil)

    Exclude files under this path



151
152
153
154
155
156
# File 'lib/datadog/ci/source_code/static_dependencies_extractor.rb', line 151

def initialize(root_path, ignored_path = nil)
  @root_path = root_path
  @ignored_path = ignored_path
  @dependencies_map = {}
  @bytecode_scanner = BytecodeScanner.new
end

Instance Attribute Details

#dependencies_mapHash{String => Hash{String => Boolean}} (readonly)

Returns Map of source file to dependencies.

Returns:

  • Map of source file to dependencies



139
140
141
# File 'lib/datadog/ci/source_code/static_dependencies_extractor.rb', line 139

def dependencies_map
  @dependencies_map
end

#ignored_pathString? (readonly)

Returns Ignored path prefix for exclusion.

Returns:

  • Ignored path prefix for exclusion



145
146
147
# File 'lib/datadog/ci/source_code/static_dependencies_extractor.rb', line 145

def ignored_path
  @ignored_path
end

#root_pathString (readonly)

Returns Root path prefix for filtering.

Returns:

  • Root path prefix for filtering



142
143
144
# File 'lib/datadog/ci/source_code/static_dependencies_extractor.rb', line 142

def root_path
  @root_path
end

Instance Method Details

#extract(iseq) ⇒ void

This method returns an undefined value.

Extract constant dependencies from an ISeq.

Parameters:

  • The instruction sequence to process



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/datadog/ci/source_code/static_dependencies_extractor.rb', line 162

def extract(iseq)
  path = extract_absolute_path(iseq)
  return if path.nil?
  return unless PathFilter.included?(path, root_path, ignored_path)

  body = extract_body(iseq)
  return if body.nil?

  deps = get_or_create_deps(path)
  constant_names = @bytecode_scanner.scan(body)

  constant_names.each do |const_name|
    resolve_and_store_dependency(const_name, deps)
  end
end

#resetvoid

This method returns an undefined value.

Reset the dependencies map.



181
182
183
# File 'lib/datadog/ci/source_code/static_dependencies_extractor.rb', line 181

def reset
  @dependencies_map = {}
end