Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions cli/src/tasks/migrate-swiftui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,29 @@ export async function migrateToSwiftUI(config: Config): Promise<void> {
});

await runTask('Writing App.swift, CapacitorView.swift and SceneDelegate.swift.', async () => {
const storyboardPath = join(config.ios.nativeTargetDirAbs, 'Base.lproj', 'Main.storyboard');
const bridgeClass = existsSync(storyboardPath)
? getStoryboardBridgeSubclass(readFileSync(storyboardPath, 'utf-8'))
: null;
const capacitorView = bridgeClass ? useBridgeSubclass(assets.capacitorView, bridgeClass) : assets.capacitorView;
const files: [string, string][] = [
['App.swift', assets.app],
['CapacitorView.swift', assets.capacitorView],
['CapacitorView.swift', capacitorView],
];
for (const [name, contents] of files) {
if (!writeAppTargetFile(config, name, contents)) {
if (writeAppTargetFile(config, name, contents)) {
if (name === 'CapacitorView.swift' && bridgeClass) {
logger.info(
`CapacitorView.swift uses your ${bridgeClass} from Main.storyboard as the bridge view controller.`,
);
}
} else {
logger.warn(`${name} already exists, skipping.`);
if (name === 'CapacitorView.swift' && bridgeClass) {
logger.warn(
`Main.storyboard set ${bridgeClass} as the bridge view controller: make sure CapacitorView.swift creates it.`,
);
}
}
}

Expand Down Expand Up @@ -217,6 +233,26 @@ function writeSceneDelegate(path: string, contents: string): 'written' | 'replac
return 'replaced';
}

/**
* Main.storyboard is about to be removed, so a CAPBridgeViewController subclass set on its initial
* view controller (the documented way to register local plugins) would be silently dropped.
* Returns that subclass, or null when the storyboard uses CAPBridgeViewController itself.
*/
function getStoryboardBridgeSubclass(storyboard: string): string | null {
const initialId = storyboard.match(/\binitialViewController="([^"]+)"/)?.[1];
if (!initialId) return null;
const tag = [...storyboard.matchAll(/<viewController\b[^>]*>/g)]
.map((m) => m[0])
.find((t) => t.includes(` id="${initialId}"`));
const customClass = tag?.match(/\bcustomClass="([A-Za-z_][A-Za-z0-9_]*)"/)?.[1];
return customClass && customClass !== 'CAPBridgeViewController' ? customClass : null;
}

/** Point the template CapacitorView at the app's own bridge subclass. */
function useBridgeSubclass(capacitorView: string, className: string): string {
return capacitorView.replace(/\bCAPBridgeViewController\b/g, className);
}

/**
* Drop the storyboard the SwiftUI App struct replaces.
*
Expand Down Expand Up @@ -705,12 +741,14 @@ export const __testables = {
classify,
describeSignals,
ensureSupportedInterfaceOrientations,
getStoryboardBridgeSubclass,
hasCustomDelegateBody,
hasCustomWindowSetup,
isStockAppDelegate,
isStockSceneDelegate,
removeLeftoverStoryboard,
rewriteAppDelegateForAdaptor,
scanAndWarn,
useBridgeSubclass,
writeSceneDelegate,
};
19 changes: 19 additions & 0 deletions cli/test/fixtures/pre-swiftui-main.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14111" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
</dependencies>
<scenes>
<!--Bridge View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="CAPBridgeViewController" customModule="Capacitor" sceneMemberID="viewController"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
</document>
41 changes: 41 additions & 0 deletions cli/test/migrate-swiftui-storyboard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { readFileSync } from 'fs-extra';
import { resolve } from 'path';

import { __testables } from '../src/tasks/migrate-swiftui';

const { getStoryboardBridgeSubclass, useBridgeSubclass } = __testables;

// The 8.x template's Main.storyboard, which cap migrate removes.
const STOCK_STORYBOARD = readFileSync(resolve(__dirname, 'fixtures/pre-swiftui-main.storyboard'), 'utf-8');
const TEMPLATE_CAPACITOR_VIEW = readFileSync(
resolve(__dirname, '../../ios-spm-template/App/App/CapacitorView.swift'),
'utf-8',
);

describe('getStoryboardBridgeSubclass', () => {
it('returns null for the stock 8.x storyboard', () => {
expect(getStoryboardBridgeSubclass(STOCK_STORYBOARD)).toBeNull();
});

it('returns the custom class of the initial view controller', () => {
const custom = STOCK_STORYBOARD.replace(
'customClass="CAPBridgeViewController" customModule="Capacitor"',
'customClass="MyViewController" customModule="App" customModuleProvider="target"',
);
expect(getStoryboardBridgeSubclass(custom)).toBe('MyViewController');
});

it('returns null when there is no initial view controller', () => {
expect(getStoryboardBridgeSubclass('<document><viewController id="x" customClass="Other"/></document>')).toBeNull();
});
});

describe('useBridgeSubclass', () => {
it('makes the template CapacitorView create and type the subclass', () => {
const result = useBridgeSubclass(TEMPLATE_CAPACITOR_VIEW, 'MyViewController');
expect(result).toContain('-> MyViewController {');
expect(result).toContain(' MyViewController()');
expect(result).toContain('_ vc: MyViewController,');
expect(result).not.toContain('CAPBridgeViewController');
});
});