Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a migration script.

This allows easier migration to Greenwich from multiple xib files.
  • Loading branch information...
commit a08900aed2548542e7440edbd3a01d718761cea7 1 parent 3fd16e3
@wbyoung wbyoung authored
View
89 Framework/Scripts/actions/migrate
@@ -0,0 +1,89 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2011 FadingRed LLC
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
+# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
+# Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+
+from scriptlib.config import OptionParser
+from scriptlib.i18n import Strings, Xib
+import sys
+import os
+import codecs
+
+# global defines
+# ----------------------------------------------------------------------
+
+LOCALIZE_SYMBOL = os.environ.get('GREENWICH_LOCALIZATION_SYMBOL', 'NSLocalizedString') # localize symbol used in code
+
+
+# main create strings function
+# ----------------------------------------------------------------------
+
+def migrate_strings(config):
+ if os.path.exists(config.resources):
+ base_path = os.path.join(config.resources, '%s.lproj' % config.lang)
+ remove = []
+ move = []
+ for xibname in os.listdir(base_path):
+ if xibname.endswith('.xib'):
+ xib_path = os.path.join(base_path, xibname)
+ xib = Xib(xibname, config, path=xib_path)
+ strings = Strings(xibname, config.lang, config)
+ xib.generate(strings, normalize=False)
+
+ # handle all languages other than the base language of the file by creating
+ # and integrating changes from xib files in other lproj directories
+ for langname in os.listdir(config.resources):
+ if langname.endswith('.lproj'):
+ lang = Strings(xibname, langname, config)
+ if lang.lang != config.lang:
+ lang_path = os.path.join(config.resources, langname)
+ lang_xib_path = os.path.join(lang_path, xibname)
+ if os.path.exists(lang_xib_path):
+ lang_xib = Xib(xibname, config, path=lang_xib_path)
+ lang_xib.generate(lang, normalize=False)
+ lang.normalize(source=strings)
+ remove.append(lang_xib_path)
+
+ # normalize strings after handling all languages
+ strings.normalize()
+ move.append(xib_path)
+
+ if move or remove:
+ if config.force: run = True
+ else:
+ sys.stdout.write(
+ 'Strings files were created successfully. This script can also move your main xib\n' \
+ 'file into place and remove extra xibs. %i xib files will be moved, and %i deleted.\n\n' % (len(move), len(remove)))
+ run = (raw_input('Update xib files? [Y/n] ').lower() == 'y')
+ if run:
+ for path in remove:
+ os.unlink(path)
+ for path in move:
+ directory, name = os.path.split(path)
+ directory = os.path.dirname(directory)
+ os.rename(path, os.path.join(directory, name))
+
+
+# main function
+# ----------------------------------------------------------------------
+
+if __name__ == '__main__':
+ usage = """usage: %%prog [options]
+ Used to migrate a project from xib files in their lproj folders to a single xib with multiple strings files."""
+ parser = OptionParser(usage=usage)
+ f = 'Force renaming and removal of old xibs'
+ parser.add_option('-f', '--force', action="store_true", dest='force', default=False, help=f)
+ migrate_strings(parser.parse_config()[0])
View
22 Framework/Scripts/lib/scriptlib/i18n/strings.py
@@ -71,20 +71,27 @@ def _create_translation(self):
FMTARG = r'%\d\$(@|d|D|i|u|U|hi|hu|qi|qu|x|X|qx|qX|o|O|f|e|E|g|G|s|S|p|L|a|A|F|z|t|j)'
FMTSUB = r'%\1'
if re.sub(FMTARG, FMTSUB, value.key) != re.sub(FMTARG, FMTSUB, value.trans):
- value.key = value.trans
+ value.key_normalized = value.trans
self._index[value.key] = value
- def normalize(self):
+ def normalize(self, source=None):
"""
- Normalizes strings to an expected format
+ Normalizes strings to an expected format. If given, it will use source
+ as the source of normalized keys to use. This is convenient for migrating
+ xib files over to multiple strings files since the real keys are object
+ identifiers in the xib. This allows you to use the source language strings
+ as the source for normalization and actually get the source language key
+ for other languages.
"""
lines = []
- index = self.index()
- for key in sorted(index.keys(), key=unicode.lower):
- value = index[key]
+ if not source: source = self
+ for key in sorted(self.index().keys(), key=unicode.lower):
+ try: line_key = source.index()[key].key_normalized
+ except KeyError: line_key = self.index()[key].key_normalized
+ line_trans = self.index()[key].trans
lines += [
'/* No comment provided by engineer. */\n',
- '"%s" = "%s";\n\n' % (value.key, value.trans),
+ '"%s" = "%s";\n\n' % (line_key, line_trans),
]
os.unlink(self.path())
if lines:
@@ -157,6 +164,7 @@ def cmp(a, b): return cmp(a.linenum, b.linenum)
class Translation(object):
def __init__(self, key, trans, comments, linenum):
self.key = key
+ self.key_normalized = key
self.trans = trans
self.comments = comments
self.linenum = linenum
View
8 Framework/Scripts/lib/scriptlib/i18n/xib.py
@@ -23,13 +23,15 @@
import os
class Xib(object):
- def __init__(self, name, config):
+ def __init__(self, name, config, path=None):
self.name = clean_name(name)
self.config = config
+ self._path = path
self._mtime = None
self._gtime = None
def path(self):
+ if self._path: return self._path
resources = self.config.resources
path = os.path.join(resources, '%s.xib' % self.name)
return path
@@ -57,7 +59,7 @@ def _ensure_dir_for_file(self, path):
if not os.path.exists(directory):
os.mkdir(directory)
- def generate(self, strings):
+ def generate(self, strings, normalize=True):
"""
Generate strings file in an expected format and update generation time
"""
@@ -73,7 +75,7 @@ def generate(self, strings):
result = process.wait()
if result != 0:
sys.stderr.write('warning: error generating strings, see prior output for information\n');
- strings.normalize()
+ if normalize: strings.normalize()
generation = self._generation()
self._ensure_dir_for_file(generation)
os.system('touch "%s"' % (generation,))
Please sign in to comment.
Something went wrong with that request. Please try again.