From 48cd983d2984a6c29c0a387c331013aafa9312d0 Mon Sep 17 00:00:00 2001 From: kronenthaler Date: Mon, 16 Jan 2017 22:50:25 +0100 Subject: [PATCH] feat: add variant groups for localization --- pbxproj/pbxextensions/ProjectFiles.py | 2 +- pbxproj/pbxsections/PBXFileReference.py | 2 +- pbxproj/pbxsections/PBXGroup.py | 14 ----------- pbxproj/pbxsections/PBXVariantGroup.py | 30 ++++++++++++++++++++++++ pbxproj/pbxsections/__init__.py | 1 + tests/pbxextensions/TestProjectFiles.py | 19 +++++++++++++++ tests/pbxsections/TestPBXVariantGroup.py | 30 ++++++++++++++++++++++++ 7 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 pbxproj/pbxsections/PBXVariantGroup.py create mode 100644 tests/pbxsections/TestPBXVariantGroup.py diff --git a/pbxproj/pbxextensions/ProjectFiles.py b/pbxproj/pbxextensions/ProjectFiles.py index 5a9896e4..f55cd0cf 100644 --- a/pbxproj/pbxextensions/ProjectFiles.py +++ b/pbxproj/pbxextensions/ProjectFiles.py @@ -357,7 +357,7 @@ def _create_variant_groups(self, file_ref): self.objects[variant_group.get_id()] = variant_group variant_group.add_variant(file_ref) - return variant + return variant_group def _file_in_project(self, path): """ diff --git a/pbxproj/pbxsections/PBXFileReference.py b/pbxproj/pbxsections/PBXFileReference.py index 0a0d90ec..30956b60 100644 --- a/pbxproj/pbxsections/PBXFileReference.py +++ b/pbxproj/pbxsections/PBXFileReference.py @@ -25,7 +25,7 @@ def set_last_known_file_type(self, file_type): # for localization strings the name has to be the name of the language file if file_type == u'text.plist.strings': - self.name = os.path.splitext(os.path.split(self.name)[0])[0] + self.name = os.path.splitext(os.path.split(self.path)[0])[0] def _print_object(self, indentation_depth=u'', entry_separator=u'\n', object_start=u'\n', indentation_increment=u'\t'): diff --git a/pbxproj/pbxsections/PBXGroup.py b/pbxproj/pbxsections/PBXGroup.py index 25cfcbed..b3f526e4 100644 --- a/pbxproj/pbxsections/PBXGroup.py +++ b/pbxproj/pbxsections/PBXGroup.py @@ -56,17 +56,3 @@ def remove_child(self, child): return True return False - - def remove(self, recursive=True): - parent = self.get_parent() - # remove from the objects reference - del parent[self.get_id()] - - # remove children if necessary - if recursive: - for subgroup_id in self.children: - subgroup = parent[subgroup_id] - if subgroup is None or not subgroup.remove(recursive): - return False - - return True diff --git a/pbxproj/pbxsections/PBXVariantGroup.py b/pbxproj/pbxsections/PBXVariantGroup.py new file mode 100644 index 00000000..3f5fdbdb --- /dev/null +++ b/pbxproj/pbxsections/PBXVariantGroup.py @@ -0,0 +1,30 @@ +from pbxproj.PBXGenericObject import * +from pbxproj.pbxsections.PBXFileReference import * + + +class PBXVariantGroup(PBXGenericObject): + @classmethod + def create(cls, name, children=None, source_tree=u''): + return cls().parse({ + u'_id': cls._generate_id(), + u'isa': cls.__name__, + u'children': children if children else [], + u'name': name, + u'sourceTree': source_tree + }) + + def add_variant(self, file_ref): + if not isinstance(file_ref, PBXFileReference): + return False + + self.children.append(file_ref.get_id()) + return True + + def remove_variant(self, file_ref): + if not isinstance(file_ref, PBXFileReference): + return False + + self.children.remove(file_ref.get_id()) + del self.get_parent()[file_ref.get_id()] + + return True diff --git a/pbxproj/pbxsections/__init__.py b/pbxproj/pbxsections/__init__.py index 816c0d9a..3a009724 100644 --- a/pbxproj/pbxsections/__init__.py +++ b/pbxproj/pbxsections/__init__.py @@ -14,3 +14,4 @@ from pbxproj.pbxsections.PBXAggregatedTarget import * from pbxproj.pbxsections.PBXNativeTarget import * from pbxproj.pbxsections.PBXGroup import * +from pbxproj.pbxsections.PBXVariantGroup import * diff --git a/tests/pbxextensions/TestProjectFiles.py b/tests/pbxextensions/TestProjectFiles.py index 57fc8ec5..9ea98107 100644 --- a/tests/pbxextensions/TestProjectFiles.py +++ b/tests/pbxextensions/TestProjectFiles.py @@ -143,6 +143,25 @@ def testAddFileIfExists(self): build_file = project.add_file("file.m", force=False) self.assertEqual(build_file, []) + def testAddVariantGroupFile(self): + project = XcodeProject(self.obj) + build_file = project.add_file("en.proj/Texts.strings") + + self.assertEqual(project.objects.get_objects_in_section(u'PBXVariantGroup').__len__(), 1) + self.assertEqual(build_file.__len__(), 2) + self.assertEqual(build_file[0].fileRef, project.objects.get_objects_in_section(u'PBXVariantGroup')[0].get_id()) + self.assertEqual(build_file[1].fileRef, project.objects.get_objects_in_section(u'PBXVariantGroup')[0].get_id()) + + def testAddVariantGroupFileToExistingGroup(self): + project = XcodeProject(self.obj) + build_file = project.add_file("en.proj/Texts.strings") + build_file = project.add_file("nl.proj/Texts.strings") + + self.assertEqual(project.objects.get_objects_in_section(u'PBXVariantGroup').__len__(), 1) + self.assertEqual(build_file.__len__(), 2) + self.assertEqual(build_file[0].fileRef, project.objects.get_objects_in_section(u'PBXVariantGroup')[0].get_id()) + self.assertEqual(build_file[1].fileRef, project.objects.get_objects_in_section(u'PBXVariantGroup')[0].get_id()) + def testGetFilesByNameWithNoParent(self): project = XcodeProject(self.obj) files = project.get_files_by_name('file') diff --git a/tests/pbxsections/TestPBXVariantGroup.py b/tests/pbxsections/TestPBXVariantGroup.py new file mode 100644 index 00000000..d98d848d --- /dev/null +++ b/tests/pbxsections/TestPBXVariantGroup.py @@ -0,0 +1,30 @@ +import unittest +from pbxproj.pbxsections.PBXVariantGroup import * +from pbxproj.PBXObjects import * + + +class PBXVariantGroupTest(unittest.TestCase): + def testAddVariantInvalidFileRef(self): + variant = PBXVariantGroup.create(u'texts') + self.assertFalse(variant.add_variant(PBXGenericObject().parse({u'isa': ''}))) + + def testAddVariantValidFileRef(self): + variant = PBXVariantGroup.create(u'texts') + self.assertTrue(variant.add_variant(PBXFileReference().parse({u'_id': '1', u'isa': 'PBXFileReference'}))) + self.assertIn('1', variant.children) + + def testRemoveVariantInvalidFileRef(self): + variant = PBXVariantGroup.create(u'texts') + self.assertFalse(variant.remove_variant(PBXGenericObject().parse({u'isa': ''}))) + + def testRemoveVariantValidFileRef(self): + objs = objects().parse({u'1': {u'isa': 'PBXFileReference'}}) + file_ref = objs['1'] + + variant = PBXVariantGroup.create(u'texts') + objs[variant.get_id()] = variant + + variant.add_variant(file_ref) + + self.assertTrue(variant.remove_variant(file_ref)) + self.assertNotIn('1', variant.children)