Class: Rigor::ModuleGraph::CLI::ClassDiagramCmd

Inherits:
Object
  • Object
show all
Includes:
EdgeFilters
Defined in:
lib/rigor/module_graph/cli.rb

Overview

‘class-diagram` renders a Mermaid classDiagram document from the edges.jsonl (the dependency graph) and the nodes.jsonl (class declarations + methods + attributes). Phase 5 of the project — turns the dependency graph material into a UML-style class diagram.

Constant Summary collapse

DEFAULT_NODES_PATH =
CLI::DEFAULT_NODES_PATH

Constants included from EdgeFilters

EdgeFilters::VALID_CONFIDENCES, EdgeFilters::VALID_DIRECTIONS, EdgeFilters::VALID_EDGE_SCOPES, EdgeFilters::VALID_KINDS

Instance Method Summary collapse

Methods included from EdgeFilters

#add_filter_options, #apply_filters, #validate!

Constructor Details

#initialize(stdout:, stderr:, stdin:) ⇒ ClassDiagramCmd

Returns a new instance of ClassDiagramCmd.



750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/rigor/module_graph/cli.rb', line 750

def initialize(stdout:, stderr:, stdin:)
  @stdout = stdout
  @stderr = stderr
  @stdin = stdin
  @options = {
    kinds: nil, confidences: nil,
    from: nil, depth: nil, direction: :both, edge_scope: :cluster,
    nodes_path: nil,
    include_methods: true,
    include_attributes: true,
    visibilities: %w[public protected private]
  }
end

Instance Method Details

#default_nodes_for(edges_path) ⇒ Object



836
837
838
839
840
# File 'lib/rigor/module_graph/cli.rb', line 836

def default_nodes_for(edges_path)
  return DEFAULT_NODES_PATH unless edges_path

  File.join(File.dirname(edges_path), "nodes.jsonl")
end

#parse_options!(argv) ⇒ Object



804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# File 'lib/rigor/module_graph/cli.rb', line 804

def parse_options!(argv)
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: rigor-module-graph class-diagram [options] [EDGES_FILE]"
    opts.on("--nodes PATH",
            "Path to the nodes JSONL (default: sibling of EDGES_FILE)") do |path|
      @options[:nodes_path] = path
    end
    opts.on("--no-methods",
            "Don't render methods inside class bodies") do
      @options[:include_methods] = false
    end
    opts.on("--no-attributes",
            "Don't render attributes inside class bodies") do
      @options[:include_attributes] = false
    end
    opts.on("--public-only",
            "Only show public members") do
      @options[:visibilities] = %w[public]
    end
    opts.on("--no-private",
            "Hide private members") do
      @options[:visibilities] = %w[public protected]
    end
    add_filter_options(opts, @options)
    opts.on("-h", "--help") do
      @stdout.puts opts
      exit 0
    end
  end
  parser.parse!(argv)
end

#read_nodes(path) ⇒ Object



842
843
844
845
846
# File 'lib/rigor/module_graph/cli.rb', line 842

def read_nodes(path)
  return [] unless path && File.exist?(path)

  File.open(path, "r") { |io| NodeIO.read(io) }
end

#run(argv) ⇒ Object



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
# File 'lib/rigor/module_graph/cli.rb', line 764

def run(argv)
  argv = argv.dup
  parse_options!(argv)
  edges_path = argv.shift
  io = edges_path ? File.open(edges_path, "r") : @stdin
  begin
    edges = EdgeIO.read(io)
  ensure
    io.close if edges_path && !io.closed?
  end

  edges = apply_filters(
    edges,
    kinds: @options[:kinds],
    confidences: @options[:confidences],
    from: @options[:from],
    depth: @options[:depth],
    direction: @options[:direction],
    edge_scope: @options[:edge_scope]
  )

  nodes_path = @options[:nodes_path] || default_nodes_for(edges_path)
  nodes = read_nodes(nodes_path)

  out = Uml::ClassDiagram.render(
    edges, nodes,
    include_methods: @options[:include_methods],
    include_attributes: @options[:include_attributes],
    visibilities: @options[:visibilities]
  )
  @stdout.print(out)
  0
rescue OptionParser::ParseError => e
  @stderr.puts "rigor-module-graph class-diagram: #{e.message}"
  2
rescue Errno::ENOENT => e
  @stderr.puts "rigor-module-graph class-diagram: #{e.message}"
  1
end