# encoding: utf-8 #-- # Copyright (c) 2010 Richard Huang (flyerhzm@gmail.com) # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #++ require 'rubygems' require 'progressbar' require 'colored' require 'haml' require 'rails_best_practices/prepares' require 'rails_best_practices/reviews' require 'rails_best_practices/core' require 'fileutils' # RailsBestPractices helps you to analyze your rails code, according to best practices on http://rails-bestpractices. # if it finds any violatioins to best practices, it will give you some readable suggestions. # # The analysis process is partitioned into two parts, # # 1. prepare process, it checks only model and mailer files, do some preparations, such as remember model names and associations. # 2. review process, it checks all files, according to configuration, it really check if codes violate the best practices, if so, remember the violations. # # After analyzing, output the violations. module RailsBestPractices DEFAULT_CONFIG = File.join(File.dirname(__FILE__), "..", "rails_best_practices.yml") class < b end } end # ignore specific files. # # @param [Array] files # @param [Regexp] pattern files match the pattern will be ignored # @return [Array] files that not match the pattern def file_ignore files, pattern files.reject { |file| file.index(pattern) } end # output errors if exist. def output_terminal_errors @runner.errors.each { |error| plain_output(error.to_s, 'red') } plain_output("\nPlease go to http://rails-bestpractices.com to see more useful Rails Best Practices.", 'green') if @runner.errors.empty? plain_output("\nNo error found. Cool!", 'green') else plain_output("\nFound #{@runner.errors.size} errors.", 'red') end end def plain_output(message, color) if @options["without-color"] puts message else puts message.send(color) end end def output_html_errors template = File.read(File.join(File.dirname(__FILE__), "..", "assets", "result.html.haml")) File.open("rails_best_practices_output.html", "w+") do |file| file.puts Haml::Engine.new(template).render(Object.new, :errors => @runner.errors, :textmate => @options["with-textmate"]) end end end end