Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check asset encoding for valid UTF-8
namespace :assets do
desc "Check that all assets have valid encoding"
task :check => :environment do
paths = ["app/assets", "lib/assets", "vendor/assets"]
extensions = ["js", "coffee", "css", "scss"]
paths.each do |path|
dir_path = Rails.root + path
if File.exists?(dir_path)
dir_files = File.join(dir_path, "**")
Dir.glob(dir_files + "/**.{#{extensions.join(',')}}").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 "Invalid encoding: #{ file }"
end
end
end # end Dir.glob
end #end File.exists
end # end paths.each
end
end
@sandstrom

This comment has been minimized.

Copy link

commented Mar 21, 2012

Thanks for the excellent script! Here is a slightly modified version that checks coffee and scss too.

# check that all assets have the correct encoding
# https://gist.github.com/1301199
namespace :assets do

  desc "Check that all assets have valid encoding"
  task  :check => :environment do

    paths = ["app/assets", "lib/assets", "vendor/assets"]
    extensions = ["js", "coffee", "css", "scss"]

    paths.each do |path|
      dir_path = Rails.root + path

      if File.exists?(dir_path)
        dir_files = File.join(dir_path, "**")

        Dir.glob(dir_files + "/**.{#{extensions.join(',')}}").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 "Invalid encoding: #{ file }"
              else
                puts "Valid: #{ file }"
              end
            end

        end # end Dir.glob

      end #end File.exists
    end # end paths.each

  end

end
@thermistor

This comment has been minimized.

Copy link
Owner Author

commented Mar 30, 2012

Thank @shedd, I'll incorporate that in my version though! Cheers.

@a-ayyash

This comment has been minimized.

Copy link

commented Jun 1, 2012

thanks a lot

@prodigerati

This comment has been minimized.

Copy link

commented Mar 11, 2013

Thanks for sharing!

@stereoscott

This comment has been minimized.

Copy link

commented May 28, 2013

Does anyone know how I could update this task so that it also checks included gems assets?

@caike

This comment has been minimized.

Copy link

commented Jun 14, 2013

Thanks!

For the record, after using this rake task to track the files that were not UTF-8, I was able to convert them using recode, as recommended here http://stackoverflow.com/questions/64860/best-way-to-convert-text-files-between-character-sets

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.