This is an automated email from the ASF dual-hosted git repository.

jcesarmobile pushed a commit to branch SPM-plugins
in repository https://gitbox.apache.org/repos/asf/cordova-ios.git

commit 4e432e879b463c555ec3f9b951af8ab7e65b0735
Author: jcesarmobile <jcesarmob...@gmail.com>
AuthorDate: Sat Apr 27 16:08:53 2024 +0200

    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 96e7c54c..20ecc66a 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
      *


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org
For additional commands, e-mail: commits-h...@cordova.apache.org

Reply via email to