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.



803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'lib/rigor/module_graph/cli.rb', line 803

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



889
890
891
892
893
# File 'lib/rigor/module_graph/cli.rb', line 889

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



857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
# File 'lib/rigor/module_graph/cli.rb', line 857

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



895
896
897
898
899
# File 'lib/rigor/module_graph/cli.rb', line 895

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

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

#run(argv) ⇒ Object



817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
# File 'lib/rigor/module_graph/cli.rb', line 817

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