Skip to content

Instantly share code, notes, and snippets.

@sinisterchipmunk
Forked from shedd/assets.rake
Created September 13, 2011 13:53
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 sinisterchipmunk/1213850 to your computer and use it in GitHub Desktop.
Save sinisterchipmunk/1213850 to your computer and use it in GitHub Desktop.
Check asset encoding for valid UTF-8; if not valid, show compatible encodings
namespace :assets do
task :check => :environment do
paths = ["app/assets", "lib/assets", "vendor/assets"]
paths.each do |path|
dir_path = Rails.root + path
if File.exists?(dir_path)
dir_files = File.join(dir_path, "**")
Dir.glob(dir_files + "/**.{js,css}").each do |file|
# make sure we're not trying to process a directory
unless File.directory?(file)
# read the file and check its encoding
data = File.read(file)
unless data.valid_encoding?
puts "#{ file } does not have valid encoding!"
encodings = []
for enc in Encoding.name_list
if data.dup.force_encoding(enc).valid_encoding?
encodings << enc
end
end
puts " (it could be one of #{encodings})" unless encodings.empty?
end
end
end # end Dir.glob
end #end File.exists
end # end paths.each
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment