Skip to content

Commit

Permalink
feat(spm): Handle plugins with npm namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dpogue committed Jan 21, 2025
1 parent b258ad9 commit d02c9ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
25 changes: 14 additions & 11 deletions lib/SwiftPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,37 @@ class SwiftPackage {
}
}

_pluginReference (plugin, opts = {}) {
let pluginPath = path.relative(path.dirname(this.path), path.join(this.root, 'packages', plugin.id));
if (opts.link) {
pluginPath = path.relative(path.dirname(this.path), plugin.dir);
}

_pluginReference (plugin, pluginPath) {
return `
package.dependencies.append(.package(name: "${plugin.id}", path: "${pluginPath.replaceAll(path.sep, path.posix.sep)}"))
package.targets.first?.dependencies.append(.product(name: "${plugin.id}", package: "${plugin.id}"))
`;
}

addPlugin (plugin, opts = {}) {
if (!opts.link) {
let pluginPath = path.relative(path.dirname(this.path), path.join(this.root, 'packages', plugin.id));
if (opts.link) {
pluginPath = path.relative(path.dirname(this.path), plugin.dir);
} else {
// Copy the plugin into the packages directory
fs.cpSync(plugin.dir, path.join(this.root, 'packages', plugin.id), { recursive: true });

const pkgSwiftPath = path.join(this.root, 'packages', plugin.id, 'Package.swift');
const pkg_fd = fs.openSync(pkgSwiftPath, 'r+');

const cordovaPath = path.relative(pluginPath, path.join(this.root, 'packages', 'cordova-ios'));
// TODO: Detect if cordova-ios was linked and use that path

let packageContent = fs.readFileSync(pkg_fd, 'utf8');
packageContent = packageContent.replace(/package\(.+cordova-ios.+\)/gm, 'package(name: "cordova-ios", path: "../cordova-ios")');
packageContent = packageContent.replace(/package\(.+cordova-ios.+\)/gm, `package(name: "cordova-ios", path: "${cordovaPath.replaceAll(path.sep, path.posix.sep)}")`);

fs.ftruncateSync(pkg_fd);
fs.writeSync(pkg_fd, packageContent, 0, 'utf8');
fs.closeSync(pkg_fd);
}

const fd = fs.openSync(this.path, 'a');
fs.writeFileSync(fd, this._pluginReference(plugin, opts), 'utf8');
fs.writeFileSync(fd, this._pluginReference(plugin, pluginPath), 'utf8');
fs.closeSync(fd);
}

Expand All @@ -74,8 +75,10 @@ package.targets.first?.dependencies.append(.product(name: "${plugin.id}", packag
let packageContent = fs.readFileSync(fd, 'utf8');

// We don't know if it was originally linked or not, so try to remove both
packageContent = packageContent.replace(this._pluginReference(plugin), '');
packageContent = packageContent.replace(this._pluginReference(plugin, { link: true }), '');
const pluginPath = path.relative(path.dirname(this.path), path.join(this.root, 'packages', plugin.id));
const pluginLink = path.relative(path.dirname(this.path), plugin.dir);
packageContent = packageContent.replace(this._pluginReference(plugin, pluginPath), '');
packageContent = packageContent.replace(this._pluginReference(plugin, pluginLink), '');

fs.ftruncateSync(fd);
fs.writeSync(fd, packageContent, 0, 'utf8');
Expand Down
22 changes: 21 additions & 1 deletion tests/spec/unit/SwiftPackage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ describe('SwiftPackage', () => {
dir: path.join(__dirname, 'fixtures', 'org.test.plugins.swiftpackageplugin')
};

const namespaced_plugin = {
id: '@username/my-plugin',
dir: path.join(__dirname, 'fixtures', 'org.test.plugins.swiftpackageplugin')
};

let pkg;
beforeEach(() => {
fs.mkdirSync(path.join(tmpDir.name, 'packages', 'cordova-ios-plugins'), { recursive: true });
Expand All @@ -72,6 +77,21 @@ describe('SwiftPackage', () => {
expect(fs.existsSync(path.join(tmpDir.name, 'packages', 'my-plugin'))).toBeTruthy();
});

it('should add namespaced plugin references to the package file', () => {
pkg.addPlugin(namespaced_plugin);

const pkgPath = path.join(tmpDir.name, 'packages', 'cordova-ios-plugins', 'Package.swift');
const content = fs.readFileSync(pkgPath, 'utf8');
expect(content).toContain('.package(name: "@username/my-plugin", path: "../@username/my-plugin")');
expect(content).toContain('.product(name: "@username/my-plugin", package: "@username/my-plugin")');
});

it('should copy the namespaced plugin into the packages directory', () => {
pkg.addPlugin(namespaced_plugin);

expect(fs.existsSync(path.join(tmpDir.name, 'packages', '@username', 'my-plugin'))).toBeTruthy();
});

it('should add plugin references to the package file when linked', () => {
pkg.addPlugin(my_plugin, { link: true });

Expand Down Expand Up @@ -103,7 +123,7 @@ describe('SwiftPackage', () => {
fs.writeFileSync(pkgPath, fixturePackage, 'utf8');

pkg = new SwiftPackage(tmpDir.name);
fs.writeFileSync(pkgPath, fixturePackage + pkg._pluginReference(my_plugin), 'utf8');
fs.writeFileSync(pkgPath, fixturePackage + pkg._pluginReference(my_plugin, '../my-plugin'), 'utf8');
});

it('should remove plugin references to the package file', () => {
Expand Down

0 comments on commit d02c9ab

Please sign in to comment.