DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Code Reports Rake Task

Snippets Manager user avatar by
Snippets Manager
·
Mar. 30, 09 · · Code Snippet
Like (0)
Save
Tweet
465 Views

Join the DZone community and get the full member experience.

Join For Free
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")
  end
  
  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)
      FileUtils.mkdir(reek_dir)
    end
    
    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
        system(cmd)
        output_files[path] = output_file
      end
    end
    
    puts "Writing index file to #{index_file}..."
    paths = output_files.keys.sort
    
    File.open(index_file, 'w') do |file|
      file.write('')
      file.write('')
      file.write("Reek Results for #{RAILS_ROOT}")
      file.write('')
      file.write('')
      file.write("

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
Task (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Ultra-Fast Microservices: When Microstream Meets Payara
  • Password Authentication. How to Correctly Do It.
  • Stupid Things Orgs Do That Kill Productivity w/ Netflix, FloSports & Refactoring.club
  • JUnit 5 Tutorial: Nice and Easy [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo