Skip to content

Commit

Permalink
Merge pull request brandingbrand#2657 from crherman7/feat/ios_deep_li…
Browse files Browse the repository at this point in the history
…nking

feat(code): add ios deep linking delegate
  • Loading branch information
NickBurkhartBB authored Apr 1, 2024
2 parents eddafab + 125ba76 commit 4b1f60c
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 2 deletions.
5 changes: 5 additions & 0 deletions apps/example/coderc/build/build.internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export default defineBuild<
ios: {
bundleId: 'com.brandingbrand',
displayName: 'Branding Brand',
plist: {
urlScheme: {
scheme: 'app',
},
},
},
android: {
packageName: 'com.brandingbrand',
Expand Down
7 changes: 7 additions & 0 deletions packages/cli-kit/__tests__/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ describe("path", () => {
);
});

it("should have an ios.appDelegate function that returns the path to ios/app/AppDelegate.mm", () => {
const appDelegatePath = path.ios.appDelegate;
expect(appDelegatePath).toEqual(
expect.stringMatching(/.*ios\/app\/AppDelegate\.mm$/)
);
});

it("should have an ios.podfile function that returns the path to ios/Podfile", () => {
const podfilePath = path.ios.podfile;
expect(podfilePath).toEqual(expect.stringMatching(/.*ios\/Podfile$/));
Expand Down
7 changes: 7 additions & 0 deletions packages/cli-kit/src/lib/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ export default {
*/
nativeConstants: resolvePathFromProject("ios", "app", "NativeConstants.m"),

/**
* Retrieves the absolute path to the iOS AppDelegate.mm file.
*
* @returns {string} The absolute path to "ios/app/AppDelegate.mm".
*/
appDelegate: resolvePathFromProject("ios", "app", "AppDelegate.mm"),

/**
* Retrieves the absolute path to the iOS project.pbxproj file.
*
Expand Down
48 changes: 48 additions & 0 deletions packages/cli/__tests__/app-delegate-mm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @jest-environment-options {"requireTemplate": true}
*/

/// <reference types="@brandingbrand/code-jest-config" />

import { type BuildConfig, fs, path } from "@brandingbrand/code-cli-kit";

import transformer from "../src/transformers/ios/app-delegate-mm";

describe("AppDelegate.mm transformers", () => {
beforeEach(() => {
jest.resetAllMocks();
});

it("should not update AppDelegate.mm with RCTLinkingManager", async () => {
const config = {
...__flagship_code_build_config,
} as BuildConfig;

await transformer.transform(config, {} as any);
const content = await fs.readFile(path.ios.appDelegate, "utf-8");

expect(content).not.toContain("RCTLinkingManager");
});

it("should update AppDelegate.mm with RCTLinkingManager", async () => {
const config = {
...__flagship_code_build_config,
ios: {
...__flagship_code_build_config,
plist: {
urlScheme: {
scheme: "app",
},
},
},
} as BuildConfig;

await transformer.transform(config, {} as any);
const content = await fs.readFile(path.ios.appDelegate, "utf-8");

expect(content).toContain("#import <React/RCTLinkingManager.h>");
expect(content).toContain(
"if ([RCTLinkingManager application:application openURL:url options:options]) {"
);
});
});
4 changes: 2 additions & 2 deletions packages/cli/__tests__/env-switcher-m.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { type BuildConfig, fs, path } from "@brandingbrand/code-cli-kit";

import transformer from "../src/transformers/ios/env-switcher-m";

describe("EnvSwitcher.java transformers", () => {
describe("EnvSwitcher.m transformers", () => {
beforeEach(() => {
jest.resetAllMocks();
});

it("should not update EnvSwitcher.java with initialEnvName to staging", async () => {
it("should not update EnvSwitcher.m with initialEnvName to staging", async () => {
const config = {
...__flagship_code_build_config,
} as BuildConfig;
Expand Down
107 changes: 107 additions & 0 deletions packages/cli/src/transformers/ios/app-delegate-mm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import {
type BuildConfig,
type PrebuildOptions,
withUTF8,
path,
string,
} from "@brandingbrand/code-cli-kit";

import { Transforms, defineTransformer } from "@/lib";

/**
* Defines a transformer for the iOS project's "AppDelegate.mm" file.
*
* @type {typeof defineTransformer<(content: string, config: BuildConfig) => string>} - The type of the transformer.
* @property {string} file - The name of the file to be transformed ("AppDelegate.mm").
* @property {Array<(content: string, config: BuildConfig) => string>} transforms - An array of transformer functions.
* @property {Function} transform - The main transform function that applies all specified transformations.
* @returns {Promise<string>} The updated content of the "AppDelegate.mm" file.
*/
export default defineTransformer<Transforms<string>>({
/**
* The name of the file to be transformed ("AppDelegate.mm").
* @type {string}
*/
file: "AppDelegate.mm",

/**
* An array of transformer functions to be applied to the "AppDelegate.mm" file.
* Each function receives the content of the file and the build configuration,
* and returns the updated content after applying specific transformations.
* @type {Array<(content: string, config: BuildConfig) => string>}
*/
transforms: [
/**
* Transformer for add RCTLinkingManager import in "AppDelegate.mm".
* @param {string} content - The content of the file.
* @param {BuildConfig} config - The build configuration.
* @returns {string} - The updated content.
*/
(
content: string,
config: BuildConfig,
options: PrebuildOptions
): string => {
if (!config.ios.plist?.urlScheme) {
return content;
}

return string.replace(
content,
/(#import "AppDelegate\.h")/,
`$1
#import <React/RCTLinkingManager.h>`
);
},

/**
* Transformer for add delegate method in "AppDelegate.mm".
* @param {string} content - The content of the file.
* @param {BuildConfig} config - The build configuration.
* @returns {string} - The updated content.
*/
(
content: string,
config: BuildConfig,
options: PrebuildOptions
): string => {
if (!config.ios.plist?.urlScheme) {
return content;
}

return string.replace(
content,
/(@end)/,
`- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
if ([RCTLinkingManager application:application openURL:url options:options]) {
return YES;
}
return NO;
}
$1`
);
},
],

/**
* The main transform function that applies all specified transformations to the "AppDelegate.mm" file.
* @param {BuildConfig} config - The build configuration.
* @returns {Promise<void>} - The updated content of the "AppDelegate.mm" file.
*/
transform: async function (
config: BuildConfig,
options: PrebuildOptions
): Promise<void> {
return withUTF8(path.ios.appDelegate, (content: string) => {
return this.transforms.reduce((acc, curr) => {
return curr(acc, config, options);
}, content);
});
},
});
5 changes: 5 additions & 0 deletions packages/cli/src/transformers/ios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ export { default as pbxproj } from "./project-pbxproj";
* Represents the app.entitlements file transformers.
*/
export { default as entitlements } from "./app-entitlements";

/**
* Represents the AppDelegate.mm file transformers.
*/
export { default as appDelegate } from "./app-delegate-mm";

0 comments on commit 4b1f60c

Please sign in to comment.