Skip to content

Commit

Permalink
fix: made code more resilient to missing/broken references
Browse files Browse the repository at this point in the history
  • Loading branch information
kronenthaler committed Oct 26, 2020
1 parent 9b2e62e commit 5e114c0
Show file tree
Hide file tree
Showing 4 changed files with 900 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pbxproj/XcodeProject.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import shutil
import datetime
import sys
from pbxproj.pbxextensions import *


Expand All @@ -26,13 +27,21 @@ def __init__(self, tree=None, path=None):
if 'objects' not in self:
return

missing_references = False
for section in self.objects.get_sections():
for obj in self.objects.get_objects_in_section(section):
if 'files' in obj and obj['files'] is not None:
comment = obj._get_comment()
for file_id in obj['files']:
# set the section into the objects
self.objects[file_id]._section = comment
if self.objects[file_id] is not None:
# set the section into the objects
self.objects[file_id]._section = comment
else:
missing_references = True

if missing_references:
print('[WARNING] The project contains missing/broken references that may cause other problems.'
' Open your project in Xcode and resolve all red-colored files.', file=sys.stderr)

def save(self, path=None):
if path is None:
Expand Down
6 changes: 6 additions & 0 deletions pbxproj/pbxextensions/ProjectFiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ def _filter_targets_without_path(self, path, target_name):
build_phase = self.get_object(build_phase_id)
for build_file_id in build_phase.files:
build_file = self.get_object(build_file_id)
if build_file is None:
continue

file_ref = self.get_object(build_file.fileRef)
if 'path' in file_ref and ProjectFiles._path_leaf(path) == ProjectFiles._path_leaf(file_ref.path):
potential_targets.remove(target)
Expand Down Expand Up @@ -316,6 +319,9 @@ def remove_file_by_id(self, file_id, target_name=None):
build_phase = self.objects[build_phase_id]

for build_file_id in build_phase.files:
if build_file_id not in self.objects:
continue

build_file = self.objects[build_file_id]

if build_file.fileRef == file_ref.get_id():
Expand Down
7 changes: 7 additions & 0 deletions tests/pbxcli/TestPBXCLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,10 @@ def testCommandWithFailure(self):
with self.assertRaisesRegex(SystemExit, '^1'):
parser({u'<project>': u'whatever/project.pbxproj', u'--backup': False})
self.assertEqual(sys.stdout.getvalue().strip(), u'Project file not found')

def testOpenFileWithBrokenReferences(self):
sys.stderr = StringIO()
project = open_project({'<project>': 'samplescli/broken-references.pbxproj'})
self.assertEqual(sys.stderr.getvalue().strip(), '[WARNING] The project contains missing/broken references that '
'may cause other problems. Open your project in Xcode and '
'resolve all red-colored files.')
Loading

0 comments on commit 5e114c0

Please sign in to comment.