Created
March 6, 2010 11:35
-
-
Save lucasefe/323648 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
namespace :encoding do | |
task :update_files do | |
Dir.glob("{app,config,spec,test,lib,db,features}/**/*.{rb,rake}").each do |f| | |
add_encoding(f) | |
end | |
end | |
def add_encoding(file_name, encoding = 'utf-8') | |
if File.exist?(file_name) | |
old_content = File.read(file_name) | |
# Ignore the Schema version line because it changes with each migration | |
encoding_regexp = Regexp.new(/^# encoding: (.*)$/) | |
match_data = old_content.match(encoding_regexp) | |
old_encoding = match_data ? match_data.captures[0].strip : "" | |
puts "#{file_name}: #{old_encoding}" | |
unless old_encoding == encoding | |
puts " => Changing encoding '#{old_encoding}' to '#{encoding}'" | |
old_content.sub!(encoding_regexp, '') | |
new_content = "# encoding: #{encoding} \n#{old_content}" | |
File.open(file_name, "wb") { |f| f.puts new_content } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment