From 495ab61c12aa8a4847dd4d828ec71acf8c678289 Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Mon, 19 Aug 2024 23:34:26 -0700 Subject: [PATCH] fix(Podfile): Avoid creating an empty Podfile on plugin install (#1467) Closes GH-1365. --- lib/Api.js | 127 +++++++++++++++++++++++++++-------------------------- 1 file changed, 64 insertions(+), 63 deletions(-) diff --git a/lib/Api.js b/lib/Api.js index 96e7c54c6..347810861 100644 --- a/lib/Api.js +++ b/lib/Api.js @@ -338,6 +338,10 @@ class Api { * @return {Promise} Return a promise */ addPodSpecs (plugin, podSpecs, installOptions) { + if (!podSpecs.length) { + return; + } + const project_dir = this.locations.root; const project_name = this.locations.xcodeCordovaProj.split(path.sep).pop(); const minDeploymentTarget = this.getPlatformInfo().projectConfig.getPreference('deployment-target', 'ios'); @@ -347,80 +351,77 @@ class Api { const podsjsonFile = new PodsJson(path.join(project_dir, PodsJson.FILENAME)); const podfileFile = new Podfile(path.join(project_dir, Podfile.FILENAME), project_name, minDeploymentTarget); - if (podSpecs.length) { - events.emit('verbose', 'Adding pods since the plugin contained '); - podSpecs.forEach(obj => { - // declarations - if (obj.declarations) { - Object.keys(obj.declarations).forEach(key => { - if (obj.declarations[key] === 'true') { - const declaration = Podfile.proofDeclaration(key); - const podJson = { - declaration - }; - const val = podsjsonFile.getDeclaration(declaration); - if (val) { - podsjsonFile.incrementDeclaration(declaration); - } else { - podJson.count = 1; - podsjsonFile.setJsonDeclaration(declaration, podJson); - podfileFile.addDeclaration(podJson.declaration); - } - } - }); - } - - // sources - if (obj.sources) { - Object.keys(obj.sources).forEach(key => { + events.emit('verbose', 'Adding pods since the plugin contained '); + podSpecs.forEach(obj => { + // declarations + if (obj.declarations) { + Object.keys(obj.declarations).forEach(key => { + if (obj.declarations[key] === 'true') { + const declaration = Podfile.proofDeclaration(key); const podJson = { - source: obj.sources[key].source + declaration }; - const val = podsjsonFile.getSource(key); + const val = podsjsonFile.getDeclaration(declaration); if (val) { - podsjsonFile.incrementSource(key); + podsjsonFile.incrementDeclaration(declaration); } else { podJson.count = 1; - podsjsonFile.setJsonSource(key, podJson); - podfileFile.addSource(podJson.source); + podsjsonFile.setJsonDeclaration(declaration, podJson); + podfileFile.addDeclaration(podJson.declaration); } - }); - } + } + }); + } - // libraries - if (obj.libraries) { - Object.keys(obj.libraries).forEach(key => { - let podJson = Object.assign({}, obj.libraries[key]); - podJson = replacePodSpecVariables(podJson, installOptions); - const val = podsjsonFile.getLibrary(key); - if (val) { - events.emit('warn', `${plugin.id} depends on ${podJson.name}, which may conflict with another plugin. ${podJson.name}@${val.spec} is already installed and was not overwritten.`); - podsjsonFile.incrementLibrary(key); - } else { - podJson.count = 1; - podsjsonFile.setJsonLibrary(key, podJson); - podfileFile.addSpec(podJson.name, podJson); - } - }); - } - }); + // sources + if (obj.sources) { + Object.keys(obj.sources).forEach(key => { + const podJson = { + source: obj.sources[key].source + }; + const val = podsjsonFile.getSource(key); + if (val) { + podsjsonFile.incrementSource(key); + } else { + podJson.count = 1; + podsjsonFile.setJsonSource(key, podJson); + podfileFile.addSource(podJson.source); + } + }); + } - // now that all the pods have been processed, write to pods.json - podsjsonFile.write(); + // libraries + if (obj.libraries) { + Object.keys(obj.libraries).forEach(key => { + let podJson = Object.assign({}, obj.libraries[key]); + podJson = replacePodSpecVariables(podJson, installOptions); + const val = podsjsonFile.getLibrary(key); + if (val) { + events.emit('warn', `${plugin.id} depends on ${podJson.name}, which may conflict with another plugin. ${podJson.name}@${val.spec} is already installed and was not overwritten.`); + podsjsonFile.incrementLibrary(key); + } else { + podJson.count = 1; + podsjsonFile.setJsonLibrary(key, podJson); + podfileFile.addSpec(podJson.name, podJson); + } + }); + } + }); - // only write and pod install if the Podfile changed - if (podfileFile.isDirty()) { - podfileFile.write(); - events.emit('verbose', 'Running `pod install` (to install plugins)'); - projectFile.purgeProjectFileCache(this.locations.root); + // now that all the pods have been processed, write to pods.json + podsjsonFile.write(); - return podfileFile.install(check_reqs.check_cocoapods) - .then(() => podsjsonFile.setSwiftVersionForCocoaPodsLibraries(this.root)); - } else { - events.emit('verbose', 'Podfile unchanged, skipping `pod install`'); - } + // only write and pod install if the Podfile changed + if (podfileFile.isDirty()) { + podfileFile.write(); + events.emit('verbose', 'Running `pod install` (to install plugins)'); + projectFile.purgeProjectFileCache(this.locations.root); + + return podfileFile.install(check_reqs.check_cocoapods) + .then(() => podsjsonFile.setSwiftVersionForCocoaPodsLibraries(this.root)); + } else { + events.emit('verbose', 'Podfile unchanged, skipping `pod install`'); } - return Promise.resolve(); } /**