kronenthaler / mod-pbxproj

A python module to manipulate XCode projects
MIT License
1.21k stars 294 forks source link

How can I get the project target #41

Closed draveness closed 9 years ago

draveness commented 9 years ago

How can I get the project target. because when I use this script to add files to Pods project, the code seems not work, But if I drag the folder to Pods project, it can works. This is my code

8dfa3268-ab1d-4e64-ad0f-d7f0904b6d2c

project = XcodeProject.Load(pbxproj_file_path)
project.remove_group_by_name('UIKit', True)
project.save()

if production:
    main_group = project.get_groups_by_name('DKNightVersion')[0]
    uikit_group = project.get_or_create_group('UIKit', None, main_group)
else:
    main_group = project.get_or_create_group('DKNightVersion')
    pod_group = project.get_or_create_group('Pod', None, main_group)
    class_group = project.get_or_create_group('Classes', None, pod_group)
    uikit_group = project.get_or_create_group('UIKit', None, class_group)

groups = json.load(open(json_file_path))

for group, files in groups.iteritems():
    new_group = project.get_or_create_group(group, None, uikit_group)
    for f in files:
        project.add_file(f, new_group)

project.save()

39d31f05-c374-42fc-80ce-60fe593197eb

kronenthaler commented 9 years ago

I might need to do some research about this one. Couple of questions:

  1. Are you opening the pods project?
  2. Your goal is to add it to both targets at the same time?
  3. What kind of file are you trying to add? (extension of the files)
  4. Does it work if you add them in another non-pods project?
draveness commented 9 years ago
  1. I am opening the pods project.
  2. I would like to add these first just to Pods project first target.
  3. .h and .m
  4. Yes, it works, when I developing my pods, But when I submit the pods and manage it with cocoapods, and run this script to add files to Pod project, some of the code there cannot be executed.
kronenthaler commented 9 years ago

can you provide a pod's xcode project?

draveness commented 9 years ago

Through email?

kronenthaler commented 9 years ago

share it in a gist. https://gist.github.com/

draveness commented 9 years ago

This is the correct project https://gist.github.com/Draveness/0c0ced9fb54bd30b8ce9

draveness commented 9 years ago

This is a wrong proj https://gist.github.com/Draveness/2e913c74c7f24878da1f which is added through this:

project = XcodeProject.Load(pbxproj_file_path)
project.remove_group_by_name('UIKit', True)
project.save()

main_group = project.get_groups_by_name('DKNightVersion')[0]
uikit_group = project.get_or_create_group('UIKit', None, main_group)
groups = json.load(open(json_file_path))

for group, files in groups.iteritems():
    new_group = project.get_or_create_group(group, None, uikit_group)
    for f in files:
        project.add_file(f, new_group)

project.save()
kronenthaler commented 9 years ago

I tested the following script

#!/usr/bin/python

import sys
from mod_pbxproj import XcodeProject

pathToProjectFile = 'test/test.xcodeproj/project.pbxproj'
project = XcodeProject.Load( pathToProjectFile )

framework_path = 'System/Library/Frameworks/WebKit.framework'
framework_group = project.get_or_create_group('Frameworks')
project.add_file(framework_path, parent=framework_group)
project.add_file('source.m', framework_group)
project.add_file('source.h', framework_group)

project.save()

The files were added properly to ALL targets (see images below, note the black letters instead of red ones.)

My suggestion though, is not to use add_file directly, but use add_file_if_doesnt_exist instead, it adds some validations and avoid duplications.

screen shot 2015-05-05 at 18 07 32 screen shot 2015-05-05 at 18 07 48

draveness commented 9 years ago

Do you means I are supposed I should use add_file_if_doesnt_exist instead of all all_file methods?

kronenthaler commented 9 years ago

wait, i didn't see the second gist. I'm rechecking...

draveness commented 9 years ago

OK, I am trying now..

kronenthaler commented 9 years ago

I found something. Can you:

  1. comment out you project.remove_group_by_name('UIKit', True)
  2. remove the group manually from xcode
  3. rerun your code.

I suspect there was an exception during the remove process that aborted the execution of the rest.

draveness commented 9 years ago

I tried, but not work.... I am really confused with this...

kronenthaler commented 9 years ago

I committed a change regarding the remove_group_by_name. please update and try again. If still fails i have to dig deeper in the issue, it will take a while.

draveness commented 9 years ago

I have already use xcodeproj instead, I think it is a problem this doesnot adding files to correct target.

draveness commented 9 years ago

Thx for your help :+1: