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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26518592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:57:51  来源:igfitidea点击:

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

pythonxcode

提问by Nilesh Agrawal

I was trying to add a plist file to a xcode project through command line, some of the blogs suggested to edit the project.pbxprojfile. I searched about the project.pbxprojfile but was not able to get much information about it. Can anyone let me know what is use of the project.pbxprojfile in xcode? How does one add entries to it?

我试图通过命令行将 plist 文件添加到 xcode 项目中,一些博客建议编辑该project.pbxproj文件。我搜索了该project.pbxproj文件,但无法获得太多相关信息。谁能让我知道project.pbxprojxcode中的文件有什么用?如何向其中添加条目?

I am using this repoto 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.pyis the script I wrote, hellois the existing cordova project and HelloWorldis the Xcode project created by using cordova platform add iOS.

myscript.py是我写的脚本,hello是现有的cordova项目,HelloWorld是使用cordova platform add iOS.

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 hellois the name of cordova project and HelloWorldname of iOS target.

hellocordova 项目的HelloWorld名称和 iOS 目标的名称在哪里。

采纳答案by brunobowden

There is a Ruby API from Cocoapods for editing Xcode projects. It also have an active community of developers:

Cocoapods 提供了一个用于编辑 Xcode 项目的 Ruby API。它还拥有活跃的开发人员社区:

https://github.com/CocoaPods/Xcodeproj

https://github.com/CocoaPods/Xcodeproj

回答by RMD

Another great option, especially for Cordova projects, is to use the XCODE node module: node-xcode; you can easily add it via NPM.

另一个很好的选择,特别是对于 Cordova 项目,是使用 XCODE 节点模块:node-xcode;您可以通过 NPM 轻松添加它。

Once in place, you can create an after_preparehook to modify the pbxproj, injecting custom source files, additional frameworks, etc, on every build. In fact, Cordova itself leverages this module during its own build processes.

一旦到位,您可以创建一个after_prepare钩子来修改 pbxproj,在每次构建时注入自定义源文件、附加框架等。事实上,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 模块足够聪明,可以知道您尝试添加的框架/文件/等是否已经存在,并且不会尝试再次添加它们。

回答by Michael Dautermann

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). As far as I can tell, Xcode doesn't have any way to add files from the command line.

你要求做的不是最直接的事情。Xcode pbxproj 文件格式看起来像 XML,但我认为它有很多专有/未记录的部分(很像 iOS 的所有内容)。据我所知,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 itand 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.

试试这个,让我知道它是否适合你。