Class: RailsCodeAuditor::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_code_auditor/analyzer.rb

Constant Summary collapse

REPORT_FOLDER =
"report"

Class Method Summary collapse

Class Method Details

.ensure_report_folderObject



29
30
31
# File 'lib/rails_code_auditor/analyzer.rb', line 29

def self.ensure_report_folder
  FileUtils.mkdir_p(REPORT_FOLDER)
end

.generate_brakeman_htmlObject



44
45
46
47
# File 'lib/rails_code_auditor/analyzer.rb', line 44

def self.generate_brakeman_html
  run_cmd("brakeman -o #{REPORT_FOLDER}/brakeman.html", raw: true)
  "#{REPORT_FOLDER}/brakeman.html"
end

.generate_rails_best_practices_htmlObject



49
50
51
52
# File 'lib/rails_code_auditor/analyzer.rb', line 49

def self.generate_rails_best_practices_html
  run_cmd("rails_best_practices -f html --output-file #{REPORT_FOLDER}/rails_best_practices.html", raw: true)
  "#{REPORT_FOLDER}/rails_best_practices.html"
end

.generate_reek_htmlObject



59
60
61
62
# File 'lib/rails_code_auditor/analyzer.rb', line 59

def self.generate_reek_html
  run_cmd("reek --format html > report/reek.html", raw: true)
  "#{REPORT_FOLDER}/reek.html"
end

.generate_rubycritic_htmlObject



54
55
56
57
# File 'lib/rails_code_auditor/analyzer.rb', line 54

def self.generate_rubycritic_html
  run_cmd("rubycritic --no-browser --path #{REPORT_FOLDER}/rubycritic", raw: true)
  "#{REPORT_FOLDER}/rubycritic/overview.html"
end

.rails_versionObject



11
12
13
# File 'lib/rails_code_auditor/analyzer.rb', line 11

def self.rails_version
  defined?(Rails) ? Gem::Version.new(Rails.version) : nil
end

.ruby_versionObject



7
8
9
# File 'lib/rails_code_auditor/analyzer.rb', line 7

def self.ruby_version
  Gem::Version.new(RUBY_VERSION)
end

.run_allObject



64
65
66
67
68
69
70
71
72
73
74
75
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
119
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
# File 'lib/rails_code_auditor/analyzer.rb', line 64

def self.run_all
  ensure_report_folder

  results = {}

  results[:brakeman] = {
    json: run_cmd("brakeman -f json --no-exit-on-error"),
    html_path: run_cmd("brakeman -o #{REPORT_FOLDER}/brakeman.html", raw: true)
  }

  results[:bundler_audit] = {
    json: run_cmd("bundle audit check --verbose"),
    html_path: write_html_report("bundler_audit", run_cmd("bundle audit check --verbose"))
  }

  results[:rubocop] = if ruby_version >= Gem::Version.new("2.7")
                        {
                          json: run_cmd("rubocop --format json"),
                          html_path: write_html_report("rubocop", run_cmd("rubocop --format simple"))
                        }
                      else
                        { skipped: true, reason: "Rubocop requires Ruby >= 2.7" }
                      end

  results[:rails_best_practices] = {
    json: run_cmd("rails_best_practices --format json"),
    html_path: run_cmd("rails_best_practices -f html --output-file #{REPORT_FOLDER}/rails_best_practices.html",
                       raw: true)
  }

  results[:flay] = {
    text: run_cmd("flay --mass 50 ."),
    html_path: write_html_report("flay", run_cmd("flay --mass 50 ."))
  }

  results[:flog] = {
    text: run_cmd("flog ."),
    html_path: write_html_report("flog", run_cmd("flog ."))
  }

  results[:license_finder] = if ruby_version >= Gem::Version.new("2.7")
                               {
                                 json: run_cmd("license_finder --format json"),
                                 html_path: write_html_report("license_finder",
                                                              run_cmd("license_finder --format text"))
                               }
                             else
                               { skipped: true, reason: "LicenseFinder requires Ruby >= 2.7" }
                             end

  results[:reek] = {
    json: run_cmd("reek --format json"),
    html_path: run_cmd("reek --format html > #{REPORT_FOLDER}/reek.html", raw: true)
  }

  begin
    Timeout.timeout(300) do
      results[:rubycritic] = if ruby_version >= Gem::Version.new("2.7")
                               {
                                 json: run_cmd("rubycritic --format json"),
                                 html_path: run_cmd("rubycritic --no-browser --path #{REPORT_FOLDER}/rubycritic",
                                                    raw: true)
                               }
                             else
                               { skipped: true, reason: "RubyCritic requires Ruby >= 2.7" }
                             end
    end
  rescue Timeout::Error
    results[:rubycritic] = { error: "RubyCritic timed out after 5 minutes" }
  end

  results[:fasterer] = {
    text: run_cmd("fasterer ."),
    html_path: write_html_report("fasterer", run_cmd("fasterer ."))
  }

  # Optional: tools only if Rails >= 5
  if rails_version && rails_version >= Gem::Version.new("5.0")
    results[:grover] = {
      html_path: write_html_report("grover", "Grover logic here (if you use it)")
    }
  end

  results
end

.run_cmd(command, raw: false) ⇒ Object



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

def self.run_cmd(command, raw: false)
  puts "Running: #{command}"
  output = `#{command}`
  if output.empty?
    nil
  else
    begin
      raw ? output : JSON.parse(output)
    rescue StandardError
      output
    end
  end
end

.write_html_report(tool_name, content) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/rails_code_auditor/analyzer.rb', line 33

def self.write_html_report(tool_name, content)
  path = File.join(REPORT_FOLDER, "#{tool_name}.html")
  File.open(path, "w") do |f|
    f.puts "<html><head><title>#{tool_name.capitalize} Report</title></head><body><pre>"
    f.puts "<h1>#{tool_name.capitalize} Report</h1>"
    f.puts content
    f.puts "</pre></body></html>"
  end
  path
end