Skip to content

Commit

Permalink
feat: add variant groups for localization
Browse files Browse the repository at this point in the history
  • Loading branch information
kronenthaler committed Jan 16, 2017
1 parent 0719478 commit 48cd983
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pbxproj/pbxextensions/ProjectFiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion pbxproj/pbxsections/PBXFileReference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down
14 changes: 0 additions & 14 deletions pbxproj/pbxsections/PBXGroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions pbxproj/pbxsections/PBXVariantGroup.py
Original file line number Diff line number Diff line change
@@ -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'<group>'):
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
1 change: 1 addition & 0 deletions pbxproj/pbxsections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
from pbxproj.pbxsections.PBXAggregatedTarget import *
from pbxproj.pbxsections.PBXNativeTarget import *
from pbxproj.pbxsections.PBXGroup import *
from pbxproj.pbxsections.PBXVariantGroup import *
19 changes: 19 additions & 0 deletions tests/pbxextensions/TestProjectFiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
30 changes: 30 additions & 0 deletions tests/pbxsections/TestPBXVariantGroup.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 48cd983

Please sign in to comment.