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.



962
963
964
965
966
967
968
969
970
# File 'lib/rigor/module_graph/cli.rb', line 962

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



1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/rigor/module_graph/cli.rb', line 1004

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



972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'lib/rigor/module_graph/cli.rb', line 972

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