Here's a quick rake file that will crawl through your Rails project and syntax check ruby, erb, and yml files. You should always run this before doing a "cap deploy" and it even doesn't hurt to run it before a "svn commit".
require 'erb' require 'open3' require 'yaml' task :check_syntax => [:check_ruby, :check_erb, :check_yaml] task :check_erb do (Dir["**/*.erb"] + Dir["**/*.rhtml"]).each do |file| next if file.match("vendor/rails") Open3.popen3('ruby -c') do |stdin, stdout, stderr| stdin.puts(ERB.new(File.read(file), nil, '-').src) stdin.close if error = ((stderr.readline rescue false)) puts file + error[1..-1] end stdout.close rescue false stderr.close rescue false end end end task :check_ruby do Dir['**/*.rb'].each do |file| next if file.match("vendor/rails") next if file.match("vendor/plugins/.*/generators/.*/templates") Open3.popen3("ruby -c #{file}") do |stdin, stdout, stderr| if error = ((stderr.readline rescue false)) puts error end stdin.close rescue false stdout.close rescue false stderr.close rescue false end end end task :check_yaml do Dir['**/*.yml'].each do |file| next if file.match("vendor/rails") begin YAML.load_file(file) rescue => e puts "#{file}:#{(e.message.match(/on line (\d+)/)[1] + ':') rescue nil} #{e.message}" end end end
And here's what the output looks like:
warren:tmp_project$ rake check_syntax (in /Users/warren/tmp_project) app/controllers/application.rb:37: syntax error, unexpected '=' app/views/people/home.html.erb:60: syntax error, unexpected '<', expecting $end vendor/plugins/will_paginate/test/fixtures/users.yml:13: syntax error on line 13, col 0: `dev_<%= digit %>:'
This could probably be expanded to support lots of other types of files as well (css, javascripts, etc).
Updates:
- Exclude all files in vendor/rails (thanks szeryf)






