diff --git a/cli/src/util/capacitor-package.ts b/cli/src/util/capacitor-package.ts index 64829d313..6d2141abe 100644 --- a/cli/src/util/capacitor-package.ts +++ b/cli/src/util/capacitor-package.ts @@ -7,9 +7,17 @@ import { fatal } from '../errors'; import { convertToUnixPath } from './fs'; import { resolveNode } from './node'; -const SOURCE_PACKAGE_URL = 'https://github.com/ionic-team/capacitor'; -export const SOURCE_PACKAGE_IDENTITY = 'capacitor'; -const SOURCE_PACKAGE_NAME = new RegExp(`name:\\s*"${SOURCE_PACKAGE_IDENTITY}"`); +export const SOURCE_PACKAGE_IDENTITY = 'capacitor-ios'; + +/** Matches the source package's git URL, with or without `.git`, but not repos sharing its prefix. */ +const SOURCE_PACKAGE_URL = /url:\s*"https:\/\/github\.com\/ionic-team\/capacitor(?:\.git)?\/?"/i; + +/** + * Names a path dependency on Capacitor may carry: the current identity, and the one syncs before + * the `capacitor-ios` rename pinned. A plugin rewritten by such a sync keeps the old name until + * it is synced again. + */ +const PINNED_PACKAGE_NAMES = [SOURCE_PACKAGE_IDENTITY, 'capacitor']; /** Product providing the Cordova compatibility layer. */ export const CORDOVA_PRODUCT = 'CapacitorCordova'; @@ -97,9 +105,66 @@ export function renderCapacitorPackage(absolutePath: string, fromDir: string): s * copy of the framework. Rewriting keeps the whole graph on one identity. */ export function rewriteCapacitorDependency(content: string, absolutePath: string, fromDir: string): string { - const rewritten = replacePackageCalls(content, renderCapacitorPackage(absolutePath, fromDir)); + const capacitorCalls = findPackageCalls(content).filter((call) => referencesCapacitor(call.text)); + if (capacitorCalls.length === 0) { + return content; + } + + // Collected before the calls are replaced: once they are, every one reads `capacitor-ios` and + // the name each target's `.product(package:)` was written against is gone. The pinned names are + // included because a sync that renamed only the `.package(...)` entry left products on the old + // name behind. + const previousNames = new Set( + [...capacitorCalls.map((call) => packageReferenceName(call.text)), ...PINNED_PACKAGE_NAMES] + .filter((name): name is string => name !== undefined) + .map((name) => name.toLowerCase()) + .filter((name) => name !== SOURCE_PACKAGE_IDENTITY), + ); + + let rewritten = replacePackageCalls(content, renderCapacitorPackage(absolutePath, fromDir)); + + // A name some other dependency still answers to belongs to that dependency, not Capacitor. + const remainingNames = new Set( + findPackageCalls(rewritten) + .map((call) => packageReferenceName(call.text)?.toLowerCase()) + .filter((name): name is string => name !== undefined), + ); + + for (const name of previousNames) { + if (remainingNames.has(name)) { + continue; + } + rewritten = rewritten.replace( + new RegExp(`(package:\\s*)"${escapeRegExp(name)}"`, 'gi'), + `$1"${SOURCE_PACKAGE_IDENTITY}"`, + ); + } + + return rewritten; +} + +/** + * The name a `.product(package:)` uses to refer to the dependency a `.package(...)` call declares: + * its explicit `name:` when it pins one, otherwise the identity SPM derives from the last + * component of its URL or path. + */ +function packageReferenceName(call: string): string | undefined { + const explicit = call.match(/name:\s*"([^"]+)"/); + if (explicit) { + return explicit[1]; + } + + const location = call.match(/(?:url|path):\s*"([^"]+)"/); + if (!location) { + return undefined; + } + + const lastComponent = location[1].replace(/\/+$/, '').split('/').pop() ?? ''; + return lastComponent.replace(/\.git$/i, '').toLowerCase() || undefined; +} - return rewritten.split(`package: "${BINARY_PACKAGE_IDENTITY}"`).join(`package: "${SOURCE_PACKAGE_IDENTITY}"`); +function escapeRegExp(value: string): string { + return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } /** Reads the version an existing `Package.swift` pins Capacitor to, if it pins one. */ @@ -163,7 +228,12 @@ export function patchPackageSwiftDeploymentTarget( * the app, or the package it points at, has moved. */ function referencesCapacitor(call: string): boolean { - return call.includes(BINARY_PACKAGE_IDENTITY) || call.includes(SOURCE_PACKAGE_URL) || SOURCE_PACKAGE_NAME.test(call); + if (call.includes(BINARY_PACKAGE_IDENTITY) || SOURCE_PACKAGE_URL.test(call)) { + return true; + } + + const pinned = call.match(/name:\s*"([^"]+)"/); + return pinned !== null && PINNED_PACKAGE_NAMES.includes(pinned[1].toLowerCase()); } interface PackageCall { diff --git a/cli/test/capacitor-package.spec.ts b/cli/test/capacitor-package.spec.ts index 6a4f93690..2efb18193 100644 --- a/cli/test/capacitor-package.spec.ts +++ b/cli/test/capacitor-package.spec.ts @@ -39,7 +39,7 @@ describe('capacitor package resolution', () => { expect(path).toBe(installed); expect(renderCapacitorPackage(path, join(appRoot, 'ios', 'App', 'CapApp-SPM'))).toBe( - '.package(name: "capacitor", path: "../../../node_modules/@capacitor/ios")', + '.package(name: "capacitor-ios", path: "../../../node_modules/@capacitor/ios")', ); }); @@ -47,7 +47,7 @@ describe('capacitor package resolution', () => { const path = await resolveCapacitorPackagePath(makeConfig(appRoot)); expect(renderCapacitorPackage(path, join(appRoot, 'node_modules', '@capacitor', 'haptics'))).toBe( - '.package(name: "capacitor", path: "../ios")', + '.package(name: "capacitor-ios", path: "../ios")', ); }); @@ -79,7 +79,7 @@ describe('capacitor package resolution', () => { describe('rewriting an existing Package.swift', () => { const installed = '/app/node_modules/@capacitor/ios'; const pluginDir = '/app/node_modules/@capacitor/haptics'; - const rendered = '.package(name: "capacitor", path: "../ios")'; + const rendered = '.package(name: "capacitor-ios", path: "../ios")'; const rewrite = (content: string) => rewriteCapacitorDependency(content, installed, pluginDir); @@ -94,7 +94,7 @@ describe('rewriting an existing Package.swift', () => { const after = rewrite(before); expect(after).toContain(rendered); - expect(after).toContain('.product(name: "Capacitor", package: "capacitor")'); + expect(after).toContain('.product(name: "Capacitor", package: "capacitor-ios")'); expect(after).not.toContain('capacitor-swift-pm'); }); @@ -109,13 +109,101 @@ describe('rewriting an existing Package.swift', () => { const after = rewrite(before); expect(after).toContain(rendered); - expect(after).toContain('.product(name: "Capacitor", package: "capacitor")'); + expect(after).toContain('.product(name: "Capacitor", package: "capacitor-ios")'); }); it('repoints a path left behind by an earlier sync', () => { expect(rewrite('.package(name: "capacitor", path: "../../../somewhere/else")')).toBe(rendered); }); + it('renames every product of a plugin on the source package, including Cordova', () => { + const before = `dependencies: [ + .package(url: "https://github.com/ionic-team/capacitor.git", from: "9.0.0-beta.0") + ], + targets: [ + .target( + name: "Plugin", + dependencies: [ + .product(name: "Capacitor", package: "capacitor"), + .product(name: "Cordova", package: "capacitor") + ]) + ]`; + + const after = rewrite(before); + + expect(after).toContain(rendered); + expect(occurrences(after, 'package: "capacitor-ios"')).toBe(2); + expect(after).not.toContain('package: "capacitor"'); + }); + + it('renames the products of a plugin rewritten by a sync from before the capacitor-ios rename', () => { + const before = `dependencies: [ + .package(name: "capacitor", path: "../ios") + ], + targets: [ + .target(name: "Plugin", dependencies: [.product(name: "Capacitor", package: "capacitor")]) + ]`; + + const after = rewrite(before); + + expect(after).toContain(rendered); + expect(after).toContain('.product(name: "Capacitor", package: "capacitor-ios")'); + }); + + it('heals a plugin whose dependency was renamed but whose products were not', () => { + const before = `dependencies: [ + .package(name: "capacitor-ios", path: "../ios") + ], + targets: [ + .target(name: "Plugin", dependencies: [.product(name: "Capacitor", package: "capacitor")]) + ]`; + + expect(rewrite(before)).toContain('.product(name: "Capacitor", package: "capacitor-ios")'); + }); + + it('does not heal products that belong to another dependency identified as "capacitor"', () => { + const before = `dependencies: [ + .package(name: "capacitor-ios", path: "../ios"), + .package(path: "../../@sentry/capacitor") + ], + targets: [ + .target(name: "Plugin", dependencies: [.product(name: "SentryCapacitor", package: "capacitor")]) + ]`; + + expect(rewrite(before)).toContain('.product(name: "SentryCapacitor", package: "capacitor")'); + }); + + it('leaves products of a similarly named dependency alone', () => { + const before = `dependencies: [ + .package(url: "https://github.com/ionic-team/capacitor", from: "9.0.0"), + .package(name: "capacitor-plugin-foo", path: "../foo") + ], + targets: [ + .target(name: "Plugin", dependencies: [ + .product(name: "Capacitor", package: "capacitor"), + .product(name: "Foo", package: "capacitor-plugin-foo") + ]) + ]`; + + const after = rewrite(before); + + expect(after).toContain('.package(name: "capacitor-plugin-foo", path: "../foo")'); + expect(after).toContain('.product(name: "Foo", package: "capacitor-plugin-foo")'); + expect(after).toContain('.product(name: "Capacitor", package: "capacitor-ios")'); + }); + + it('leaves repositories that only share the source package URL prefix alone', () => { + const before = '.package(url: "https://github.com/ionic-team/capacitor-plugins.git", from: "1.0.0")'; + + expect(rewrite(before)).toBe(before); + }); + + it('leaves path dependencies inside node_modules/@capacitor alone', () => { + const before = '.package(name: "CapacitorHaptics", path: "../../node_modules/@capacitor/haptics")'; + + expect(rewrite(before)).toBe(before); + }); + it('leaves a similarly named plugin package alone', () => { const before = '.package(name: "capacitor-plugin-foo", path: "../foo")'; diff --git a/ios-spm-template/App/CapApp-SPM/Package.swift b/ios-spm-template/App/CapApp-SPM/Package.swift index 6b96e14ed..88aab5db3 100644 --- a/ios-spm-template/App/CapApp-SPM/Package.swift +++ b/ios-spm-template/App/CapApp-SPM/Package.swift @@ -11,13 +11,13 @@ let package = Package( targets: ["CapApp-SPM"]) ], dependencies: [ - .package(name: "capacitor", path: "../../../node_modules/@capacitor/ios") + .package(name: "capacitor-ios", path: "../../../node_modules/@capacitor/ios") ], targets: [ .target( name: "CapApp-SPM", dependencies: [ - .product(name: "Capacitor", package: "capacitor") + .product(name: "Capacitor", package: "capacitor-ios") ] ) ]