Class: StudFinder::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/stud_finder/cli.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: Analysis, Report, ValidationError

Constant Summary collapse

OUTPUT_FORMATS =
%w[table json markdown csv].freeze
RESULT_COLUMNS =
%w[
  rank language file score class fan_in fan_in_pct fan_out fan_out_pct instability instability_pct complexity
  complexity_pct churn_commits churn_lines churn_pct max_coupling max_coupling_partner coupling_partners
  coupling_pct coverage
].freeze
MARKDOWN_COLUMNS =
%w[
  rank language file score class fan_in fan_out fan_out_pct instability complexity churn_commits churn_lines
  churn_pct max_coupling max_coupling_partner coupling_partners coupling_pct coverage
].freeze
WEIGHT_KEYS =
%i[fan_in fan_out complexity churn coverage].freeze
DEFAULT_OPTIONS =
{
  output: 'table',
  churn_days: 180,
  weights: { fan_in: 0.25, fan_out: 0.10, complexity: 0.25, churn: 0.25, coverage: 0.15 },
  custom_weights: false,
  trunk_threshold: 85,
  branch_threshold: 50,
  excludes: [],
  min_files: 20,
  top: nil,
  verbose: false,
  ruby_coverage_path: nil,
  js_coverage_path: nil,
  js_timeout: 60,
  diff_base: nil,
  only_paths: nil,
  filter_set: nil,
  coupling_threshold: 0.30,
  coupling_min_commits: 5,
  cli_warnings: []
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdout: $stdout, stderr: $stderr) ⇒ CLI

Returns a new instance of CLI.



65
66
67
68
69
70
# File 'lib/stud_finder/cli.rb', line 65

def initialize(argv, stdout: $stdout, stderr: $stderr)
  @argv = argv.dup
  @stdout = stdout
  @stderr = stderr
  @options = Marshal.load(Marshal.dump(DEFAULT_OPTIONS))
end

Class Method Details

.start(argv = ARGV, stdout: $stdout, stderr: $stderr) ⇒ Object



72
73
74
# File 'lib/stud_finder/cli.rb', line 72

def self.start(argv = ARGV, stdout: $stdout, stderr: $stderr)
  new(argv, stdout: stdout, stderr: stderr).run
end

Instance Method Details

#runObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/stud_finder/cli.rb', line 76

def run
  parser = option_parser

  if @argv[0] == 'edges'
    @argv.shift
    parser.parse!(@argv)
    target = @argv.shift
    path = @argv.shift || '.'
    raise ValidationError, "Error: unexpected arguments: #{@argv.join(' ')}" unless @argv.empty?

    @repo_path = File.expand_path(path)
    validate_options!
    return run_edges(target, path)
  end

  parser.parse!(@argv)
  path = @argv.shift || '.'
  raise ValidationError, "Error: unexpected arguments: #{@argv.join(' ')}" unless @argv.empty?

  @repo_path = File.expand_path(path)
  validate_options!

  result = FileCollector.new(
    path: path,
    excludes: @options[:excludes],
    min_files: @options[:min_files],
    stderr: @stderr
  ).collect
  progress("collecting files... #{result.files.length} found")

  @options[:filter_set] = resolve_filter_set(@repo_path)

  coupling = compute_coupling(@repo_path, result.files)
  analysis = analyze(@repo_path, result.files, result.languages, coupling)
  analysis = warn_if_no_scored_files(analysis)
  emit_results(@repo_path, result, analysis)
  0
rescue OptionParser::InvalidOption, OptionParser::MissingArgument, OptionParser::InvalidArgument, ValidationError,
       FileCollector::Error, Churn::Error, Complexity::Error, Coverage::Cobertura::Error, Coverage::Detector::Error,
       Coverage::Lcov::Error, Coverage::Resultset::Error, Diff::Error, Scorer::ValidationError => e
  @stderr.puts e.message
  1
end

#run_edges(target, path) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/stud_finder/cli.rb', line 120

def run_edges(target, path)
  @repo_path = File.expand_path(path)
  result = FileCollector.new(path: path, excludes: @options[:excludes],
                             min_files: @options[:min_files], stderr: @stderr).collect
  progress("collecting files... #{result.files.length} found")
  analysis = analyze(@repo_path, result.files, result.languages)
  all_rows = analysis.ruby.rows + analysis.javascript.rows
  all_edges = analysis.ruby.edges.merge(analysis.javascript.edges)

  progress("computing temporal coupling (git log, #{@options[:churn_days]} days)...")
  coupling_result = TemporalCoupling.new(
    repo_path: @repo_path,
    files: result.files,
    days: @options[:churn_days],
    min_co_changes: @options[:coupling_min_commits],
    coupling_threshold: @options[:coupling_threshold]
  ).call

  Edges.new(
    target: target, rows: all_rows, edges: all_edges,
    coupling: coupling_result.pairs,
    churn_days: @options[:churn_days],
    coupling_min_commits: @options[:coupling_min_commits],
    coupling_threshold: @options[:coupling_threshold],
    stdout: @stdout, stderr: @stderr
  ).call
rescue FileCollector::Error, Churn::Error, Complexity::Error, Scorer::ValidationError => e
  @stderr.puts e.message
  1
end