Class: ArchUnit::GraphReporting::FluentApi::ProjectGraphBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/archunit/graph/fluentapi/project_graph_builder.rb

Overview

Immutable query builder for dependency graph snapshots and reports.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_locator: nil, options: nil, check_options: nil) ⇒ ProjectGraphBuilder

Returns a new instance of ProjectGraphBuilder.



40
41
42
43
44
45
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 40

def initialize(project_locator: nil, options: nil, check_options: nil)
  @project_locator = immutable_project_locator(project_locator)
  @options = Projection::GraphQueryOptions.resolve(options)
  @check_options = immutable_check_options(check_options)
  freeze
end

Instance Attribute Details

#check_options ⇒ Object (readonly)

Returns the value of attribute check_options.



38
39
40
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 38

def check_options
  @check_options
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



38
39
40
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 38

def options
  @options
end

#project_locator ⇒ Object (readonly)

Returns the value of attribute project_locator.



38
39
40
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 38

def project_locator
  @project_locator
end

Instance Method Details

#collapse_by_pattern(pattern, replacement = '\\1') ⇒ ProjectGraphBuilder

Groups node names through a regular-expression replacement.

Returns:



88
89
90
91
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 88

def collapse_by_pattern(pattern, replacement = '\\1')
  collapse = Projection::PatternCollapse.from(pattern, replacement)
  with_options(options.with(collapse:))
end

#collapse_to_folder_depth(depth) ⇒ ProjectGraphBuilder

Groups file nodes by the requested project-relative folder depth.

Returns:



82
83
84
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 82

def collapse_to_folder_depth(depth)
  with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
end

#dependents_of(pattern, except: nil) ⇒ ProjectGraphBuilder

Keeps matching nodes and everything that depends on them.

Returns:



75
76
77
78
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 75

def dependents_of(pattern, except: nil)
  filter = Common::RegexFactory.path_matcher(pattern, except:)
  with_options(options.with(dependents_of: filter))
end

#export_as_csv(output_path) ⇒ nil

Returns writes CSV to output_path.

Returns:

  • (nil) —

    writes CSV to output_path



37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 37

class ProjectGraphBuilder
  attr_reader :project_locator, :options, :check_options

  def initialize(project_locator: nil, options: nil, check_options: nil)
    @project_locator = immutable_project_locator(project_locator)
    @options = Projection::GraphQueryOptions.resolve(options)
    @check_options = immutable_check_options(check_options)
    freeze
  end

  # Includes standard-library and gem imports in the report.
  # @return [ProjectGraphBuilder]
  def include_external_dependencies
    with_options(options.with(include_external_dependencies: true))
  end

  # Includes the self-edges that represent dependency-free source files.
  # @return [ProjectGraphBuilder]
  def include_self_dependencies
    with_options(options.with(include_self_dependencies: true))
  end

  # Keeps matching nodes and neighbors within depth hops.
  # @return [ProjectGraphBuilder]
  def focus_on(pattern, depth = 1, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(focus: filter, focus_depth: depth))
  end

  # Keeps nodes reachable from matching starting nodes.
  # @return [ProjectGraphBuilder]
  def reachable_from(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(reachable_from: filter))
  end

  # Keeps matching nodes and everything that depends on them.
  # @return [ProjectGraphBuilder]
  def dependents_of(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(dependents_of: filter))
  end

  # Groups file nodes by the requested project-relative folder depth.
  # @return [ProjectGraphBuilder]
  def collapse_to_folder_depth(depth)
    with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
  end

  # Groups node names through a regular-expression replacement.
  # @return [ProjectGraphBuilder]
  def collapse_by_pattern(pattern, replacement = '\\1')
    collapse = Projection::PatternCollapse.from(pattern, replacement)
    with_options(options.with(collapse:))
  end

  # Sets the report title.
  # @return [ProjectGraphBuilder]
  def titled(title)
    with_options(options.with(title:))
  end

  # Attaches extraction-related CheckOptions to this report.
  # @return [ProjectGraphBuilder]
  def with_check_options(value)
    self.class.new(project_locator:, options:, check_options: value)
  end

  # @return [GraphReportSnapshot] the immutable queried graph
  def snapshot
    graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
    Projection::SnapshotFactory.create(graph, options)
  end

  # @return [GraphReportSummary] counts for the current snapshot
  def summary
    snapshot.summary
  end

  Rendering::GraphRenderer::RENDERERS.each_key do |format|
    define_method("to_#{format}") do
      Rendering::GraphRenderer.render(snapshot, format)
    end

    define_method("export_as_#{format}") do |output_path|
      Rendering::GraphRenderer.export(snapshot, format, output_path)
    end
  end

  private

  def with_options(new_options)
    self.class.new(project_locator:, options: new_options, check_options:)
  end

  def immutable_project_locator(locator)
    return if locator.nil?

    locator = locator.to_path if locator.respond_to?(:to_path)
    return locator.dup.freeze if locator.is_a?(String) && !locator.empty?

    raise ArgumentError, 'project_locator must be a non-empty path or nil'
  end

  def immutable_check_options(value)
    return if value.nil?
    return value if value.is_a?(Common::FluentApi::CheckOptions)

    raise ArgumentError, 'check_options must be a CheckOptions value or nil'
  end
end

#export_as_d2(output_path) ⇒ nil

Returns writes D2 to output_path.

Returns:

  • (nil) —

    writes D2 to output_path



37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 37

class ProjectGraphBuilder
  attr_reader :project_locator, :options, :check_options

  def initialize(project_locator: nil, options: nil, check_options: nil)
    @project_locator = immutable_project_locator(project_locator)
    @options = Projection::GraphQueryOptions.resolve(options)
    @check_options = immutable_check_options(check_options)
    freeze
  end

  # Includes standard-library and gem imports in the report.
  # @return [ProjectGraphBuilder]
  def include_external_dependencies
    with_options(options.with(include_external_dependencies: true))
  end

  # Includes the self-edges that represent dependency-free source files.
  # @return [ProjectGraphBuilder]
  def include_self_dependencies
    with_options(options.with(include_self_dependencies: true))
  end

  # Keeps matching nodes and neighbors within depth hops.
  # @return [ProjectGraphBuilder]
  def focus_on(pattern, depth = 1, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(focus: filter, focus_depth: depth))
  end

  # Keeps nodes reachable from matching starting nodes.
  # @return [ProjectGraphBuilder]
  def reachable_from(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(reachable_from: filter))
  end

  # Keeps matching nodes and everything that depends on them.
  # @return [ProjectGraphBuilder]
  def dependents_of(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(dependents_of: filter))
  end

  # Groups file nodes by the requested project-relative folder depth.
  # @return [ProjectGraphBuilder]
  def collapse_to_folder_depth(depth)
    with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
  end

  # Groups node names through a regular-expression replacement.
  # @return [ProjectGraphBuilder]
  def collapse_by_pattern(pattern, replacement = '\\1')
    collapse = Projection::PatternCollapse.from(pattern, replacement)
    with_options(options.with(collapse:))
  end

  # Sets the report title.
  # @return [ProjectGraphBuilder]
  def titled(title)
    with_options(options.with(title:))
  end

  # Attaches extraction-related CheckOptions to this report.
  # @return [ProjectGraphBuilder]
  def with_check_options(value)
    self.class.new(project_locator:, options:, check_options: value)
  end

  # @return [GraphReportSnapshot] the immutable queried graph
  def snapshot
    graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
    Projection::SnapshotFactory.create(graph, options)
  end

  # @return [GraphReportSummary] counts for the current snapshot
  def summary
    snapshot.summary
  end

  Rendering::GraphRenderer::RENDERERS.each_key do |format|
    define_method("to_#{format}") do
      Rendering::GraphRenderer.render(snapshot, format)
    end

    define_method("export_as_#{format}") do |output_path|
      Rendering::GraphRenderer.export(snapshot, format, output_path)
    end
  end

  private

  def with_options(new_options)
    self.class.new(project_locator:, options: new_options, check_options:)
  end

  def immutable_project_locator(locator)
    return if locator.nil?

    locator = locator.to_path if locator.respond_to?(:to_path)
    return locator.dup.freeze if locator.is_a?(String) && !locator.empty?

    raise ArgumentError, 'project_locator must be a non-empty path or nil'
  end

  def immutable_check_options(value)
    return if value.nil?
    return value if value.is_a?(Common::FluentApi::CheckOptions)

    raise ArgumentError, 'check_options must be a CheckOptions value or nil'
  end
end

#export_as_dot(output_path) ⇒ nil

Returns writes Graphviz DOT to output_path.

Returns:

  • (nil) —

    writes Graphviz DOT to output_path



37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 37

class ProjectGraphBuilder
  attr_reader :project_locator, :options, :check_options

  def initialize(project_locator: nil, options: nil, check_options: nil)
    @project_locator = immutable_project_locator(project_locator)
    @options = Projection::GraphQueryOptions.resolve(options)
    @check_options = immutable_check_options(check_options)
    freeze
  end

  # Includes standard-library and gem imports in the report.
  # @return [ProjectGraphBuilder]
  def include_external_dependencies
    with_options(options.with(include_external_dependencies: true))
  end

  # Includes the self-edges that represent dependency-free source files.
  # @return [ProjectGraphBuilder]
  def include_self_dependencies
    with_options(options.with(include_self_dependencies: true))
  end

  # Keeps matching nodes and neighbors within depth hops.
  # @return [ProjectGraphBuilder]
  def focus_on(pattern, depth = 1, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(focus: filter, focus_depth: depth))
  end

  # Keeps nodes reachable from matching starting nodes.
  # @return [ProjectGraphBuilder]
  def reachable_from(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(reachable_from: filter))
  end

  # Keeps matching nodes and everything that depends on them.
  # @return [ProjectGraphBuilder]
  def dependents_of(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(dependents_of: filter))
  end

  # Groups file nodes by the requested project-relative folder depth.
  # @return [ProjectGraphBuilder]
  def collapse_to_folder_depth(depth)
    with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
  end

  # Groups node names through a regular-expression replacement.
  # @return [ProjectGraphBuilder]
  def collapse_by_pattern(pattern, replacement = '\\1')
    collapse = Projection::PatternCollapse.from(pattern, replacement)
    with_options(options.with(collapse:))
  end

  # Sets the report title.
  # @return [ProjectGraphBuilder]
  def titled(title)
    with_options(options.with(title:))
  end

  # Attaches extraction-related CheckOptions to this report.
  # @return [ProjectGraphBuilder]
  def with_check_options(value)
    self.class.new(project_locator:, options:, check_options: value)
  end

  # @return [GraphReportSnapshot] the immutable queried graph
  def snapshot
    graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
    Projection::SnapshotFactory.create(graph, options)
  end

  # @return [GraphReportSummary] counts for the current snapshot
  def summary
    snapshot.summary
  end

  Rendering::GraphRenderer::RENDERERS.each_key do |format|
    define_method("to_#{format}") do
      Rendering::GraphRenderer.render(snapshot, format)
    end

    define_method("export_as_#{format}") do |output_path|
      Rendering::GraphRenderer.export(snapshot, format, output_path)
    end
  end

  private

  def with_options(new_options)
    self.class.new(project_locator:, options: new_options, check_options:)
  end

  def immutable_project_locator(locator)
    return if locator.nil?

    locator = locator.to_path if locator.respond_to?(:to_path)
    return locator.dup.freeze if locator.is_a?(String) && !locator.empty?

    raise ArgumentError, 'project_locator must be a non-empty path or nil'
  end

  def immutable_check_options(value)
    return if value.nil?
    return value if value.is_a?(Common::FluentApi::CheckOptions)

    raise ArgumentError, 'check_options must be a CheckOptions value or nil'
  end
end

#export_as_html(output_path) ⇒ nil

Returns writes self-contained HTML to output_path.

Returns:

  • (nil) —

    writes self-contained HTML to output_path



37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 37

class ProjectGraphBuilder
  attr_reader :project_locator, :options, :check_options

  def initialize(project_locator: nil, options: nil, check_options: nil)
    @project_locator = immutable_project_locator(project_locator)
    @options = Projection::GraphQueryOptions.resolve(options)
    @check_options = immutable_check_options(check_options)
    freeze
  end

  # Includes standard-library and gem imports in the report.
  # @return [ProjectGraphBuilder]
  def include_external_dependencies
    with_options(options.with(include_external_dependencies: true))
  end

  # Includes the self-edges that represent dependency-free source files.
  # @return [ProjectGraphBuilder]
  def include_self_dependencies
    with_options(options.with(include_self_dependencies: true))
  end

  # Keeps matching nodes and neighbors within depth hops.
  # @return [ProjectGraphBuilder]
  def focus_on(pattern, depth = 1, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(focus: filter, focus_depth: depth))
  end

  # Keeps nodes reachable from matching starting nodes.
  # @return [ProjectGraphBuilder]
  def reachable_from(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(reachable_from: filter))
  end

  # Keeps matching nodes and everything that depends on them.
  # @return [ProjectGraphBuilder]
  def dependents_of(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(dependents_of: filter))
  end

  # Groups file nodes by the requested project-relative folder depth.
  # @return [ProjectGraphBuilder]
  def collapse_to_folder_depth(depth)
    with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
  end

  # Groups node names through a regular-expression replacement.
  # @return [ProjectGraphBuilder]
  def collapse_by_pattern(pattern, replacement = '\\1')
    collapse = Projection::PatternCollapse.from(pattern, replacement)
    with_options(options.with(collapse:))
  end

  # Sets the report title.
  # @return [ProjectGraphBuilder]
  def titled(title)
    with_options(options.with(title:))
  end

  # Attaches extraction-related CheckOptions to this report.
  # @return [ProjectGraphBuilder]
  def with_check_options(value)
    self.class.new(project_locator:, options:, check_options: value)
  end

  # @return [GraphReportSnapshot] the immutable queried graph
  def snapshot
    graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
    Projection::SnapshotFactory.create(graph, options)
  end

  # @return [GraphReportSummary] counts for the current snapshot
  def summary
    snapshot.summary
  end

  Rendering::GraphRenderer::RENDERERS.each_key do |format|
    define_method("to_#{format}") do
      Rendering::GraphRenderer.render(snapshot, format)
    end

    define_method("export_as_#{format}") do |output_path|
      Rendering::GraphRenderer.export(snapshot, format, output_path)
    end
  end

  private

  def with_options(new_options)
    self.class.new(project_locator:, options: new_options, check_options:)
  end

  def immutable_project_locator(locator)
    return if locator.nil?

    locator = locator.to_path if locator.respond_to?(:to_path)
    return locator.dup.freeze if locator.is_a?(String) && !locator.empty?

    raise ArgumentError, 'project_locator must be a non-empty path or nil'
  end

  def immutable_check_options(value)
    return if value.nil?
    return value if value.is_a?(Common::FluentApi::CheckOptions)

    raise ArgumentError, 'check_options must be a CheckOptions value or nil'
  end
end

#export_as_json(output_path) ⇒ nil

Returns writes JSON to output_path.

Returns:

  • (nil) —

    writes JSON to output_path



37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 37

class ProjectGraphBuilder
  attr_reader :project_locator, :options, :check_options

  def initialize(project_locator: nil, options: nil, check_options: nil)
    @project_locator = immutable_project_locator(project_locator)
    @options = Projection::GraphQueryOptions.resolve(options)
    @check_options = immutable_check_options(check_options)
    freeze
  end

  # Includes standard-library and gem imports in the report.
  # @return [ProjectGraphBuilder]
  def include_external_dependencies
    with_options(options.with(include_external_dependencies: true))
  end

  # Includes the self-edges that represent dependency-free source files.
  # @return [ProjectGraphBuilder]
  def include_self_dependencies
    with_options(options.with(include_self_dependencies: true))
  end

  # Keeps matching nodes and neighbors within depth hops.
  # @return [ProjectGraphBuilder]
  def focus_on(pattern, depth = 1, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(focus: filter, focus_depth: depth))
  end

  # Keeps nodes reachable from matching starting nodes.
  # @return [ProjectGraphBuilder]
  def reachable_from(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(reachable_from: filter))
  end

  # Keeps matching nodes and everything that depends on them.
  # @return [ProjectGraphBuilder]
  def dependents_of(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(dependents_of: filter))
  end

  # Groups file nodes by the requested project-relative folder depth.
  # @return [ProjectGraphBuilder]
  def collapse_to_folder_depth(depth)
    with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
  end

  # Groups node names through a regular-expression replacement.
  # @return [ProjectGraphBuilder]
  def collapse_by_pattern(pattern, replacement = '\\1')
    collapse = Projection::PatternCollapse.from(pattern, replacement)
    with_options(options.with(collapse:))
  end

  # Sets the report title.
  # @return [ProjectGraphBuilder]
  def titled(title)
    with_options(options.with(title:))
  end

  # Attaches extraction-related CheckOptions to this report.
  # @return [ProjectGraphBuilder]
  def with_check_options(value)
    self.class.new(project_locator:, options:, check_options: value)
  end

  # @return [GraphReportSnapshot] the immutable queried graph
  def snapshot
    graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
    Projection::SnapshotFactory.create(graph, options)
  end

  # @return [GraphReportSummary] counts for the current snapshot
  def summary
    snapshot.summary
  end

  Rendering::GraphRenderer::RENDERERS.each_key do |format|
    define_method("to_#{format}") do
      Rendering::GraphRenderer.render(snapshot, format)
    end

    define_method("export_as_#{format}") do |output_path|
      Rendering::GraphRenderer.export(snapshot, format, output_path)
    end
  end

  private

  def with_options(new_options)
    self.class.new(project_locator:, options: new_options, check_options:)
  end

  def immutable_project_locator(locator)
    return if locator.nil?

    locator = locator.to_path if locator.respond_to?(:to_path)
    return locator.dup.freeze if locator.is_a?(String) && !locator.empty?

    raise ArgumentError, 'project_locator must be a non-empty path or nil'
  end

  def immutable_check_options(value)
    return if value.nil?
    return value if value.is_a?(Common::FluentApi::CheckOptions)

    raise ArgumentError, 'check_options must be a CheckOptions value or nil'
  end
end

#export_as_mermaid(output_path) ⇒ nil

Returns writes Mermaid to output_path.

Returns:

  • (nil) —

    writes Mermaid to output_path



37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 37

class ProjectGraphBuilder
  attr_reader :project_locator, :options, :check_options

  def initialize(project_locator: nil, options: nil, check_options: nil)
    @project_locator = immutable_project_locator(project_locator)
    @options = Projection::GraphQueryOptions.resolve(options)
    @check_options = immutable_check_options(check_options)
    freeze
  end

  # Includes standard-library and gem imports in the report.
  # @return [ProjectGraphBuilder]
  def include_external_dependencies
    with_options(options.with(include_external_dependencies: true))
  end

  # Includes the self-edges that represent dependency-free source files.
  # @return [ProjectGraphBuilder]
  def include_self_dependencies
    with_options(options.with(include_self_dependencies: true))
  end

  # Keeps matching nodes and neighbors within depth hops.
  # @return [ProjectGraphBuilder]
  def focus_on(pattern, depth = 1, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(focus: filter, focus_depth: depth))
  end

  # Keeps nodes reachable from matching starting nodes.
  # @return [ProjectGraphBuilder]
  def reachable_from(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(reachable_from: filter))
  end

  # Keeps matching nodes and everything that depends on them.
  # @return [ProjectGraphBuilder]
  def dependents_of(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(dependents_of: filter))
  end

  # Groups file nodes by the requested project-relative folder depth.
  # @return [ProjectGraphBuilder]
  def collapse_to_folder_depth(depth)
    with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
  end

  # Groups node names through a regular-expression replacement.
  # @return [ProjectGraphBuilder]
  def collapse_by_pattern(pattern, replacement = '\\1')
    collapse = Projection::PatternCollapse.from(pattern, replacement)
    with_options(options.with(collapse:))
  end

  # Sets the report title.
  # @return [ProjectGraphBuilder]
  def titled(title)
    with_options(options.with(title:))
  end

  # Attaches extraction-related CheckOptions to this report.
  # @return [ProjectGraphBuilder]
  def with_check_options(value)
    self.class.new(project_locator:, options:, check_options: value)
  end

  # @return [GraphReportSnapshot] the immutable queried graph
  def snapshot
    graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
    Projection::SnapshotFactory.create(graph, options)
  end

  # @return [GraphReportSummary] counts for the current snapshot
  def summary
    snapshot.summary
  end

  Rendering::GraphRenderer::RENDERERS.each_key do |format|
    define_method("to_#{format}") do
      Rendering::GraphRenderer.render(snapshot, format)
    end

    define_method("export_as_#{format}") do |output_path|
      Rendering::GraphRenderer.export(snapshot, format, output_path)
    end
  end

  private

  def with_options(new_options)
    self.class.new(project_locator:, options: new_options, check_options:)
  end

  def immutable_project_locator(locator)
    return if locator.nil?

    locator = locator.to_path if locator.respond_to?(:to_path)
    return locator.dup.freeze if locator.is_a?(String) && !locator.empty?

    raise ArgumentError, 'project_locator must be a non-empty path or nil'
  end

  def immutable_check_options(value)
    return if value.nil?
    return value if value.is_a?(Common::FluentApi::CheckOptions)

    raise ArgumentError, 'check_options must be a CheckOptions value or nil'
  end
end

#focus_on(pattern, depth = 1, except: nil) ⇒ ProjectGraphBuilder

Keeps matching nodes and neighbors within depth hops.

Returns:



61
62
63
64
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 61

def focus_on(pattern, depth = 1, except: nil)
  filter = Common::RegexFactory.path_matcher(pattern, except:)
  with_options(options.with(focus: filter, focus_depth: depth))
end

#include_external_dependencies ⇒ ProjectGraphBuilder

Includes standard-library and gem imports in the report.

Returns:



49
50
51
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 49

def include_external_dependencies
  with_options(options.with(include_external_dependencies: true))
end

#include_self_dependencies ⇒ ProjectGraphBuilder

Includes the self-edges that represent dependency-free source files.

Returns:



55
56
57
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 55

def include_self_dependencies
  with_options(options.with(include_self_dependencies: true))
end

#reachable_from(pattern, except: nil) ⇒ ProjectGraphBuilder

Keeps nodes reachable from matching starting nodes.

Returns:



68
69
70
71
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 68

def reachable_from(pattern, except: nil)
  filter = Common::RegexFactory.path_matcher(pattern, except:)
  with_options(options.with(reachable_from: filter))
end

#snapshot ⇒ GraphReportSnapshot

Returns the immutable queried graph.

Returns:



106
107
108
109
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 106

def snapshot
  graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
  Projection::SnapshotFactory.create(graph, options)
end

#summary ⇒ GraphReportSummary

Returns counts for the current snapshot.

Returns:



112
113
114
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 112

def summary
  snapshot.summary
end

#titled(title) ⇒ ProjectGraphBuilder

Sets the report title.

Returns:



95
96
97
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 95

def titled(title)
  with_options(options.with(title:))
end

#to_csv ⇒ String

Returns the current graph report as CSV.

Returns:

  • (String) —

    the current graph report as CSV



37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 37

class ProjectGraphBuilder
  attr_reader :project_locator, :options, :check_options

  def initialize(project_locator: nil, options: nil, check_options: nil)
    @project_locator = immutable_project_locator(project_locator)
    @options = Projection::GraphQueryOptions.resolve(options)
    @check_options = immutable_check_options(check_options)
    freeze
  end

  # Includes standard-library and gem imports in the report.
  # @return [ProjectGraphBuilder]
  def include_external_dependencies
    with_options(options.with(include_external_dependencies: true))
  end

  # Includes the self-edges that represent dependency-free source files.
  # @return [ProjectGraphBuilder]
  def include_self_dependencies
    with_options(options.with(include_self_dependencies: true))
  end

  # Keeps matching nodes and neighbors within depth hops.
  # @return [ProjectGraphBuilder]
  def focus_on(pattern, depth = 1, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(focus: filter, focus_depth: depth))
  end

  # Keeps nodes reachable from matching starting nodes.
  # @return [ProjectGraphBuilder]
  def reachable_from(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(reachable_from: filter))
  end

  # Keeps matching nodes and everything that depends on them.
  # @return [ProjectGraphBuilder]
  def dependents_of(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(dependents_of: filter))
  end

  # Groups file nodes by the requested project-relative folder depth.
  # @return [ProjectGraphBuilder]
  def collapse_to_folder_depth(depth)
    with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
  end

  # Groups node names through a regular-expression replacement.
  # @return [ProjectGraphBuilder]
  def collapse_by_pattern(pattern, replacement = '\\1')
    collapse = Projection::PatternCollapse.from(pattern, replacement)
    with_options(options.with(collapse:))
  end

  # Sets the report title.
  # @return [ProjectGraphBuilder]
  def titled(title)
    with_options(options.with(title:))
  end

  # Attaches extraction-related CheckOptions to this report.
  # @return [ProjectGraphBuilder]
  def with_check_options(value)
    self.class.new(project_locator:, options:, check_options: value)
  end

  # @return [GraphReportSnapshot] the immutable queried graph
  def snapshot
    graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
    Projection::SnapshotFactory.create(graph, options)
  end

  # @return [GraphReportSummary] counts for the current snapshot
  def summary
    snapshot.summary
  end

  Rendering::GraphRenderer::RENDERERS.each_key do |format|
    define_method("to_#{format}") do
      Rendering::GraphRenderer.render(snapshot, format)
    end

    define_method("export_as_#{format}") do |output_path|
      Rendering::GraphRenderer.export(snapshot, format, output_path)
    end
  end

  private

  def with_options(new_options)
    self.class.new(project_locator:, options: new_options, check_options:)
  end

  def immutable_project_locator(locator)
    return if locator.nil?

    locator = locator.to_path if locator.respond_to?(:to_path)
    return locator.dup.freeze if locator.is_a?(String) && !locator.empty?

    raise ArgumentError, 'project_locator must be a non-empty path or nil'
  end

  def immutable_check_options(value)
    return if value.nil?
    return value if value.is_a?(Common::FluentApi::CheckOptions)

    raise ArgumentError, 'check_options must be a CheckOptions value or nil'
  end
end

#to_d2 ⇒ String

Returns the current graph report as D2.

Returns:

  • (String) —

    the current graph report as D2



37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 37

class ProjectGraphBuilder
  attr_reader :project_locator, :options, :check_options

  def initialize(project_locator: nil, options: nil, check_options: nil)
    @project_locator = immutable_project_locator(project_locator)
    @options = Projection::GraphQueryOptions.resolve(options)
    @check_options = immutable_check_options(check_options)
    freeze
  end

  # Includes standard-library and gem imports in the report.
  # @return [ProjectGraphBuilder]
  def include_external_dependencies
    with_options(options.with(include_external_dependencies: true))
  end

  # Includes the self-edges that represent dependency-free source files.
  # @return [ProjectGraphBuilder]
  def include_self_dependencies
    with_options(options.with(include_self_dependencies: true))
  end

  # Keeps matching nodes and neighbors within depth hops.
  # @return [ProjectGraphBuilder]
  def focus_on(pattern, depth = 1, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(focus: filter, focus_depth: depth))
  end

  # Keeps nodes reachable from matching starting nodes.
  # @return [ProjectGraphBuilder]
  def reachable_from(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(reachable_from: filter))
  end

  # Keeps matching nodes and everything that depends on them.
  # @return [ProjectGraphBuilder]
  def dependents_of(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(dependents_of: filter))
  end

  # Groups file nodes by the requested project-relative folder depth.
  # @return [ProjectGraphBuilder]
  def collapse_to_folder_depth(depth)
    with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
  end

  # Groups node names through a regular-expression replacement.
  # @return [ProjectGraphBuilder]
  def collapse_by_pattern(pattern, replacement = '\\1')
    collapse = Projection::PatternCollapse.from(pattern, replacement)
    with_options(options.with(collapse:))
  end

  # Sets the report title.
  # @return [ProjectGraphBuilder]
  def titled(title)
    with_options(options.with(title:))
  end

  # Attaches extraction-related CheckOptions to this report.
  # @return [ProjectGraphBuilder]
  def with_check_options(value)
    self.class.new(project_locator:, options:, check_options: value)
  end

  # @return [GraphReportSnapshot] the immutable queried graph
  def snapshot
    graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
    Projection::SnapshotFactory.create(graph, options)
  end

  # @return [GraphReportSummary] counts for the current snapshot
  def summary
    snapshot.summary
  end

  Rendering::GraphRenderer::RENDERERS.each_key do |format|
    define_method("to_#{format}") do
      Rendering::GraphRenderer.render(snapshot, format)
    end

    define_method("export_as_#{format}") do |output_path|
      Rendering::GraphRenderer.export(snapshot, format, output_path)
    end
  end

  private

  def with_options(new_options)
    self.class.new(project_locator:, options: new_options, check_options:)
  end

  def immutable_project_locator(locator)
    return if locator.nil?

    locator = locator.to_path if locator.respond_to?(:to_path)
    return locator.dup.freeze if locator.is_a?(String) && !locator.empty?

    raise ArgumentError, 'project_locator must be a non-empty path or nil'
  end

  def immutable_check_options(value)
    return if value.nil?
    return value if value.is_a?(Common::FluentApi::CheckOptions)

    raise ArgumentError, 'check_options must be a CheckOptions value or nil'
  end
end

#to_dot ⇒ String

Returns the current graph report as Graphviz DOT.

Returns:

  • (String) —

    the current graph report as Graphviz DOT



37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 37

class ProjectGraphBuilder
  attr_reader :project_locator, :options, :check_options

  def initialize(project_locator: nil, options: nil, check_options: nil)
    @project_locator = immutable_project_locator(project_locator)
    @options = Projection::GraphQueryOptions.resolve(options)
    @check_options = immutable_check_options(check_options)
    freeze
  end

  # Includes standard-library and gem imports in the report.
  # @return [ProjectGraphBuilder]
  def include_external_dependencies
    with_options(options.with(include_external_dependencies: true))
  end

  # Includes the self-edges that represent dependency-free source files.
  # @return [ProjectGraphBuilder]
  def include_self_dependencies
    with_options(options.with(include_self_dependencies: true))
  end

  # Keeps matching nodes and neighbors within depth hops.
  # @return [ProjectGraphBuilder]
  def focus_on(pattern, depth = 1, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(focus: filter, focus_depth: depth))
  end

  # Keeps nodes reachable from matching starting nodes.
  # @return [ProjectGraphBuilder]
  def reachable_from(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(reachable_from: filter))
  end

  # Keeps matching nodes and everything that depends on them.
  # @return [ProjectGraphBuilder]
  def dependents_of(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(dependents_of: filter))
  end

  # Groups file nodes by the requested project-relative folder depth.
  # @return [ProjectGraphBuilder]
  def collapse_to_folder_depth(depth)
    with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
  end

  # Groups node names through a regular-expression replacement.
  # @return [ProjectGraphBuilder]
  def collapse_by_pattern(pattern, replacement = '\\1')
    collapse = Projection::PatternCollapse.from(pattern, replacement)
    with_options(options.with(collapse:))
  end

  # Sets the report title.
  # @return [ProjectGraphBuilder]
  def titled(title)
    with_options(options.with(title:))
  end

  # Attaches extraction-related CheckOptions to this report.
  # @return [ProjectGraphBuilder]
  def with_check_options(value)
    self.class.new(project_locator:, options:, check_options: value)
  end

  # @return [GraphReportSnapshot] the immutable queried graph
  def snapshot
    graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
    Projection::SnapshotFactory.create(graph, options)
  end

  # @return [GraphReportSummary] counts for the current snapshot
  def summary
    snapshot.summary
  end

  Rendering::GraphRenderer::RENDERERS.each_key do |format|
    define_method("to_#{format}") do
      Rendering::GraphRenderer.render(snapshot, format)
    end

    define_method("export_as_#{format}") do |output_path|
      Rendering::GraphRenderer.export(snapshot, format, output_path)
    end
  end

  private

  def with_options(new_options)
    self.class.new(project_locator:, options: new_options, check_options:)
  end

  def immutable_project_locator(locator)
    return if locator.nil?

    locator = locator.to_path if locator.respond_to?(:to_path)
    return locator.dup.freeze if locator.is_a?(String) && !locator.empty?

    raise ArgumentError, 'project_locator must be a non-empty path or nil'
  end

  def immutable_check_options(value)
    return if value.nil?
    return value if value.is_a?(Common::FluentApi::CheckOptions)

    raise ArgumentError, 'check_options must be a CheckOptions value or nil'
  end
end

#to_html ⇒ String

Returns the current graph report as self-contained HTML.

Returns:

  • (String) —

    the current graph report as self-contained HTML



37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 37

class ProjectGraphBuilder
  attr_reader :project_locator, :options, :check_options

  def initialize(project_locator: nil, options: nil, check_options: nil)
    @project_locator = immutable_project_locator(project_locator)
    @options = Projection::GraphQueryOptions.resolve(options)
    @check_options = immutable_check_options(check_options)
    freeze
  end

  # Includes standard-library and gem imports in the report.
  # @return [ProjectGraphBuilder]
  def include_external_dependencies
    with_options(options.with(include_external_dependencies: true))
  end

  # Includes the self-edges that represent dependency-free source files.
  # @return [ProjectGraphBuilder]
  def include_self_dependencies
    with_options(options.with(include_self_dependencies: true))
  end

  # Keeps matching nodes and neighbors within depth hops.
  # @return [ProjectGraphBuilder]
  def focus_on(pattern, depth = 1, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(focus: filter, focus_depth: depth))
  end

  # Keeps nodes reachable from matching starting nodes.
  # @return [ProjectGraphBuilder]
  def reachable_from(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(reachable_from: filter))
  end

  # Keeps matching nodes and everything that depends on them.
  # @return [ProjectGraphBuilder]
  def dependents_of(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(dependents_of: filter))
  end

  # Groups file nodes by the requested project-relative folder depth.
  # @return [ProjectGraphBuilder]
  def collapse_to_folder_depth(depth)
    with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
  end

  # Groups node names through a regular-expression replacement.
  # @return [ProjectGraphBuilder]
  def collapse_by_pattern(pattern, replacement = '\\1')
    collapse = Projection::PatternCollapse.from(pattern, replacement)
    with_options(options.with(collapse:))
  end

  # Sets the report title.
  # @return [ProjectGraphBuilder]
  def titled(title)
    with_options(options.with(title:))
  end

  # Attaches extraction-related CheckOptions to this report.
  # @return [ProjectGraphBuilder]
  def with_check_options(value)
    self.class.new(project_locator:, options:, check_options: value)
  end

  # @return [GraphReportSnapshot] the immutable queried graph
  def snapshot
    graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
    Projection::SnapshotFactory.create(graph, options)
  end

  # @return [GraphReportSummary] counts for the current snapshot
  def summary
    snapshot.summary
  end

  Rendering::GraphRenderer::RENDERERS.each_key do |format|
    define_method("to_#{format}") do
      Rendering::GraphRenderer.render(snapshot, format)
    end

    define_method("export_as_#{format}") do |output_path|
      Rendering::GraphRenderer.export(snapshot, format, output_path)
    end
  end

  private

  def with_options(new_options)
    self.class.new(project_locator:, options: new_options, check_options:)
  end

  def immutable_project_locator(locator)
    return if locator.nil?

    locator = locator.to_path if locator.respond_to?(:to_path)
    return locator.dup.freeze if locator.is_a?(String) && !locator.empty?

    raise ArgumentError, 'project_locator must be a non-empty path or nil'
  end

  def immutable_check_options(value)
    return if value.nil?
    return value if value.is_a?(Common::FluentApi::CheckOptions)

    raise ArgumentError, 'check_options must be a CheckOptions value or nil'
  end
end

#to_json ⇒ String

Returns the current graph report as JSON.

Returns:

  • (String) —

    the current graph report as JSON



37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 37

class ProjectGraphBuilder
  attr_reader :project_locator, :options, :check_options

  def initialize(project_locator: nil, options: nil, check_options: nil)
    @project_locator = immutable_project_locator(project_locator)
    @options = Projection::GraphQueryOptions.resolve(options)
    @check_options = immutable_check_options(check_options)
    freeze
  end

  # Includes standard-library and gem imports in the report.
  # @return [ProjectGraphBuilder]
  def include_external_dependencies
    with_options(options.with(include_external_dependencies: true))
  end

  # Includes the self-edges that represent dependency-free source files.
  # @return [ProjectGraphBuilder]
  def include_self_dependencies
    with_options(options.with(include_self_dependencies: true))
  end

  # Keeps matching nodes and neighbors within depth hops.
  # @return [ProjectGraphBuilder]
  def focus_on(pattern, depth = 1, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(focus: filter, focus_depth: depth))
  end

  # Keeps nodes reachable from matching starting nodes.
  # @return [ProjectGraphBuilder]
  def reachable_from(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(reachable_from: filter))
  end

  # Keeps matching nodes and everything that depends on them.
  # @return [ProjectGraphBuilder]
  def dependents_of(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(dependents_of: filter))
  end

  # Groups file nodes by the requested project-relative folder depth.
  # @return [ProjectGraphBuilder]
  def collapse_to_folder_depth(depth)
    with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
  end

  # Groups node names through a regular-expression replacement.
  # @return [ProjectGraphBuilder]
  def collapse_by_pattern(pattern, replacement = '\\1')
    collapse = Projection::PatternCollapse.from(pattern, replacement)
    with_options(options.with(collapse:))
  end

  # Sets the report title.
  # @return [ProjectGraphBuilder]
  def titled(title)
    with_options(options.with(title:))
  end

  # Attaches extraction-related CheckOptions to this report.
  # @return [ProjectGraphBuilder]
  def with_check_options(value)
    self.class.new(project_locator:, options:, check_options: value)
  end

  # @return [GraphReportSnapshot] the immutable queried graph
  def snapshot
    graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
    Projection::SnapshotFactory.create(graph, options)
  end

  # @return [GraphReportSummary] counts for the current snapshot
  def summary
    snapshot.summary
  end

  Rendering::GraphRenderer::RENDERERS.each_key do |format|
    define_method("to_#{format}") do
      Rendering::GraphRenderer.render(snapshot, format)
    end

    define_method("export_as_#{format}") do |output_path|
      Rendering::GraphRenderer.export(snapshot, format, output_path)
    end
  end

  private

  def with_options(new_options)
    self.class.new(project_locator:, options: new_options, check_options:)
  end

  def immutable_project_locator(locator)
    return if locator.nil?

    locator = locator.to_path if locator.respond_to?(:to_path)
    return locator.dup.freeze if locator.is_a?(String) && !locator.empty?

    raise ArgumentError, 'project_locator must be a non-empty path or nil'
  end

  def immutable_check_options(value)
    return if value.nil?
    return value if value.is_a?(Common::FluentApi::CheckOptions)

    raise ArgumentError, 'check_options must be a CheckOptions value or nil'
  end
end

#to_mermaid ⇒ String

Returns the current graph report as Mermaid.

Returns:

  • (String) —

    the current graph report as Mermaid



37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 37

class ProjectGraphBuilder
  attr_reader :project_locator, :options, :check_options

  def initialize(project_locator: nil, options: nil, check_options: nil)
    @project_locator = immutable_project_locator(project_locator)
    @options = Projection::GraphQueryOptions.resolve(options)
    @check_options = immutable_check_options(check_options)
    freeze
  end

  # Includes standard-library and gem imports in the report.
  # @return [ProjectGraphBuilder]
  def include_external_dependencies
    with_options(options.with(include_external_dependencies: true))
  end

  # Includes the self-edges that represent dependency-free source files.
  # @return [ProjectGraphBuilder]
  def include_self_dependencies
    with_options(options.with(include_self_dependencies: true))
  end

  # Keeps matching nodes and neighbors within depth hops.
  # @return [ProjectGraphBuilder]
  def focus_on(pattern, depth = 1, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(focus: filter, focus_depth: depth))
  end

  # Keeps nodes reachable from matching starting nodes.
  # @return [ProjectGraphBuilder]
  def reachable_from(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(reachable_from: filter))
  end

  # Keeps matching nodes and everything that depends on them.
  # @return [ProjectGraphBuilder]
  def dependents_of(pattern, except: nil)
    filter = Common::RegexFactory.path_matcher(pattern, except:)
    with_options(options.with(dependents_of: filter))
  end

  # Groups file nodes by the requested project-relative folder depth.
  # @return [ProjectGraphBuilder]
  def collapse_to_folder_depth(depth)
    with_options(options.with(collapse: Projection::FolderDepthCollapse.new(depth:)))
  end

  # Groups node names through a regular-expression replacement.
  # @return [ProjectGraphBuilder]
  def collapse_by_pattern(pattern, replacement = '\\1')
    collapse = Projection::PatternCollapse.from(pattern, replacement)
    with_options(options.with(collapse:))
  end

  # Sets the report title.
  # @return [ProjectGraphBuilder]
  def titled(title)
    with_options(options.with(title:))
  end

  # Attaches extraction-related CheckOptions to this report.
  # @return [ProjectGraphBuilder]
  def with_check_options(value)
    self.class.new(project_locator:, options:, check_options: value)
  end

  # @return [GraphReportSnapshot] the immutable queried graph
  def snapshot
    graph = ArchUnit::Extraction.extract_graph(project_locator, options: check_options)
    Projection::SnapshotFactory.create(graph, options)
  end

  # @return [GraphReportSummary] counts for the current snapshot
  def summary
    snapshot.summary
  end

  Rendering::GraphRenderer::RENDERERS.each_key do |format|
    define_method("to_#{format}") do
      Rendering::GraphRenderer.render(snapshot, format)
    end

    define_method("export_as_#{format}") do |output_path|
      Rendering::GraphRenderer.export(snapshot, format, output_path)
    end
  end

  private

  def with_options(new_options)
    self.class.new(project_locator:, options: new_options, check_options:)
  end

  def immutable_project_locator(locator)
    return if locator.nil?

    locator = locator.to_path if locator.respond_to?(:to_path)
    return locator.dup.freeze if locator.is_a?(String) && !locator.empty?

    raise ArgumentError, 'project_locator must be a non-empty path or nil'
  end

  def immutable_check_options(value)
    return if value.nil?
    return value if value.is_a?(Common::FluentApi::CheckOptions)

    raise ArgumentError, 'check_options must be a CheckOptions value or nil'
  end
end

#with_check_options(value) ⇒ ProjectGraphBuilder

Attaches extraction-related CheckOptions to this report.

Returns:



101
102
103
# File 'lib/archunit/graph/fluentapi/project_graph_builder.rb', line 101

def with_check_options(value)
  self.class.new(project_locator:, options:, check_options: value)
end