From 4e432e879b463c555ec3f9b951af8ab7e65b0735 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Sat, 27 Apr 2024 16:08:53 +0200 Subject: [PATCH] Update Package.swift on plugin add/remove --- lib/Api.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/Api.js b/lib/Api.js index 96e7c54c6..20ecc66af 100644 --- a/lib/Api.js +++ b/lib/Api.js @@ -279,6 +279,11 @@ class Api { return this.addPodSpecs(plugin, podSpecs, installOptions); } }) + .then(()=>{ + if (plugin != null && fs.existsSync(path.join(plugin.dir, 'Package.swift'))) { + return this.updatePackageSwift(plugin, "add"); + } + }) // CB-11022 Return truthy value to prevent running prepare after .then(() => true); } @@ -325,6 +330,11 @@ class Api { return this.removePodSpecs(plugin, podSpecs, uninstallOptions); } }) + .then(()=>{ + if (plugin != null && fs.existsSync(path.join(plugin.dir, 'Package.swift'))) { + return this.updatePackageSwift(plugin, "remove"); + } + }) // CB-11022 Return truthy value to prevent running prepare after .then(() => true); } @@ -512,6 +522,28 @@ class Api { return Promise.resolve(); } + /** + * Updates the Package.swift file to add or remove plugin dependencies + * @param {PluginInfo} plugin plugin to edit the Package.swift with + * @param {String} mode Add or remove plugin + */ + updatePackageSwift(plugin, mode) { + const project_dir = this.locations.root; + const pluginDep = ` +package.dependencies.append(.package(name: "${plugin.id}", path: "../../../plugins/${plugin.id}")) +package.targets.first?.dependencies.append(.product(name: "${plugin.id}", package: "${plugin.id}")) +`; + const packagePath = path.join(project_dir, 'CordovaPluginsSPM', 'Package.swift'); + let packageContent = fs.readFileSync(packagePath, 'utf8'); + if (mode === "add") { + packageContent = packageContent + pluginDep; + } else { + packageContent = packageContent.replace(pluginDep, ''); + } + fs.writeFileSync(packagePath, packageContent); + return Promise.resolve(); + } + /** * set Swift Version for all CocoaPods libraries *