Skip to content

Instantly share code, notes, and snippets.

@rimidl
Created June 5, 2012 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rimidl/2874591 to your computer and use it in GitHub Desktop.
Save rimidl/2874591 to your computer and use it in GitHub Desktop.
RubyEncodingMarker - скрипт, занимающийся вставкой информации о кодировке файла необходимой для корректной работы Ruby 1.9. Полезен при переходе со старой версии Ruby.
# -*- encoding: utf-8 -*-
# file name: ruby_encoding_marker.rb
#
# @example Use in console:
# $ ruby ruby_encoding_marker.rb app/controllers
#
module RubyEncodingMarker
UTF8 = '# -*- encoding: utf-8 -*-'
class << self
# @param [String] Путь до файла или директории
#
def mark_as_utf8(path)
mark_dir_or_file(path, UTF8)
end
private
def mark_dir_or_file(path, encoding_comment)
if Dir.exists?(path)
mark_dir(path, UTF8)
elsif File.exists?(path)
mark_file(path, UTF8)
else
raise "Path: #{path} not detected as directory or file"
end
end
# @note Работает рекурсия
#
def mark_dir(path, encoding_comment)
Dir.foreach(path) do |filename|
next if filename == '.' || filename == '..'
file_path = File.join(path, filename)
if Dir.exists?(file_path)
mark_dir(file_path, encoding_comment)
elsif File.exists?(file_path)
mark_file(file_path, encoding_comment)
else
raise "Path: #{file_path} not detected as directory or file"
end
end
end
def mark_file(file_path, encoding_comment)
current_file_content = File.read(file_path)
new_file_content = [encoding_comment, current_file_content].join("\n\n")
File.write(file_path, new_file_content, :mode => 'w')
end
end # class << self
end
def main(path)
RubyEncodingMarker.mark_as_utf8(path)
end
## Begin
path = ARGV[0]
main(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment