pbxproj Python Library: Adding a reference to a group duplicates directories in the path

40 Views Asked by At

I'm using the pbxproj Python library to programatically add a reference to a folder in my Xcode project (we need it so an external script can pick it up to tag resources)

Manually, we can drag the folder into the xcode navigator and work from there, but I believe this library can also do that. Below is our project tree

.
└── MyProject/
    └── FolderOne/
        └── FolderTwo

FolderTwo is empty so does not show in the navigator (but it is in Finder). We drag it in to reference it and then we can add it as a Resource Tag for a target and our external script tags all the resources we need (uses FolderTwo as a reference)

Using the above library, I've experimented with the below

from pbxproj import XcodeProject, PBXGenericObject, PBXFileReference
from pbxproj.pbxextensions import FileOptions

# open the project
project = XcodeProject.load('{ABS_PATH}/MyProject.xcodeproj/project.pbxproj')

ondemand_group = project.get_or_create_group(name='FolderOne')

# Unsure if we require any FileOptions for this
file_options = FileOptions(
    create_build_files=True,
    add_groups_relative=False
)

relative_path = 'FolderTwo'

folder = project.add_folder(
    path=relative_path,
    parent=ondemand_group,
    create_groups=False,    #This makes sure it's a reference, not a group
    target_name='MyTarget',
    file_options=file_options
)

project.save()

Wherever I run the python script from, outside the project, inside the project, it always gets created as a file (not a folder), in FolderOne. Not referenced (red letters) and the path in Xcode duplicates the project path

{ABS_PATH}/MyProject/FolderOne/MyProject/FolderOne/FolderTwo

Curiously, if I omit the parent, the referenced folder is created correctly, but in the MainProject

Unsure what I'm doing wrong. Any help would be appreciated

1

There are 1 best solutions below

0
JordiLaForge On

Solution to this is to use add_file instead of add_group or add_folder. You can then get the fileRef and change that to lastKnownType or explicit type to 'folder'. Seems counterintuitive with add_folder and add_group as options but anyway.