【问题标题】:Automatically add build phases in Cordova based Xcode project在基于 Cordova 的 Xcode 项目中自动添加构建阶段
【发布时间】:2017-06-05 14:43:13
【问题描述】:

我有一个基于 Cordova 的 iOS 项目,我需要在其中将自定义脚本添加到构建阶段。我有一个设置并且工作正常,但是我需要能够以某种方式自动添加构建阶段,因为我需要项目能够在 CI 服务器上自动安装和构建,而无需手动添加阶段Xcode。

为了澄清,当cordova platform add ios 运行时,项目是在没有构建阶段的情况下创建的,我需要在cordova build ios 之前(或期间)以编程方式添加一个构建阶段。

我可以在 .xcconfig 文件中添加自定义构建设置,是否有可以定义构建阶段的地方?我看到构建阶段存在于我的 .pbxproj 文件中,但是这是自动生成的并包含一些随机 ID,所以我不确定它是否可以解析并插入任意内容?

【问题讨论】:

    标签: ios xcode cordova


    【解决方案1】:

    我找到的解决方案是使用 Xcode 项目解析器,周围有一些。我用过cordova-node-xcode。我找不到任何 API 文档,但单元测试有助于弄清楚我需要什么。我需要使用proj.addBuildPhase 方法,在这里可以看到使用它添加运行脚本的示例:https://github.com/apache/cordova-node-xcode/blob/master/test/addBuildPhase.js#L113

    【讨论】:

    • 您上面链接的行现在是 130
    【解决方案2】:

    根据一个公认的答案,我创建了代码,也许有人可以使用它:)

    使用以下命令安装 xcode npm:

    npm i xcode
    

    在根目录下创建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;
      }
    }
    

    使用以下代码扩展 config.xml:

    <platform name="ios">
        ...
        <hook src="extend_build_phase.js" type="after_platform_add" />
        ...
    </platform>
    

    我创建了一个 Ionic 项目,为了使其工作,我使用以下命令:

    ionic cordova platform rm ios
    ionic cordova platform add ios
    ionic cordova build ios
    

    【讨论】:

      猜你喜欢
      • 2014-01-25
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      相关资源
      最近更新 更多