简体   繁体   English

在基于Cordova的Xcode项目中自动添加构建阶段

[英]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.

相关问题 如何在 xcode 中构建 cordova 项目? - How to build a cordova project in xcode? Xcode构建脚本(构建阶段 - &gt;运行脚本)基于用户名(用户)增加构建版本 - Xcode Build Script (Build Phases->Run Script) Increment Build Version based on Username(User) 为什么在XCode项目中看不到“构建阶段”选项卡? - Why can't I see my build phases tab in my XCode project? xcode 10.1找不到“构建阶段”选项卡 - xcode 10.1 Cannot find Build Phases Tab 如何使用嵌入式Cordova WebView将Cordova插件添加到Xcode项目中? - How to add Cordova plugins to Xcode project with embedded Cordova WebView? Cordova CLI“cordova build android”错误“似乎不是xcode项目,没有xcode项目文件......”? - Cordova CLI "cordova build android" error "Does not appear to be an xcode project, no xcode project file in..."? 为什么“ cordova build ios”不更新Xcode项目? - Why “cordova build ios” does not update Xcode project? 使用Cordova的iOS项目的Xcode 7.1和iOS 9.1的xcodebuild构建失败 - xcodebuild build failed with Xcode 7.1 and iOS 9.1 for iOS project with Cordova 如何在xcode构建阶段运行脚本中检查appledoc是否有效? - How to check appledoc valid or not in the xcode build phases run script? Xcode警告:当构建阶段有此文件时,没有规则来处理文件? - Xcode warning : no rule to Process file when build phases has this file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM