简体   繁体   English

通过命令行将文件添加到 Xcode 项目? 在 Xcode 中使用 project.pbxproj 文件?

[英]Add files to Xcode project through command line ? Use of project.pbxproj file in Xcode?

I was trying to add a plist file to a xcode project through command line, some of the blogs suggested to edit the project.pbxproj file.我试图通过命令行将 plist 文件添加到 xcode 项目中,一些博客建议编辑project.pbxproj文件。 I searched about the project.pbxproj file but was not able to get much information about it.我搜索了project.pbxproj文件,但无法获得太多相关信息。 Can anyone let me know what is use of the project.pbxproj file in xcode?谁能让我知道 xcode 中project.pbxproj文件的用途? How does one add entries to it?如何向其中添加条目?

I am using this repo to work with it.我正在使用这个 repo来处理它。

the script that I wrote is as follows:我写的脚本如下:

import sys
import os
from mod_pbxproj import XcodeProject


def addPlistInProject(corodova_proj_name, xcode_proj_name, plist_file_name):
    print "Cordova project name : " + corodova_proj_name
    present_directory = os.getcwd()
    path_to_xcode_proj = present_directory + '/' + corodova_proj_name + '/platforms/ios/' + xcode_proj_name + '.xcodeproj/project.pbxproj'
    print "Xcode Project Path : " + path_to_xcode_proj
    project = XcodeProject.Load(path_to_xcode_proj)
    project.get_or_create_group('new group')
    project.add_file(plist_file_name)


if __name__ == "__main__":
    corodova_proj_name = sys.argv[1]
    xcode_proj_name = sys.argv[2]
    plist_file_name = sys.argv[3]
    print "Xcode Project Name = : " + xcode_proj_name
    print "Plist File Path = : " + plist_file_name
    addPlistInProject(corodova_proj_name, xcode_proj_name, plist_file_name)

I will be invoking the script as:我将调用脚本为:

python myscript.py hello HelloWorld manisha-rules_camdo.plist

myscript.py is the script I wrote, hello is the existing cordova project and HelloWorld is the Xcode project created by using cordova platform add iOS . myscript.py是我写的脚本, hello是现有的cordova 项目, HelloWorld是使用cordova platform add iOS创建的Xcode 项目。

The command Sequence I will be following will be as follows:我将遵循的命令序列如下:

cordova create hello com.example.hello HelloWorld
cordova platform add iOS
py myscript.py hello HelloWorld manisha-rules_camdo.plist

Where hello is the name of cordova project and HelloWorld name of iOS target.其中hello是cordova 项目的名称和iOS 目标的HelloWorld名称。

There is a Ruby API from Cocoapods for editing Xcode projects. Cocoapods 提供了一个用于编辑 Xcode 项目的 Ruby API。 It also have an active community of developers:它还拥有活跃的开发人员社区:

https://github.com/CocoaPods/Xcodeproj https://github.com/CocoaPods/Xcodeproj

Another great option, especially for Cordova projects, is to use the XCODE node module: node-xcode ;另一个很好的选择,特别是对于 Cordova 项目,是使用 XCODE 节点模块: node-xcode you can easily add it via NPM.您可以通过 NPM 轻松添加它。

Once in place, you can create an after_prepare hook to modify the pbxproj, injecting custom source files, additional frameworks, etc, on every build.一旦到位,您可以创建一个after_prepare钩子来修改 pbxproj,在每次构建时注入自定义源文件、附加框架等。 In fact, Cordova itself leverages this module during its own build processes.事实上,Cordova 本身在它自己的构建过程中利用了这个模块。

Within my solution, I first added the module via npm:在我的解决方案中,我首先通过 npm 添加了模块:

npm install xcode --save-dev

And then I created and after_prepare hook to add extra frameworks into my XCode project:然后我创建了 after_prepare 钩子来将额外的框架添加到我的 XCode 项目中:

var xcode = require('xcode'),
    fs = require('fs'),
    rootdir = process.argv[2],
    projectPath = rootdir + '/platforms/ios/my-project/project.pbxproj',
    proj = new xcode.project(projectPath);

proj.parse(function(err) {
    if (err) {
        console.log("Oh noes! XCODE project failed to parse:");
        console.log(err);
    } else {
        proj.addFramework('Fabric.framework', {customFramework:true});
        proj.addFramework('Crashlytics.framework', {customFramework:true});
        proj.addFramework('AdSupport.framework');
        proj.addFramework('FacebookSDK.framework', {customFramework:true});

        fs.writeFileSync(projectPath, proj.writeSync());
        console.log("Updated XCODE project with references to social libs!");
    }
});

The XCODE module is smart enough to know if the frameworks / files / etc that you are trying to add are already present, and won't try to add them again. XCODE 模块足够聪明,可以知道您尝试添加的框架/文件/等是否已经存在,并且不会尝试再次添加它们。

What you are asking to do is not the most straightforward thing.你要求做的不是最直接的事情。 The Xcode pbxproj file format looks like XML, but I think there's quite a few proprietary / undocumented pieces to it (much like everything iOS). Xcode pbxproj 文件格式看起来像 XML,但我认为它有很多专有/未记录的部分(很像 iOS 的所有内容)。 As far as I can tell, Xcode doesn't have any way to add files from the command line.据我所知,Xcode 无法从命令行添加文件。

I did find a Python script that you might be able to use to modify Xcode's project files, but it's a few years old and it might be out of date.我确实找到了一个 Python 脚本,您可以使用它来修改 Xcode 的项目文件,但它已经有几年了,而且可能已经过时了。

Here is the Blog post that talks about it and here is the current GitHub repo (last updated five months ago, as of the date of me typing this answer).这是讨论它的博客文章,这是当前的 GitHub 存储库(上次更新是五个月前,截至我输入此答案的日期)。

Give this a try and let me know if it works for you.试试这个,让我知道它是否适合你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM