cordova-node-xcode
cordova-node-xcode copied to clipboard
Feature Request: Overwrite an existent build phase script
As the title says. Is there a function or a workaround to overwrite an existant build phase script?
As far as is know is not posible with this package to remove an existant build phase, so as an alternative i want to overwrite some of the script for already added build phases.
Thanks in advance :)
After parsing the pbx file, the entire file is represented as a large JSON structure.
So if you want to modify/manipulate it, you can traverse the data structure to find the build phase you're looking for. Once you have the build phase object, should be able to reference the id for other API calls. (If there is any, I'm not too familiar with the API myself)
If you want to simply remove it, it does seem there is no delete
counterpart of https://github.com/apache/cordova-node-xcode/blob/659a4e7cfe945a13b3968c0d8c5439683f6d0932/lib/pbxProject.js#L872
But likewise, as a stopgap, the data structure could be directly manipulated, but you'll have to clean all the references of the build phase from the data structure. I think you can reference the addBuildPhase
API to see where these references occurs. I think a PR would be welcome for a deleteBuildPhase
API.
Well, this is how i solved. I delete the build phase with unwanted script and then add new build phase with wanted script. I think could be improved but is doing the job for now..
var fs = require('fs');
const xcodeProjPath = 'platforms/ios/T-Play.xcodeproj';
module.exports = function (ctx) {
console.log("Build phase sentry");
const projectPath = xcodeProjPath + '/project.pbxproj';
const myProj = xcode.project(projectPath);
myProj.parse(function(err) {
let keyToReplace = '';
var buildPhases = myProj.hash.project.objects['PBXShellScriptBuildPhase'];
for (var key in buildPhases) {
//Find build phase by name
if (buildPhases[key].name?.includes('strip unused archs')) {
keyToReplace = key;
delete myProj.hash.project.objects['PBXShellScriptBuildPhase'][keyToReplace];
fs.writeFileSync(projectPath, myProj.writeSync());
}
}
var options = { shellPath: '/bin/sh', shellScript: '' };
//Read the new script from file
fs.readFile('./hooks/buildPhaseScript.sh','utf8', function(err, data){
if (err) {
console.error(err);
return;
}
// Display the file content
options.shellScript = data;
myProj.addBuildPhase([], 'PBXShellScriptBuildPhase', 'Sentry strip unused archs', myProj.getFirstTarget().uuid, options);
fs.writeFileSync(projectPath, myProj.writeSync());
});
});
}