apache / cordova-node-xcode

Apache cordova
Apache License 2.0
190 stars 97 forks source link

addBuildPhase keeps adding phases when they already exist #110

Open sitefinitysteve opened 4 years ago

sitefinitysteve commented 4 years ago

This is the build script line

xcodeProject.addBuildPhase(
              [], 'PBXShellScriptBuildPhase', 'Configure Crashlytics', undefined, options
            ).buildPhase;
            fs.writeFileSync(xcodeProjectPath, xcodeProject.writeSync());
            $logger.trace('Xcode project written');
          } else {
            $logger.error(xcodeProjectPath + ' is missing.');
            reject();
            return;
          }

So I guess everytime the CLI build runs it adds a NEW "Configure Crashlytics" to the build phase. Eventually my machine just outright died because the script was running hundreds of times.

Is there maybe a way before this I can check to see if it exists first before just adding it again?

AndrWeisR commented 3 years ago

I did it with this js script, called from a Cordova "after_build" hook:

var xcode = require('xcode'),
    fs = require('fs'),
    projectPath = 'platforms/ios/MyProj.xcodeproj/project.pbxproj',
    proj = xcode.project(projectPath);

proj.parse(function (err) {
        var scriptName = 'My Script';
        var buildPhases = proj.getPBXObject('PBXShellScriptBuildPhase');
        if (JSON.stringify(buildPhases).match(scriptName)) {
            console.log('Xcode project not updated - ' + scriptName + ' already exists')
        } else {
            var options = {shellPath: '/bin/sh', shellScript: 'bash somescript.sh'};
            proj.addBuildPhase([], 'PBXShellScriptBuildPhase', scriptName, proj.getFirstTarget().uuid, options);

            fs.writeFileSync(projectPath, proj.writeSync());
            console.log('Xcode project updated - added ' + scriptName);
        }
});