Skip to content
This repository was archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Pass class graph to collectors
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Mar 28, 2024
1 parent 7a0f061 commit 0e53762
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
16 changes: 16 additions & 0 deletions lib/mosaik/collector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module MOSAIK
class Collector
attr_reader :options, :graph

def initialize(options, graph)
@options = options
@graph = graph
end

def call
raise NotImplementedError
end
end
end
8 changes: 1 addition & 7 deletions lib/mosaik/collectors/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

module MOSAIK
module Collectors
class History
attr_reader :options

def initialize(options)
@options = options
end

class History < Collector
def call
git = Git.open(MOSAIK.options.directory, log: ::Logger.new(File::NULL))

Expand Down
15 changes: 3 additions & 12 deletions lib/mosaik/collectors/static.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

module MOSAIK
module Collectors
class Static
attr_reader :options

def initialize(options)
@options = options
end

class Static < Collector
PARSERS = {
".rb" => Parsers::Ruby,
}.freeze
Expand Down Expand Up @@ -43,10 +37,7 @@ def call
debug (" " * constant.name.scan("::").count) + constant.name
end

# Construct a call graph
graph = GraphViz.new(:call_graph, type: :digraph)

registry.each { |constant| construct(constant, graph) }
registry.each { |constant| construct(constant) } # rubocop:disable Style/CombinableLoops

# Write the graph to a file
graph.output(dot: options[:output])
Expand All @@ -56,7 +47,7 @@ def call

private

def construct(constant, graph)
def construct(constant)
# Get or create the node for the constant
node = graph.get_node(constant.name) || graph.add_node(constant.name)

Expand Down
26 changes: 24 additions & 2 deletions lib/mosaik/commands/collect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,39 @@ def prepare
end

def start
# Construct graph of classes based on file path
graph = GraphViz.new(:class_graph, type: :digraph)

MOSAIK.configuration.files.each do |file|
# Resolve file path to class name
class_name = resolver.resolve(file)

# Add class to graph
graph.add_node(class_name)
end

# Collect data and add to graph
case options[:type]
when "static"
Collectors::Static
.new(options)
.new(options, graph)
.call
when "history"
Collectors::History
.new(options)
.new(options, graph)
.call
end
end

private

def resolver
@resolver ||= Resolver.new(
MOSAIK.options.directory,
MOSAIK.configuration.load_paths,
MOSAIK.configuration.overrides,
)
end
end
end
end

0 comments on commit 0e53762

Please sign in to comment.