|
3 | 3 | # Released under the MIT License. |
4 | 4 | # Copyright, 2017-2026, by Samuel Williams. |
5 | 5 |
|
| 6 | +require "samovar" |
6 | 7 | require_relative "selection" |
7 | | -require "graphviz" |
| 8 | +require "build/controller" |
8 | 9 |
|
9 | 10 | module Teapot |
10 | 11 | module Command |
11 | | - # A command to visualize the dependency graph. |
12 | | - class Visualize < Selection |
13 | | - self.description = "Generate a picture of the dependency graph." |
| 12 | + # A command to visualize dependency graphs. |
| 13 | + class Visualize < Samovar::Command |
| 14 | + self.description = "Generate visualizations of package and target dependencies." |
14 | 15 |
|
15 | | - options do |
16 | | - option "-o/--output-path <path>", "The output path for the visualization.", default: "dependency.svg" |
17 | | - option "-d/--dependency-name <name>", "Show the partial chain for the given named dependency." |
18 | | - end |
19 | | - |
20 | | - # Get the dependency names to visualize. |
21 | | - # @returns [Array(String)] The dependency names. |
22 | | - def dependency_names |
23 | | - @targets || [] |
24 | | - end |
25 | | - |
26 | | - # Get the specific dependency name to visualize. |
27 | | - # @returns [String | Nil] The dependency name. |
28 | | - def dependency_name |
29 | | - @options[:dependency_name] |
| 16 | + # Visualize package-level dependencies. |
| 17 | + class Packages < Selection |
| 18 | + self.description = "Visualize package-level dependencies from configuration.require." |
| 19 | + |
| 20 | + options do |
| 21 | + option "-o/--output-path <path>", "The output path for the visualization." |
| 22 | + option "-d/--dependency-name <name>", "Show the partial chain for the given named dependency." |
| 23 | + end |
| 24 | + |
| 25 | + # Get the specific dependency name to visualize. |
| 26 | + # @returns [String | Nil] The dependency name. |
| 27 | + def dependency_name |
| 28 | + @options[:dependency_name] |
| 29 | + end |
| 30 | + |
| 31 | + # Process and generate the package dependency visualization. |
| 32 | + # @parameter selection [Select] The selection to visualize. |
| 33 | + # @returns [String] The generated Mermaid diagram. |
| 34 | + def process(selection) |
| 35 | + chain = selection.chain |
| 36 | + |
| 37 | + if dependency_name |
| 38 | + provider = selection.dependencies[dependency_name] |
| 39 | + chain = chain.partial(provider) |
| 40 | + end |
| 41 | + |
| 42 | + visualization = ::Build::Dependency::Visualization.new |
| 43 | + diagram = visualization.generate(chain) |
| 44 | + |
| 45 | + if output_path = @options[:output_path] |
| 46 | + File.write(output_path, diagram) |
| 47 | + else |
| 48 | + $stdout.puts diagram |
| 49 | + end |
| 50 | + |
| 51 | + return diagram |
| 52 | + end |
30 | 53 | end |
31 | 54 |
|
32 | | - # Process and generate the visualization. |
33 | | - # @parameter selection [Select] The selection to visualize. |
34 | | - # @returns [Graphviz::Graph] The generated graph. |
35 | | - def process(selection) |
36 | | - context = selection.context |
37 | | - chain = selection.chain |
| 55 | + # Visualize target-level dependencies. |
| 56 | + class Targets < Selection |
| 57 | + self.description = "Visualize target-level dependencies from target.depends." |
38 | 58 |
|
39 | | - if dependency_name |
40 | | - provider = selection.dependencies[dependency_name] |
41 | | - |
42 | | - chain = chain.partial(provider) |
| 59 | + options do |
| 60 | + option "-o/--output-path <path>", "The output path for the visualization." |
43 | 61 | end |
44 | 62 |
|
45 | | - visualization = ::Build::Dependency::Visualization.new |
| 63 | + # Process and generate the target dependency visualization. |
| 64 | + # @parameter selection [Select] The selection to visualize. |
| 65 | + # @returns [String] The generated Mermaid diagram. |
| 66 | + def process(selection) |
| 67 | + lines = ["flowchart LR"] |
| 68 | + lines << "" |
| 69 | + |
| 70 | + # Build the graph from all targets in the selection |
| 71 | + # The selection contains all targets loaded from packages |
| 72 | + selection.targets.each do |name, target| |
| 73 | + target.dependencies.each do |dependency| |
| 74 | + dependency_name = dependency.name.to_s |
| 75 | + |
| 76 | + # Create edge from target to its dependency |
| 77 | + if dependency.private? |
| 78 | + lines << " #{sanitize_id(name)}[#{name}] -.-> #{sanitize_id(dependency_name)}[#{dependency_name}]" |
| 79 | + else |
| 80 | + lines << " #{sanitize_id(name)}[#{name}] --> #{sanitize_id(dependency_name)}[#{dependency_name}]" |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + diagram = lines.join("\n") |
| 86 | + |
| 87 | + if output_path = @options[:output_path] |
| 88 | + File.write(output_path, diagram) |
| 89 | + else |
| 90 | + $stdout.puts diagram |
| 91 | + end |
| 92 | + |
| 93 | + return diagram |
| 94 | + end |
46 | 95 |
|
47 | | - graph = visualization.generate(chain) |
| 96 | + private |
48 | 97 |
|
49 | | - if output_path = @options[:output_path] |
50 | | - Graphviz.output(graph, path: output_path, format: :svg) |
51 | | - else |
52 | | - $stdout.puts graph.to_dot |
| 98 | + # Convert a name to a valid Mermaid node ID. |
| 99 | + # @parameter name [String] The name to sanitize. |
| 100 | + # @returns [String] A sanitized identifier safe for use in Mermaid diagrams. |
| 101 | + def sanitize_id(name) |
| 102 | + name.to_s.gsub(/[^a-zA-Z0-9_]/, "_") |
53 | 103 | end |
54 | | - |
55 | | - return graph |
| 104 | + end |
| 105 | + |
| 106 | + nested :command, { |
| 107 | + "packages" => Packages, |
| 108 | + "targets" => Targets, |
| 109 | + }, default: "packages" |
| 110 | + |
| 111 | + # Delegate context to parent Top command. |
| 112 | + # @returns [Context] The project context. |
| 113 | + def context |
| 114 | + parent.context |
| 115 | + end |
| 116 | + |
| 117 | + # Execute the visualize command. |
| 118 | + def call |
| 119 | + @command.call |
56 | 120 | end |
57 | 121 | end |
58 | 122 | end |
|
0 commit comments