[英]Automatically add build phases in Cordova based Xcode project
I have a Cordova based iOS project in which I need to add a custom script to the build phases. 我有一个基于Cordova的iOS项目,在其中需要向构建阶段添加自定义脚本。 I have one set up and it works fine, however I need to be able to add the build phase automatically somehow, as I need the project to be able to install and build automatically on a CI server, without having to manually add the phase in Xcode. 我已经设置好了,并且工作正常,但是我需要能够以某种方式自动添加构建阶段,因为我需要项目能够在CI服务器上自动安装和构建,而无需手动在其中添加阶段。 Xcode中。
To clarify, when cordova platform add ios
is run the project is created without build phases, I need to add a build phase in programmatically, before (or during) cordova build ios
. 为了澄清,在运行cordova platform add ios
时,创建的项目没有构建阶段,我需要以编程方式在cordova build ios
之前(或期间)添加构建阶段。
I can add custom build settings in the .xcconfig file, is there somewhere I can define build phases? 我可以在.xcconfig文件中添加自定义构建设置,是否可以在某个地方定义构建阶段? I see that the build phase is present in my .pbxproj file, however this is automatically generated and contains some random IDs so I am not sure its something I can just parse and insert arbitrary stuff into? 我看到构建阶段存在于我的.pbxproj文件中,但是它是自动生成的,并且包含一些随机ID,所以我不确定它可以解析并插入任意内容吗?
The solution I have found is to use an Xcode project parser, there are a few around. 我发现的解决方案是使用Xcode项目解析器,周围有一些解决方案。 I have used cordova-node-xcode
. 我用了cordova-node-xcode
。 I couldn't find any API documentation but the unit tests were helpful in figuring out what I needed. 我找不到任何API文档,但是单元测试有助于弄清楚我需要什么。 I needed to use the proj.addBuildPhase
method, an example of using this to add a run script can be seen here: https://github.com/apache/cordova-node-xcode/blob/master/test/addBuildPhase.js#L113 我需要使用proj.addBuildPhase
方法,可以在此处查看使用此方法添加运行脚本的示例: https : //github.com/apache/cordova-node-xcode/blob/master/test/addBuildPhase.js #L113
Based on an accepted answer i created the code maybe somebody can use it :) 基于一个可接受的答案,我创建了代码,也许有人可以使用它:)
Install xcode npm with: 使用以下命令安装xcode npm:
npm i xcode
Create extend_build_phase.js in root: 在root中创建extend_build_phase.js:
var xcode = require('xcode');
var fs = require('fs');
var path = require('path');
const xcodeProjPath = fromDir('platforms/ios', '.xcodeproj', false);
const projectPath = xcodeProjPath + '/project.pbxproj';
const myProj = xcode.project(projectPath);
// Here you can add your own shellScript
var options = { shellPath: '/bin/sh', shellScript: 'echo "hello world!"' };
myProj.parse(function(err) {
myProj.addBuildPhase([], 'PBXShellScriptBuildPhase', 'Run a script',myProj.getFirstTarget().uuid, options);
fs.writeFileSync(projectPath, myProj.writeSync());
})
function fromDir(startPath, filter, rec, multiple) {
if (!fs.existsSync(startPath)) {
console.log("no dir ", startPath);
return;
}
const files = fs.readdirSync(startPath);
var resultFiles = [];
for (var i = 0; i < files.length; i++) {
var filename = path.join(startPath, files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory() && rec) {
fromDir(filename, filter); //recurse
}
if (filename.indexOf(filter) >= 0) {
if (multiple) {
resultFiles.push(filename);
} else {
return filename;
}
}
}
if (multiple) {
return resultFiles;
}
}
Extend config.xml with the following code: 使用以下代码扩展config.xml:
<platform name="ios">
...
<hook src="extend_build_phase.js" type="after_platform_add" />
...
</platform>
I'm created an Ionic project so to make it work I use the following commands: 我创建了一个Ionic项目,因此要使其正常工作,请使用以下命令:
ionic cordova platform rm ios
ionic cordova platform add ios
ionic cordova build ios
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.