Skip to content

Instantly share code, notes, and snippets.

@kbinani
Created April 25, 2013 06:17
Show Gist options
  • Save kbinani/5457874 to your computer and use it in GitHub Desktop.
Save kbinani/5457874 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'tempfile'
require 'fileutils'
def usage
print "usage: xcode-textencoding [project-file] [text-encoding-name]\n"
print " project-file a file path of Xcode project (ex. some.xcodeproj)\n"
print " text-encoding-name screen name of text encoding\n"
end
if ARGV[0].nil? then
usage()
exit
end
file = ARGV[0] + "/project.pbxproj"
encoding_list = {
"Arabic (ISO 8859-6)" => 2147484166, "Arabic (Mac OS)" => 2147483652, "Arabic (Windows)" => 2147484934,
"Baltic (ISO Latin 7)" => 2147484173, "Baltic (Windows)" => 2147484935, "Celtic (ISO Latin 8)" => 2147484174,
"Central European (ISO Latin 2)" => 9, "Central European (ISO Latin 4)" => 2147484164, "Central European (Mac OS)" => 2147483677,
"Central European (Windows Latin 2)" => 15, "Cyrillic (ISO 8859-5)" => 2147484165, "Cyrillic (Mac OS)" => 2147483655,
"Cyrillic (Windows)" => 11, "Greek (ISO 8859-7)" => 2147484167, "Greek (Mac OS)" => 2147483654,
"Greek (Windows)" => 13, "Hebrew (ISO 8859-8)" => 2147484168, "Hebrew (Mac OS)" => 2147483653,
"Hebrew (Windows)" => 2147484933, "Icelandic (Mac OS)" => 2147483685, "Japanese (ISO 2022-JP)" => 21,
"Japanese (Mac OS)" => 2147483649, "Japanese (Shift JIS)" => 2147486209, "Japanese (Windows, DOS)" => 8,
"Japanesee (EUC)" => 3, "Korean (EUC)" => 2147486016, "Korean (Mac OS)" => 2147483651,
"Korean (Windows, DOS)" => 2147484706, "Latin-US (DOS)" => 2147484672, "Non-lossy ASCII" => 7,
"Nordic (ISO Latin 6)" => 2147484170, "Romanian (ISO Latin 10)" => 2147484176, "Simplified Chinese (GB 2312)" => 2147486000,
"Simplified Chinese (Mac OS)" => 2147483673, "Simplified Chinese (Windows, DOS)" => 2147484705, "Thai (ISO 8859-11)" => 2147484171,
"Traditional Chinese (EUC)" => 2147486001, "Traditional Chinese (Mac OS)" => 2147483650, "Traditional Chinese (Windows, DOS)" => 2147484707,
"Turkish (ISO Latin 5)" => 2147484169, "Turkish (Mac OS)" => 2147483683, "Turkish (Windows Latin 5)" => 14,
"Unicode (UTF-8)" => 4, "Unicode (UTF-16)" => 10, "Unicode (UTF-16BE)" => 2415919360,
"Unicode (UTF-16LE)" => 2483028224, "Vietnamese (Windows)" => 2147484936, "Western (ISO Latin 1)" => 5,
"Western (ISO Latin 3)" => 2147484163, "Western (ISO Latin 9)" => 2147484175, "Western (Mac OS Roman)" => 30,
"Western (NextStep)" => 2, "Western (Windows Latin 1)" => 12
}
if ARGV[1].nil? then
print "error: specify text encoding\n"
print "available text encoding:\n"
encoding_list.keys.each { |line| print "\t" + line + "\n" }
exit
end
file = ARGV[0] + "/project.pbxproj"
if not encoding_list.has_key?(ARGV[1]) then
print "error: unknown text encoding \"" + ARGV[1] + "\""
exit
end
encoding = encoding_list[ARGV[1]].to_s
temp = Tempfile.new("temp")
temp_file = temp.path
temp.close!
output = open(temp_file, "w")
input = open(file, "r")
input.each { |line|
line.rstrip!
if (line =~ /^(.*)\{(isa\s?=\s?PBXFileReference;[^}]*)\}(.*)$/) then
prefix = $1
isa_old = $2
suffix = $3
if (isa_old =~ /^(.*)(fileEncoding\s?=\s?[0-9]*)(.*)$/) then
isa = $1 + "fileEncoding = #{encoding}" + $3
else
isa_old =~ /^(.*PBXFileReference;)( .*)$/
isa = $1 + " fileEncoding = #{encoding};" + $2
end
output.puts(prefix + "{" + isa + "}" + suffix)
else
output.puts(line)
end
}
output.close()
input.close()
FileUtils.mv(temp_file, file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment