kronenthaler / mod-pbxproj

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

Adding files to variants #113

Open st3fan opened 7 years ago

st3fan commented 7 years ago

(I know this is not really a bug report, but I don't know where else to ask questions - I'm happy to convert this into a wiki page as an example usage though)

Hello and thank you for maintaining this project. At Mozilla we are trying to use this for the following use case:

We have a product called Focus at https://github.com/mozilla-mobile/focus which currently is English only.

We localize the product by exporting an en.XLIFF file to our localization volunteers and we get back XLIFF files for other locales. To import these we run xcodebuild -importLocalizations -localizationPath l10n/nl.xliff for example. This spits out .strings files in the proper .lproj directories.

The problem is that xcodebuild only generates the files. It does not actually add them to the project.

This is where pbxproj comes in. I'd like to use it to find for example the PBXVariantGroup for Localizable.strings and then add a PBXFileReference reference for it.lproj/Localizable.strings to it.

However, we are lost in the complexity of both the Xcode projects and the pbxproj API.

My question to you is if we can get some help to find out how to do this. I'm happy to turn the end result in some reusable code that anyoen can use. Or a blog post or a wiki page.

🧀

kronenthaler commented 7 years ago

Hello @st3fan, Sounds like an interesting case that i never have dealt with. Xcode project format doesn't have an official spec, so the way pbxproj has been built is by reverse engineering the format. What i usually do when a new case appear is the following:

  1. create the new xcode project, the simplest you can find (for iOS, single view application is what i use the most)
  2. save the project.pbxproj somewhere to diff it after the change
  3. do the change you want achieve in Xcode manually.
  4. compare the saved project.pbxproj with the one after your changes.

Based on that sequence, i can determine what new sections were created and how they are linked to one another.

The PBXVariantGroup it's not defined and probably will have some special logic that have to be dealt with.

I'm more than happy to do this for you guys if you send over the project.pbxproj after the changes have been done manually. :)

st3fan commented 7 years ago

Awesome. We really appreciate the help.

This is what I did:

Attached are the projects files before and after. The -importLocalizations did not result in any project changes. Just the files being updated.

Git status:

On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Blockzilla/it.lproj/InfoPlist.strings
    new file:   Blockzilla/it.lproj/Localizable.strings

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   Blockzilla.xcodeproj/project.pbxproj
    modified:   Blockzilla/it.lproj/InfoPlist.strings
    modified:   Blockzilla/it.lproj/Localizable.strings

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    ContentBlocker/it.lproj/
    XCUITest/it.lproj/

Diff of the project file:

diff -Bub ~/Desktop/Focus1BeforeAddingLocale.pbxproj  ~/Desktop/Focus2AfterAddingLocale.pbxproj
--- /Users/sarentz/Desktop/Focus1BeforeAddingLocale.pbxproj 2017-01-09 12:59:01.000000000 -0500
+++ /Users/sarentz/Desktop/Focus2AfterAddingLocale.pbxproj  2017-01-09 13:00:06.000000000 -0500
@@ -241,6 +241,8 @@
        E47F9AE91DB9329D00A93285 /* Adjust-Focus.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Adjust-Focus.plist"; sourceTree = "<group>"; };
        E47F9AEB1DB9333200A93285 /* Adjust-Klar.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Adjust-Klar.plist"; sourceTree = "<group>"; };
        E47F9AED1DB9338600A93285 /* AdjustIntegration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AdjustIntegration.swift; sourceTree = "<group>"; };
+       E4BCCD041E24071800BBAAA7 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/InfoPlist.strings; sourceTree = "<group>"; };
+       E4BCCD051E24071800BBAAA7 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/Localizable.strings; sourceTree = "<group>"; };
        E4BF2DD31BACE8CA00DA9D68 /* Klar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; name = Klar.app; path = "Firefox Klar.app"; sourceTree = BUILT_PRODUCTS_DIR; };
        E4BF2DD61BACE8CA00DA9D68 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
        E4BF2DDD1BACE8CA00DA9D68 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@@ -603,6 +605,7 @@
                en,
                Base,
                de,
+               it,
            );
            mainGroup = E4BF2DCA1BACE8CA00DA9D68;
            productRefGroup = E4BF2DD41BACE8CA00DA9D68 /* Products */;
@@ -807,6 +810,7 @@
            children = (
                D343DCC31C44356500D7EEE8 /* de */,
                D362D26A1C47326A0041980A /* en */,
+               E4BCCD051E24071800BBAAA7 /* it */,
            );
            name = Localizable.strings;
            sourceTree = "<group>";
@@ -832,6 +836,7 @@
            children = (
                E40AFC791DDDE98300DA5651 /* en */,
                D3C70C671DDE795300CEE458 /* de */,
+               E4BCCD041E24071800BBAAA7 /* it */,
            );
            name = InfoPlist.strings;
            sourceTree = "<group>";

Zip file containing both project files:

AddingLocaleToProject.zip

kronenthaler commented 7 years ago

Thanks for the files, i took a look at the way Xcode imports the files, and i saw some funny behaviors when importing the .xliff file with the Editor > Import localization menu.

Note to self: so far, this is what i manage to gather:

I will try to tackle this this week/weekend.

kronenthaler commented 7 years ago

@st3fan i have some proposed changes #116 , can you verify it works for what you need?

Rex-Appier commented 3 years ago

PBXVariantGroup

Is this feature done or included in current stable build