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.



1059
1060
1061
1062
1063
1064
1065
1066
1067
# File 'lib/rigor/module_graph/cli.rb', line 1059

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



1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
# File 'lib/rigor/module_graph/cli.rb', line 1101

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



1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
# File 'lib/rigor/module_graph/cli.rb', line 1069

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