Class: Rigor::ModuleGraph::CLI::Cycles

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

Constant Summary

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:) ⇒ Cycles

Returns a new instance of Cycles.



1015
1016
1017
1018
1019
1020
1021
1022
1023
# File 'lib/rigor/module_graph/cli.rb', line 1015

def initialize(stdout:, stderr:, stdin:)
  @stdout = stdout
  @stderr = stderr
  @stdin = stdin
  @state = {
    kinds: nil, confidences: nil,
    from: nil, depth: nil, direction: :both
  }
end

Instance Method Details

#parse_options!(argv) ⇒ Object



1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'lib/rigor/module_graph/cli.rb', line 1057

def parse_options!(argv)
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: rigor-module-graph cycles [options] [FILE]"
    # `--only` kept as an alias for `--kind` for backward
    # compat with the Phase 1 flag.
    opts.on("--only KINDS", Array,
            "Alias for --kind") do |kinds|
      @state[:kinds] = kinds
    end
    add_filter_options(opts, @state)
    opts.on("-h", "--help") do
      @stdout.puts opts
      exit 0
    end
  end
  parser.parse!(argv)
end

#run(argv) ⇒ Object



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
# File 'lib/rigor/module_graph/cli.rb', line 1025

def run(argv)
  argv = argv.dup
  parse_options!(argv)
  path, = argv
  io = path ? File.open(path, "r") : @stdin
  begin
    edges = EdgeIO.read(io)
  ensure
    io.close if path && !io.closed?
  end
  edges = apply_filters(
    edges,
    kinds: @state[:kinds],
    confidences: @state[:confidences],
    from: @state[:from],
    depth: @state[:depth],
    direction: @state[:direction],
    edge_scope: @state[:edge_scope]
  )
  cycles = CycleDetector.detect(edges)
  if cycles.empty?
    @stderr.puts "rigor-module-graph cycles: no cycles found"
    0
  else
    cycles.each { |c| @stdout.puts c.to_s }
    1
  end
rescue OptionParser::ParseError => e
  @stderr.puts "rigor-module-graph cycles: #{e.message}"
  2
end