Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Checking the syntax of various Ruby related files in a Rails project.
namespace :syntax do
desc 'Checks the syntax of ERB files'
task :erb do
require 'action_view'
Dir['app/views/**/*.erb'].each do |file|
template = File.read(file)
begin
ActionView::Template::Handlers::Erubis \
.new(template, :filename => file) \
.result
print '.'
# ERB syntax errors.
rescue SyntaxError => error
puts 'F'
puts error.message
exit 1
# Calls to undefined methods, etc. We only care about the syntax.
rescue Exception => error
print '.'
end
end
puts
puts 'ERB OK!'
end
desc 'Checks the syntax of HAML files'
task :haml do
require 'haml'
Dir['app/views/**/*.haml'].each do |file|
template = File.read(file)
begin
Haml::Engine.new(template, :filename => file)
print '.'
rescue Haml::SyntaxError, Haml::Error => error
line = error.backtrace[0].match(/(\d+)$/)[1]
puts 'F'
puts "#{file}: line ##{line}: #{error.message}"
exit 1
end
end
puts
puts 'HAML OK!'
end
desc 'Checks the syntax of YAML files'
task :yaml do
require 'yaml'
Dir['./**/*.{yml,yaml}'].each do |file|
begin
YAML.load_file(file)
print '.'
rescue => error
puts 'F'
puts "#{file}: #{error.message}"
exit 1
end
end
puts
puts 'YAML OK!'
end
end
desc 'Checks the syntax of various files'
task :syntax => ['syntax:erb', 'syntax:haml', 'syntax:yaml']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.