xcode 读取/写入 pbxproj/xcodeproj 文件的库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1452707/
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-14 18:51:25  来源:igfitidea点击:

Library to read/write pbxproj/xcodeproj files?

javaobjective-crubyxcodemacos

提问by JP Richardson

Does anyone know of a library to read/write the Xcode project files .xcodeproj/.pbxproj? Any language is welcome.

有谁知道读/写 Xcode 项目文件.xcodeproj/.pbxproj 的库吗?欢迎任何语言。

Thanks in advance.

提前致谢。

回答by cdespinosa

The surface syntax of an Xcode project is an "old-style plist." You can easily convert it to an XML plist with the command

Xcode 项目的表面语法是“旧式 plist”。您可以使用以下命令轻松将其转换为 XML plist

plutil -convert xml1 -o - myproj.xcodeproj/project.pbxproj

Note this is not "real XML" but the Mac OS X plist structure expressed in XML syntax; it consists almost entirely of key-value pair dictionaries and arrays. Xcode will read the XML representation but convert it back to "old-style plist" when the project is opened.

请注意,这不是“真正的 XML”,而是以 XML 语法表示的 Mac OS X plist 结构;它几乎完全由键值对字典和数组组成。Xcode 将读取 XML 表示,但在项目打开时将其转换回“旧式 plist”。

The structure and relationship of the items in the plist follow the structure of the project. The UUIDs are used to cross-reference items between the project and its targets, and between the project and the user files in the project wrapper.

plist 中各项的结构和关系遵循项目的结构。UUID 用于在项目与其目标之间以及项目与项目包装器中的用户文件之间交叉引用项。

The 'isa' key identifies each kind of object. The PBXProject contains PBXFileReference, PBXGroup, PBXNativeTarget, and PBXBuildConfiguration objects.

“isa”键标识每种对象。PBXProject 包含 PBXFileReference、PBXGroup、PBXNativeTarget 和 PBXBuildConfiguration 对象。

The targets have PBXBuildPhase objects that contain cross-references to the file references; BuildConfigurationLists that store the build settings for the targets, and other target settings like the target type and name.

目标具有包含对文件引用的交叉引用的 PBXBuildPhase 对象;BuildConfigurationLists 存储目标的构建设置,以及其他目标设置,如目标类型和名称。

The buildConfigurationLists cross-reference buildConfigurations, which in turn contain dictionaries of buildSettings.

buildConfigurationLists 交叉引用 buildConfigurations,后者又包含 buildSettings 的字典。

I'd recommend looking at the old-style plist text first, as it's much more readable and actually has inline comments to tell you what's what. Then you can use XML tools to edit or write the project files to your liking.

我建议先查看旧式 plist 文本,因为它更具可读性,并且实际上具有内联注释来告诉您什么是什么。然后,您可以使用 XML 工具根据自己的喜好编辑或编写项目文件。

回答by Jasper Blues

I ended up creating one, and it's hosted on github here:

我最终创建了一个,它托管在 github 上:

https://github.com/jasperblues/XcodeEditor

https://github.com/jasperblues/XcodeEditor

Allows listing headers, files, adding source files, setting source file as a member of a target, adding images, creating groups, etc).

允许列出标题、文件、添加源文件、将源文件设置为目标的成员、添加图像、创建组等)。

It works by manipulating the contents of the project.pbxproj file. Used in:

它通过操作 project.pbxproj 文件的内容来工作。用于:

  • Less Painful: Automated functional testing for mobile applications. (Edit: now part of Xamarin's tools).
  • Level Helper: A RAD framework for developing 2D games on iOS & Android.
  • TextMate
  • Apportable
  • 不那么痛苦:移动应用程序的自动化功能测试。(编辑:现在是 Xamarin 工具的一部分)。
  • Level Helper:用于在 iOS 和 Android 上开发 2D 游戏的 RAD 框架。
  • 文字伴侣
  • 便携

回答by JP Richardson

I just discovered this: https://github.com/alunny/node-xcodeI haven't used it yet, but it looks promising for Node.js developers.

我刚刚发现了这个:https: //github.com/alunny/node-xcode我还没有使用它,但它对 Node.js 开发人员来说看起来很有希望。

回答by JosephH

CocoaPods now have a ruby library to manipulate pbxproj files:

CocoaPods 现在有一个 ruby​​ 库来操作 pbxproj 文件:

https://github.com/CocoaPods/Xcodeproj

https://github.com/CocoaPods/Xcodeproj

As well as being used by CocoaPods, this is also used by fastlane, synx, slatherand no doubt many more.

除了正在使用的CocoaPods,这也被FASTLANEsynx涂满无疑更多。

回答by Alexander

Try this one: https://github.com/sap-production/XcodeProjectJavaAPI

试试这个:https: //github.com/sap-production/XcodeProjectJavaAPI

It can read and write any Property List file and contains a semantic model to read and manipulate Xcode Project files. The semantic model is in an early stage but can be easily extended.

它可以读写任何属性列表文件,并包含一个语义模型来读取和操作 Xcode 项目文件。语义模型处于早期阶段,但可以轻松扩展。

It is also used heavily in the Xcode Maven plugin of SAP, which will be released this month.

它也在本月发布的 SAP Xcode Maven 插件中大量使用。

回答by Sandy Chapman

There's a nice Python package for doing this. The documentation isn't great, but the source code is pretty organized and you can find what you need.

有一个很好的 Python 包可以做到这一点。文档不是很好,但源代码非常有条理,您可以找到所需的内容。

https://github.com/kronenthaler/mod-pbxproj

https://github.com/kronenthaler/mod-pbxproj

回答by user1122069

For PHP (easily ported to whatever language you need), very basic write only:

对于 PHP(很容易移植到你需要的任何语言),非常基本的只写:

function modifyPlistXCodeProject($file_path,$app_name,$bundle_identifier,$latest_version,$bundle_version)
{
    $data = file_get_contents($file_path);
    $changes = Array();
    $changes['PRODUCT_BUNDLE_IDENTIFIER'] = $bundle_identifier;
    foreach ($changes as $key => $value){
        $data = oldStylePlistReplace($data,$key,$value);
    }
    file_put_contents($file_path, $data);
}

function oldStylePlistReplace($data,$key,$value){
    $e = explode($key,$data);
    for ($i=1;$i<count($e);$i++){
        $row_i = $e[$i];
        $nextline_pos = stripos($row_i,"\n");
        $text_i = '= '.$value.';'.substr($row_i, $nextline_pos);
        $e[$i] = $text_i;
        //var_dump(substr($text_i,0,100));exit();
    }
    return implode($key,$e);
}