Skip to content

Instantly share code, notes, and snippets.

@lucasefe
Created March 6, 2010 11:35
Show Gist options
  • Save lucasefe/323648 to your computer and use it in GitHub Desktop.
Save lucasefe/323648 to your computer and use it in GitHub Desktop.
# 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