Skip to content

Instantly share code, notes, and snippets.

@caffo
Created July 28, 2008 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caffo/2959 to your computer and use it in GitHub Desktop.
Save caffo/2959 to your computer and use it in GitHub Desktop.
sake task for syntax checking a ruby on rails project
# sake rails:check_syntax
# based on this rake task: http://moourl.com/s2dnw
# non-copyright (c) 2008 rodrigo franco <caffo@imap.cc>
namespace :rails do
task :load_requirements_and do
require 'erb'
require 'open3'
require 'yaml'
end
desc "Check all files"
task :check_syntax => ["rails:check_ruby", "rails:check_erb", "rails:check_yaml"]
desc "Check ERB files"
task :check_erb => "rails:load_requirements_and" do
puts "#{Time.now} - checking erb files..."
(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
desc "Check ruby files"
task :check_ruby => "rails:load_requirements_and" do
puts "#{Time.now} - checking ruby files..."
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|
puts("\t#{error}") if ((error ||= "") == stderr.readline rescue false)
stdin.close rescue false
stdout.close rescue false
stderr.close rescue false
end
end
end
desc "Check YAML files"
task :check_yaml => "rails:load_requirements_and" do
puts "#{Time.now} - checking yaml files..."
Dir['**/*.yml'].each do |file|
next if file.match("vendor/rails")
next if file.match("vendor/plugins/.*")
begin
YAML.load_file(file)
rescue => e
puts("\t#{file}:#{(e.message.match(/on line (\d+)/)[1] + ":") rescue nil} #{e.message}")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment