Guest User

Untitled

a guest
May 20th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. # encoding: utf-8
  2. namespace :encoding do
  3. task :update_files do
  4. Dir.glob("{app,config,spec,test,lib,db,features}/**/*.{rb,rake}").each do |f|
  5. add_encoding(f)
  6. end
  7. end
  8.  
  9. def add_encoding(file_name, encoding = 'utf-8')
  10. if File.exist?(file_name)
  11. old_content = File.read(file_name)
  12.  
  13. # Ignore the Schema version line because it changes with each migration
  14. encoding_regexp = Regexp.new(/^# encoding: (.*)$/)
  15. match_data = old_content.match(encoding_regexp)
  16. old_encoding = match_data ? match_data.captures[0].strip : ""
  17.  
  18. puts "#{file_name}: #{old_encoding}"
  19. unless old_encoding == encoding
  20. puts " => Changing encoding '#{old_encoding}' to '#{encoding}'"
  21. old_content.sub!(encoding_regexp, '')
  22. new_content = "# encoding: #{encoding} \n#{old_content}"
  23. File.open(file_name, "wb") { |f| f.puts new_content }
  24. end
  25. end
  26. end
  27. end
Add Comment
Please, Sign In to add comment