Skip to content

Instantly share code, notes, and snippets.

@derencius
Forked from francesc/utf8encode.rake
Created February 3, 2011 19:05
Show Gist options
  • Save derencius/809970 to your computer and use it in GitHub Desktop.
Save derencius/809970 to your computer and use it in GitHub Desktop.
thor file to add utf-8 header on ruby files. useful for converting ruby 1.8 projects to 1.9
class Utf8Header < Thor
desc "add", "Add Content UTF-8 on top of all .rb/.feature files"
# copy & pasted from https://gist.github.com/738245
def add
files = Array.new
["*.rb", "*.rake","*.feature"].each do |extension|
files.concat(Dir[ File.join(Dir.getwd.split(/\\/), "**", extension) ])
end
files.each do |file|
content = File.read(file)
next if content[0..16] == "# coding: UTF-8\n\n" ||
content[0..22] == "# -*- coding: utf-8 -*-"
["\n\n", "\n"].each do |file_end|
content = content.gsub(/(# encoding: UTF-8#{file_end})|(# coding: UTF-8#{file_end})|(# -*- coding: UTF-8 -*-#{file_end})|(# -*- coding: utf-8 -*-#{file_end})/i, "")
end
new_file = File.open(file, "w")
new_file.write("# coding: UTF-8\n\n"+content)
new_file.close
end
end
end
@derencius
Copy link
Author

gem install thor
thor install utf8_header.rb
thor utf8_header:add

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment