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
CLASS_RANK =
{ 'trunk' => 3, 'branch' => 2, 'leaf' => 1 }.freeze
RESULT_COLUMNS =
%w[
  rank language file score evidence class new_file age_days escalation fan_in fan_in_pct fan_out fan_out_pct
  instability instability_pct complexity complexity_pct churn_commits churn_lines churn_pct loc loc_pct max_coupling
  max_coupling_partner coupling_partners coupling_pct coverage
].freeze
MARKDOWN_COLUMNS =
%w[
  rank language file score evidence class new_file age_days escalation 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
OPTIONAL_WEIGHT_KEYS =
%i[interaction coupling].freeze
DEFAULT_OPTIONS =
{
  output: 'table',
  churn_days: 180,
  weights: StudFinder::Scorer::DEFAULT_WEIGHTS,
  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,
  rails_inference: true,
  diff_base: nil,
  only_paths: nil,
  filter_set: nil,
  coupling_threshold: 0.30,
  coupling_min_commits: 5,
  coupling_max_commit_files: 50,
  newness: true,
  new_file_days: StudFinder::Newness::DEFAULT_DAYS,
  new_file_min_commits: StudFinder::Newness::DEFAULT_MIN_COMMITS,
  auto_unshallow: true,
  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.



77
78
79
80
81
82
# File 'lib/stud_finder/cli.rb', line 77

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



84
85
86
# File 'lib/stud_finder/cli.rb', line 84

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

Instance Method Details

#runObject



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
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/stud_finder/cli.rb', line 88

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, Newness::Error, Scorer::ValidationError => e
  @stderr.puts e.message
  1
end

#run_edges(target, path) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/stud_finder/cli.rb', line 132

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],
    max_commit_files: @options[:coupling_max_commit_files]
  ).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, Newness::Error, Scorer::ValidationError => e
  @stderr.puts e.message
  1
end