DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Reek Rake Task

03.30.2009
| 4335 views |
  • submit to reddit
        This will use the <a href="http://github.com/kevinrutherford/reek/tree/master">Reek Ruby Gem</a> and Rake to go through your Rails app and call Reek on every .rb file.  It pipes the Reek output to a file in public/reek/file_name.rb.txt and generates an index.html file in public/reek/ with links to view each text file.  I wrote this Rake task because I couldn't seem to run Reek on my Rails app's app/ directory; maybe I just didn't read enough about Reek's usage.

Save this file with the extension .rake in your Rails app's lib/tasks/ directory, e.g. lib/tasks/reek.rake.  You can then run 'rake reek' on the command line within your Rails app's directory.  You will need Reek to be installed first, and Reek should be runnable just by typing 'reek'.

desc "Will generate Reek output for each Ruby file in RAILS_ROOT."
task(: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 =~ /\.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}..."
  
  File.open(index_file, 'w') do |file|
    file.write('<html>')
    file.write('<head>')
    file.write('<title>Reek Results</title>')
    file.write('</head>')
    file.write('<body>')
    file.write('<ol>')
    output_files.each do |path, name|
      next unless File.size?(path)
      file.write('<li>')
      file.write('<a href="' + name + '">' + name + '</a>')
      file.write(' (' + path + ')')
      file.write(' file size ' + File.size(path).to_s)
      file.write('</li>')
    end
    file.write('</ol>')
    file.write('</body>')
    file.write('</html>')
  end
end