Class: Codescout::RepoAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/codescout/repo_analyzer.rb

Constant Summary collapse

ALLOWED_FILES =
%w(.rb .rake .gemspec)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ RepoAnalyzer



7
8
9
10
11
12
13
# File 'lib/codescout/repo_analyzer.rb', line 7

def initialize(path)
  @base_path = File.expand_path(path)
  @files = []
  @codescout = {
    version: Codescout::VERSION
  }
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



5
6
7
# File 'lib/codescout/repo_analyzer.rb', line 5

def base_path
  @base_path
end

#filesObject (readonly)

Returns the value of attribute files.



5
6
7
# File 'lib/codescout/repo_analyzer.rb', line 5

def files
  @files
end

Instance Method Details

#analyzeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/codescout/repo_analyzer.rb', line 15

def analyze
  within_base do
    scan_files
    run_filestats
    run_flog
    run_flay
    run_churn
    run_brakeman
    run_rubocop
    run_commitstats
    cleanup
  end
end

#cleanupObject



93
94
95
96
97
98
99
# File 'lib/codescout/repo_analyzer.rb', line 93

def cleanup
  files = %w(./rubocop.yml ./rubocop.json ./tmp/churn ./brakeman.json)

  files.each do |path|
    FileUtils.rm_rf(path)
  end
end

#resultObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/codescout/repo_analyzer.rb', line 29

def result
  {
    codescout:  @codescout,
    file_stats: @file_stats,
    flog:       @flog,
    flay:       @flay,
    churn:      @churn,
    brakeman:   @brakeman,
    rubocop:    @rubocop,
    commit:     @commit
  }
end

#run_brakemanObject



78
79
80
81
# File 'lib/codescout/repo_analyzer.rb', line 78

def run_brakeman
  STDERR.puts "Running brakeman"
  @brakeman = Codescout::BrakemanStats.new(self).results
end

#run_churnObject



73
74
75
76
# File 'lib/codescout/repo_analyzer.rb', line 73

def run_churn
  STDERR.puts "Running churn"
  @churn = Codescout::ChurnStats.new(self).files
end

#run_commitstatsObject



88
89
90
91
# File 'lib/codescout/repo_analyzer.rb', line 88

def run_commitstats
  STDERR.puts "Runnig commit stats"
  @commit = Codescout::CommitStats.new(self).to_hash
end

#run_filestatsObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/codescout/repo_analyzer.rb', line 60

def run_filestats
  STDERR.puts "Running filestats"
  @file_stats = {}

  @files.each do |f|
    @file_stats[f] = Codescout::FileStats.new(@base_path, f).to_hash
  end

  if File.exists?("Gemfile")
    @file_stats["Gemfile"] = Codescout::FileStats.new(@base_path, "Gemfile").to_hash
  end
end

#run_flayObject



50
51
52
53
# File 'lib/codescout/repo_analyzer.rb', line 50

def run_flay
  STDERR.puts "Running flay"
  @flay = Codescout::FlayStats.new(self).matches
end

#run_flogObject



55
56
57
58
# File 'lib/codescout/repo_analyzer.rb', line 55

def run_flog
  STDERR.puts "Running flog"
  @flog = Codescout::FlogStats.new(self).scores
end

#run_rubocopObject



83
84
85
86
# File 'lib/codescout/repo_analyzer.rb', line 83

def run_rubocop
  STDERR.puts "Running rubocop"
  @rubocop = Codescout::RubocopStats.new(self).results
end

#scan_filesObject



46
47
48
# File 'lib/codescout/repo_analyzer.rb', line 46

def scan_files
  Dir["**/*"].each { |file| @files << file if valid_file?(file) }
end

#valid_file?(file) ⇒ Boolean



101
102
103
104
105
# File 'lib/codescout/repo_analyzer.rb', line 101

def valid_file?(file)
  return false unless ALLOWED_FILES.include?(File.extname(file))
  return false if file =~ /^db|spec|features|test|examples|samples\//
  true
end

#within_base(&blk) ⇒ Object



42
43
44
# File 'lib/codescout/repo_analyzer.rb', line 42

def within_base(&blk)
  Dir.chdir(@base_path) { blk.call }
end