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.



847
848
849
850
851
852
853
854
855
856
857
858
859
# File 'lib/rigor/module_graph/cli.rb', line 847

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



933
934
935
936
937
# File 'lib/rigor/module_graph/cli.rb', line 933

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



901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
# File 'lib/rigor/module_graph/cli.rb', line 901

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



939
940
941
942
943
# File 'lib/rigor/module_graph/cli.rb', line 939

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

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

#run(argv) ⇒ Object



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
888
889
890
891
892
893
894
895
896
897
898
899
# File 'lib/rigor/module_graph/cli.rb', line 861

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