Over a million developers have joined DZone.

Code Reports Rake Task

DZone's Guide to

Code Reports Rake Task

Free Resource
Will run various Ruby Gems for creating code quality reports, store them in public/ in your Rails app, and create public/reports.html with links to each report.  You need Saikuro, Flog, Flay, Reek, and Roodi, since this Rake task uses them all.

Save this Rake task in your lib/tasks/ directory in your Rails app, and give it the extension .rake so that you can then run 'rake metrics:make_all'.

namespace :metrics do
  def get_mtime(path)
    File.mtime(path).strftime("%Y-%m-%d %I:%M %p")
  desc 'Generates Reek report'
  task :make_reek do
    require 'find'
    require 'fileutils'
    reek_dir_name = "reek"
    reek_dir = "#{RAILS_ROOT}/public/#{reek_dir_name}"
    index_file = "#{reek_dir}/index.html"
    output_files = {}
    unless File.exists?(reek_dir) && File.directory?(reek_dir)
    Find.find(RAILS_ROOT) do |path|
      if (
        (path =~ /#{RAILS_ROOT}\/app\// || path =~ /#{RAILS_ROOT}\/lib\//) &&
        path =~ /\.rb$/i
        output_file = "#{File.basename(path)}.txt"
        cmd = "reek #{path} > #{reek_dir}/#{output_file}"
        puts cmd
        output_files[path] = output_file
    puts "Writing index file to #{index_file}..."
    paths = output_files.keys.sort
    File.open(index_file, 'w') do |file|
      file.write("Reek Results for #{RAILS_ROOT}")

Reek Results for #{RAILS_ROOT}

") file.write('
    ') paths.each do |path| name = output_files[path] file.write('
  1. ') link_name = File.basename(name, '.rb.txt') file.write('' + link_name + '') file.write(' from ' + path) file.write("
  2. \n") end file.write('
') file.write('') file.write('') end end desc 'Generates public/reports.html' task :make_index do require 'find' require 'fileutils' puts "\nGenerating index of reports..." reports = [] index_file = "#{RAILS_ROOT}/public/reports.html" Find.find(RAILS_ROOT + '/public') do |path| if ( path =~ /flay_report\.txt$/ || path =~ /flog_report\.txt$/ || path =~ /roodi_report\.txt$/ ) reports << File.basename(path) end end File.open(index_file, 'w') do |file| file.write('') file.write('') file.write("Reports for #{RAILS_ROOT}") file.write('') file.write('') file.write("

Reports for #{RAILS_ROOT}

") file.write('
    ') reek_file = "#{RAILS_ROOT}/public/reek/index.html" if File.exists?(reek_file) file.write('
  1. Reek') file.write(", last updated #{get_mtime(reek_file)}
  2. ") end token_file = "#{RAILS_ROOT}/public/saikuro/index_token.html" if File.exists?(token_file) file.write('
  3. Tokens') file.write(", last updated #{get_mtime(token_file)}
  4. ") end cyclo_file = "#{RAILS_ROOT}/public/saikuro/index_cyclo.html" if File.exists?(cyclo_file) file.write('
  5. Cyclomatic complexity') file.write(", last updated #{get_mtime(cyclo_file)}
  6. ") end reports.each do |name| file.write('
  7. ') file.write('' + name + '') file.write(", last updated #{get_mtime(RAILS_ROOT + '/public/' + name)}") file.write('
  8. ') end file.write('
') file.write('') file.write('') end end desc 'Generates Roodi report' task :make_roodi do puts "\nGenerating Roodi report..." cmd = "roodi \"#{RAILS_ROOT}/**/*.rb\" > #{RAILS_ROOT}/public/roodi_report.txt" puts cmd system(cmd) end desc 'Generates Flay report' task :make_flay do puts "\nGenerating Flay report..." cmd = "flay #{RAILS_ROOT}/app/ > #{RAILS_ROOT}/public/flay_report.txt" puts cmd system(cmd) end desc 'Generates Saikuro report' task :make_saikuro do saikuro_dir = "#{RAILS_ROOT}/public/saikuro/" unless File.exists?(saikuro_dir) && File.directory?(saikuro_dir) FileUtils.mkdir(saikuro_dir) end puts "\nGenerating Saikuro report..." cmd = "saikuro -c -t -i #{RAILS_ROOT}/app/ -y 0 -w 11 -e 16 -o #{saikuro_dir}" puts cmd system(cmd) end desc 'Generates Flog report' task :make_flog do puts "\nGenerating Flog report..." cmd = "flog #{RAILS_ROOT}/app/ > #{RAILS_ROOT}/public/flog_report.txt" puts cmd system(cmd) end desc 'Generates all reports.' task :make_all do system('rake metrics:make_reek') system('rake metrics:make_roodi') system('rake metrics:make_flay') system('rake metrics:make_saikuro') system('rake metrics:make_flog') system('rake metrics:make_index') end end

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}