mirror of
https://github.com/expo/config-plugins.git
synced 2026-11-13 20:28:07 -10:00
refactor: update plugin to use CodeMod
This commit is contained in:
6 files changed
+217
-349
No files matched your search
+2
-4
@@ -2,15 +2,13 @@ import { AndroidConfig } from "@expo/config-plugins";
|
||||
import type { ConfigPlugin, ExportedConfigWithProps } from "@expo/config-plugins";
|
||||
import type { ExpoConfig } from "@expo/config-types";
|
||||
import type { MergeResults } from "@expo/config-plugins/build/utils/generateCode";
|
||||
export declare function editMainApplication(config: ExportedConfigWithProps, action: (mainApplication: string) => string): Promise<void>;
|
||||
export declare function editProguardRules(config: ExportedConfigWithProps, action: (mainApplication: string) => string): Promise<void>;
|
||||
export declare function addBranchMainApplicationImport(src: string, packageId: string): MergeResults;
|
||||
export declare function addBranchGetAutoInstance(src: string): MergeResults;
|
||||
export declare function addBranchMainActivityImport(src: string, packageId: string): MergeResults;
|
||||
export declare function modifyMainApplication(mainApplication: string, language: "java" | "kt"): string;
|
||||
export declare function addBranchInitSession(src: string): MergeResults;
|
||||
export declare function addBranchOnNewIntent(src: string): MergeResults;
|
||||
export declare function getBranchApiKey(config: ExpoConfig): string | null;
|
||||
export declare function setBranchApiKey(apiKey: string, androidManifest: AndroidConfig.Manifest.AndroidManifest): AndroidConfig.Manifest.AndroidManifest;
|
||||
export declare const modifyMainActivity: (mainActivity: string, language: "java" | "kt") => string;
|
||||
export declare const withBranchAndroid: ConfigPlugin<{
|
||||
apiKey?: string;
|
||||
}>;
|
||||
+30
-81
@@ -18,16 +18,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.withBranchAndroid = exports.setBranchApiKey = exports.getBranchApiKey = exports.addBranchOnNewIntent = exports.addBranchInitSession = exports.addBranchMainActivityImport = exports.addBranchGetAutoInstance = exports.addBranchMainApplicationImport = exports.editProguardRules = exports.editMainApplication = void 0;
|
||||
exports.withBranchAndroid = exports.modifyMainActivity = exports.setBranchApiKey = exports.getBranchApiKey = exports.addBranchOnNewIntent = exports.addBranchInitSession = exports.modifyMainApplication = exports.editProguardRules = void 0;
|
||||
const config_plugins_1 = require("@expo/config-plugins");
|
||||
const codeMod_1 = require("@expo/config-plugins/build/android/codeMod");
|
||||
const generateCode_1 = require("@expo/config-plugins/build/utils/generateCode");
|
||||
const fs = __importStar(require("fs"));
|
||||
const path = __importStar(require("path"));
|
||||
const assert_1 = __importDefault(require("assert"));
|
||||
const { addMetaDataItemToMainApplication, getMainApplicationOrThrow, removeMetaDataItemFromMainApplication, } = config_plugins_1.AndroidConfig.Manifest;
|
||||
const META_BRANCH_KEY = "io.branch.sdk.BranchKey";
|
||||
async function readFileAsync(path) {
|
||||
@@ -58,20 +55,6 @@ function appendContents({ src, newSrc, tag, comment, }) {
|
||||
}
|
||||
return { contents: src, didClear: false, didMerge: false };
|
||||
}
|
||||
async function editMainApplication(config, action) {
|
||||
var _a;
|
||||
const packageName = (_a = config.android) === null || _a === void 0 ? void 0 : _a.package;
|
||||
assert_1.default(packageName, "android.package must be defined");
|
||||
const mainApplicationPath = path.join(config.modRequest.platformProjectRoot, "app", "src", "main", "java", ...packageName.split("."), "MainApplication.java");
|
||||
try {
|
||||
const mainApplication = action(await readFileAsync(mainApplicationPath));
|
||||
return await saveFileAsync(mainApplicationPath, mainApplication);
|
||||
}
|
||||
catch (e) {
|
||||
config_plugins_1.WarningAggregator.addWarningAndroid("@config-plugins/react-native-branch", `Couldn't modify MainApplication.java - ${e}.`);
|
||||
}
|
||||
}
|
||||
exports.editMainApplication = editMainApplication;
|
||||
async function editProguardRules(config, action) {
|
||||
const proguardRulesPath = path.join(config.modRequest.platformProjectRoot, "app", "proguard-rules.pro");
|
||||
try {
|
||||
@@ -83,45 +66,20 @@ async function editProguardRules(config, action) {
|
||||
}
|
||||
}
|
||||
exports.editProguardRules = editProguardRules;
|
||||
function addBranchMainApplicationImport(src, packageId) {
|
||||
const newSrc = ["import io.branch.rnbranch.RNBranchModule;"];
|
||||
return generateCode_1.mergeContents({
|
||||
tag: "react-native-branch-import",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: `package ${packageId};`,
|
||||
offset: 1,
|
||||
comment: "//",
|
||||
});
|
||||
function addGetAutoInstanceIfNeeded(mainApplication, isJava) {
|
||||
if (mainApplication.match(/\s+RNBranchModule\.getAutoInstance\(/m)) {
|
||||
return mainApplication;
|
||||
}
|
||||
const semicolon = isJava ? ";" : "";
|
||||
return codeMod_1.appendContentsInsideDeclarationBlock(mainApplication, "onCreate", ` RNBranchModule.getAutoInstance(this)${semicolon}\n `);
|
||||
}
|
||||
exports.addBranchMainApplicationImport = addBranchMainApplicationImport;
|
||||
function addBranchGetAutoInstance(src) {
|
||||
const newSrc = [" RNBranchModule.getAutoInstance(this);"];
|
||||
return generateCode_1.mergeContents({
|
||||
tag: "react-native-branch-auto-instance",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: /super\.onCreate\(\);/,
|
||||
offset: 1,
|
||||
comment: "//",
|
||||
});
|
||||
function modifyMainApplication(mainApplication, language) {
|
||||
const isJava = language === "java";
|
||||
mainApplication = codeMod_1.addImports(mainApplication, ["io.branch.rnbranch.RNBranchModule"], isJava);
|
||||
mainApplication = addGetAutoInstanceIfNeeded(mainApplication, isJava);
|
||||
return mainApplication;
|
||||
}
|
||||
exports.addBranchGetAutoInstance = addBranchGetAutoInstance;
|
||||
function addBranchMainActivityImport(src, packageId) {
|
||||
const newSrc = [
|
||||
"import android.content.Intent;",
|
||||
"import io.branch.rnbranch.*;",
|
||||
];
|
||||
return generateCode_1.mergeContents({
|
||||
tag: "react-native-branch-import",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: `package ${packageId};`,
|
||||
offset: 1,
|
||||
comment: "//",
|
||||
});
|
||||
}
|
||||
exports.addBranchMainActivityImport = addBranchMainActivityImport;
|
||||
exports.modifyMainApplication = modifyMainApplication;
|
||||
function addBranchInitSession(src) {
|
||||
const tag = "react-native-branch-init-session";
|
||||
try {
|
||||
@@ -212,6 +170,14 @@ function setBranchApiKey(apiKey, androidManifest) {
|
||||
return androidManifest;
|
||||
}
|
||||
exports.setBranchApiKey = setBranchApiKey;
|
||||
const modifyMainActivity = (mainActivity, language) => {
|
||||
const isJava = language === "java";
|
||||
mainActivity = codeMod_1.addImports(mainActivity, ["android.content.Intent", "io.branch.rnbranch.*"], isJava);
|
||||
mainActivity = addBranchInitSession(mainActivity).contents;
|
||||
mainActivity = addBranchOnNewIntent(mainActivity).contents;
|
||||
return mainActivity;
|
||||
};
|
||||
exports.modifyMainActivity = modifyMainActivity;
|
||||
const withBranchAndroid = (config, data) => {
|
||||
var _a;
|
||||
const apiKey = (_a = data.apiKey) !== null && _a !== void 0 ? _a : getBranchApiKey(config);
|
||||
@@ -222,21 +188,14 @@ const withBranchAndroid = (config, data) => {
|
||||
config.modResults = setBranchApiKey(apiKey, config.modResults);
|
||||
return config;
|
||||
});
|
||||
// Directly edit MainApplication.java
|
||||
config = config_plugins_1.withDangerousMod(config, [
|
||||
"android",
|
||||
async (config) => {
|
||||
var _a;
|
||||
const packageName = (_a = config.android) === null || _a === void 0 ? void 0 : _a.package;
|
||||
assert_1.default(packageName, "android.package must be defined");
|
||||
await editMainApplication(config, (mainApplication) => {
|
||||
mainApplication = addBranchMainApplicationImport(mainApplication, packageName).contents;
|
||||
mainApplication = addBranchGetAutoInstance(mainApplication).contents;
|
||||
return mainApplication;
|
||||
});
|
||||
return config;
|
||||
},
|
||||
]);
|
||||
config = config_plugins_1.withMainApplication(config, (config) => {
|
||||
config.modResults.contents = modifyMainApplication(config.modResults.contents, config.modResults.language);
|
||||
return config;
|
||||
});
|
||||
config = config_plugins_1.withMainActivity(config, (config) => {
|
||||
config.modResults.contents = exports.modifyMainActivity(config.modResults.contents, config.modResults.language);
|
||||
return config;
|
||||
});
|
||||
// Update proguard rules directly
|
||||
config = config_plugins_1.withDangerousMod(config, [
|
||||
"android",
|
||||
@@ -252,16 +211,6 @@ const withBranchAndroid = (config, data) => {
|
||||
return config;
|
||||
},
|
||||
]);
|
||||
// Insert the required Branch code into MainActivity.java
|
||||
config = config_plugins_1.withMainActivity(config, (config) => {
|
||||
var _a;
|
||||
const packageName = (_a = config.android) === null || _a === void 0 ? void 0 : _a.package;
|
||||
assert_1.default(packageName, "android.package must be defined");
|
||||
config.modResults.contents = addBranchMainActivityImport(config.modResults.contents, packageName).contents;
|
||||
config.modResults.contents = addBranchInitSession(config.modResults.contents).contents;
|
||||
config.modResults.contents = addBranchOnNewIntent(config.modResults.contents).contents;
|
||||
return config;
|
||||
});
|
||||
return config;
|
||||
};
|
||||
exports.withBranchAndroid = withBranchAndroid;
|
||||
+4
-5
@@ -1,11 +1,10 @@
|
||||
import type { ConfigPlugin, InfoPlist } from "@expo/config-plugins";
|
||||
import type { ExpoConfig } from "@expo/config-types";
|
||||
import type { MergeResults } from "@expo/config-plugins/build/utils/generateCode";
|
||||
import type { ConfigData } from "./types";
|
||||
export declare function addBranchAppDelegateImport(src: string): MergeResults;
|
||||
export declare function addBranchAppDelegateInit(src: string): MergeResults;
|
||||
export declare function addBranchAppDelegateOpenURL(src: string): MergeResults;
|
||||
export declare function addBranchAppDelegateContinueUserActivity(src: string): MergeResults;
|
||||
export declare function getBranchApiKey(config: Pick<ExpoConfig, "ios">): string | null;
|
||||
export declare function setBranchApiKey(apiKey: string, infoPlist: InfoPlist): InfoPlist;
|
||||
export declare function updateApplicationDidFinishLaunchingWithOptions(contents: string): string;
|
||||
export declare function updateApplicationOpenURLWithOptions(contents: string): string;
|
||||
export declare function updateApplicationContinueUserActivity(contents: string): string;
|
||||
export declare const modifyAppDelegateObjc: (contents: string) => string;
|
||||
export declare const withBranchIOS: ConfigPlugin<ConfigData>;
|
||||
+44
-66
@@ -1,67 +1,8 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.withBranchIOS = exports.setBranchApiKey = exports.getBranchApiKey = exports.addBranchAppDelegateContinueUserActivity = exports.addBranchAppDelegateOpenURL = exports.addBranchAppDelegateInit = exports.addBranchAppDelegateImport = void 0;
|
||||
exports.withBranchIOS = exports.modifyAppDelegateObjc = exports.updateApplicationContinueUserActivity = exports.updateApplicationOpenURLWithOptions = exports.updateApplicationDidFinishLaunchingWithOptions = exports.setBranchApiKey = exports.getBranchApiKey = void 0;
|
||||
const config_plugins_1 = require("@expo/config-plugins");
|
||||
const generateCode_1 = require("@expo/config-plugins/build/utils/generateCode");
|
||||
function addBranchAppDelegateImport(src) {
|
||||
const newSrc = ["#import <RNBranch/RNBranch.h>"];
|
||||
return generateCode_1.mergeContents({
|
||||
tag: "react-native-branch-import",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: /#import "AppDelegate\.h"/,
|
||||
offset: 1,
|
||||
comment: "//",
|
||||
});
|
||||
}
|
||||
exports.addBranchAppDelegateImport = addBranchAppDelegateImport;
|
||||
// Match against `UMModuleRegistryAdapter` (unimodules), and React Native without unimodules (Expo Modules).
|
||||
const MATCH_INIT = /(?:(self\.|_)(\w+)\s?=\s?\[\[UMModuleRegistryAdapter alloc\])|(?:RCTBridge\s?\*\s?(\w+)\s?=\s?\[\[RCTBridge alloc\])|(?:RCTBridge\s?\*\s?(\w+)\s?=\s?\[self\.(\w+))/g;
|
||||
function addBranchAppDelegateInit(src) {
|
||||
const newSrc = [];
|
||||
newSrc.push(" [RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:YES];");
|
||||
return generateCode_1.mergeContents({
|
||||
tag: "react-native-branch-init",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: MATCH_INIT,
|
||||
offset: 0,
|
||||
comment: "//",
|
||||
});
|
||||
}
|
||||
exports.addBranchAppDelegateInit = addBranchAppDelegateInit;
|
||||
function addBranchAppDelegateOpenURL(src) {
|
||||
const newSrc = [
|
||||
" if ([RNBranch application:application openURL:url options:options]) {",
|
||||
" // do other deep link routing for the Facebook SDK, Pinterest SDK, etc",
|
||||
" }",
|
||||
];
|
||||
return generateCode_1.mergeContents({
|
||||
tag: "react-native-branch-open-url",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: /\(UIApplication \*\)application openURL:/,
|
||||
offset: 1,
|
||||
comment: "//",
|
||||
});
|
||||
}
|
||||
exports.addBranchAppDelegateOpenURL = addBranchAppDelegateOpenURL;
|
||||
function addBranchAppDelegateContinueUserActivity(src) {
|
||||
const newSrc = [
|
||||
" if ([RNBranch continueUserActivity:userActivity]) {",
|
||||
" return YES;",
|
||||
" }",
|
||||
];
|
||||
return generateCode_1.mergeContents({
|
||||
tag: "react-native-branch-continue-user-activity",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: /\(UIApplication \*\)application continueUserActivity:/,
|
||||
offset: 1,
|
||||
comment: "//",
|
||||
});
|
||||
}
|
||||
exports.addBranchAppDelegateContinueUserActivity = addBranchAppDelegateContinueUserActivity;
|
||||
const codeMod_1 = require("@expo/config-plugins/build/ios/codeMod");
|
||||
function getBranchApiKey(config) {
|
||||
var _a, _b, _c, _d;
|
||||
return (_d = (_c = (_b = (_a = config.ios) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.branch) === null || _c === void 0 ? void 0 : _c.apiKey) !== null && _d !== void 0 ? _d : null;
|
||||
@@ -79,6 +20,44 @@ function setBranchApiKey(apiKey, infoPlist) {
|
||||
};
|
||||
}
|
||||
exports.setBranchApiKey = setBranchApiKey;
|
||||
function updateApplicationDidFinishLaunchingWithOptions(contents) {
|
||||
// application:didFinishLaunchingWithOptions:
|
||||
const initSessionWithLaunchOptions = "[RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:YES];";
|
||||
if (!contents.includes(` ${initSessionWithLaunchOptions}`)) {
|
||||
contents = codeMod_1.insertContentsInsideObjcFunctionBlock(contents, "application:didFinishLaunchingWithOptions:", initSessionWithLaunchOptions, { position: "head", indent: 2 });
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
exports.updateApplicationDidFinishLaunchingWithOptions = updateApplicationDidFinishLaunchingWithOptions;
|
||||
function updateApplicationOpenURLWithOptions(contents) {
|
||||
// application:openURL:options:
|
||||
const branchOpenURLWithOptions = "[RNBranch application:application openURL:url options:options];";
|
||||
if (!contents.includes(` ${branchOpenURLWithOptions}`)) {
|
||||
contents = codeMod_1.insertContentsInsideObjcFunctionBlock(contents, "application:openURL:options:", branchOpenURLWithOptions, { position: "tailBeforeLastReturn", indent: 2 });
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
exports.updateApplicationOpenURLWithOptions = updateApplicationOpenURLWithOptions;
|
||||
function updateApplicationContinueUserActivity(contents) {
|
||||
// application:continueUserActivity:restorationHandler:
|
||||
const branchContinueUserActivity = "if ([RNBranch continueUserActivity:userActivity]) { return YES; }";
|
||||
if (!contents.includes(` ${branchContinueUserActivity}`)) {
|
||||
contents = codeMod_1.insertContentsInsideObjcFunctionBlock(contents, "application:continueUserActivity:restorationHandler:", branchContinueUserActivity, { position: "tailBeforeLastReturn", indent: 2 });
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
exports.updateApplicationContinueUserActivity = updateApplicationContinueUserActivity;
|
||||
const modifyAppDelegateObjc = (contents) => {
|
||||
// Add imports if needed
|
||||
if (!contents.match(/^#import\s+<RNBranch\/RNBranch\.h>\s*$/m)) {
|
||||
contents = codeMod_1.addObjcImports(contents, ["<RNBranch/RNBranch.h>"]);
|
||||
}
|
||||
contents = updateApplicationDidFinishLaunchingWithOptions(contents);
|
||||
contents = updateApplicationOpenURLWithOptions(contents);
|
||||
contents = updateApplicationContinueUserActivity(contents);
|
||||
return contents;
|
||||
};
|
||||
exports.modifyAppDelegateObjc = modifyAppDelegateObjc;
|
||||
const withBranchIOS = (config, data) => {
|
||||
var _a;
|
||||
// Ensure object exist
|
||||
@@ -107,12 +86,11 @@ const withBranchIOS = (config, data) => {
|
||||
}
|
||||
return config;
|
||||
});
|
||||
// Update the AppDelegate.m
|
||||
config = config_plugins_1.withAppDelegate(config, (config) => {
|
||||
config.modResults.contents = addBranchAppDelegateImport(config.modResults.contents).contents;
|
||||
config.modResults.contents = addBranchAppDelegateInit(config.modResults.contents).contents;
|
||||
config.modResults.contents = addBranchAppDelegateOpenURL(config.modResults.contents).contents;
|
||||
config.modResults.contents = addBranchAppDelegateContinueUserActivity(config.modResults.contents).contents;
|
||||
if (config.modResults.language === "swift") {
|
||||
throw new Error("Branch is not supported in Swift");
|
||||
}
|
||||
config.modResults.contents = exports.modifyAppDelegateObjc(config.modResults.contents);
|
||||
return config;
|
||||
});
|
||||
return config;
|
||||
|
||||
@@ -4,7 +4,12 @@ import {
|
||||
withAndroidManifest,
|
||||
withDangerousMod,
|
||||
withMainActivity,
|
||||
withMainApplication,
|
||||
} from "@expo/config-plugins";
|
||||
import {
|
||||
addImports,
|
||||
appendContentsInsideDeclarationBlock,
|
||||
} from "@expo/config-plugins/build/android/codeMod";
|
||||
import type {
|
||||
ConfigPlugin,
|
||||
ExportedConfigWithProps,
|
||||
@@ -18,7 +23,6 @@ import {
|
||||
import type { MergeResults } from "@expo/config-plugins/build/utils/generateCode";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import assert from "assert";
|
||||
|
||||
const {
|
||||
addMetaDataItemToMainApplication,
|
||||
@@ -71,34 +75,6 @@ function appendContents({
|
||||
return { contents: src, didClear: false, didMerge: false };
|
||||
}
|
||||
|
||||
export async function editMainApplication(
|
||||
config: ExportedConfigWithProps,
|
||||
action: (mainApplication: string) => string
|
||||
) {
|
||||
const packageName = config.android?.package;
|
||||
assert(packageName, "android.package must be defined");
|
||||
|
||||
const mainApplicationPath = path.join(
|
||||
config.modRequest.platformProjectRoot,
|
||||
"app",
|
||||
"src",
|
||||
"main",
|
||||
"java",
|
||||
...packageName.split("."),
|
||||
"MainApplication.java"
|
||||
);
|
||||
|
||||
try {
|
||||
const mainApplication = action(await readFileAsync(mainApplicationPath));
|
||||
return await saveFileAsync(mainApplicationPath, mainApplication);
|
||||
} catch (e) {
|
||||
WarningAggregator.addWarningAndroid(
|
||||
"@config-plugins/react-native-branch",
|
||||
`Couldn't modify MainApplication.java - ${e}.`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function editProguardRules(
|
||||
config: ExportedConfigWithProps,
|
||||
action: (mainApplication: string) => string
|
||||
@@ -119,52 +95,36 @@ export async function editProguardRules(
|
||||
}
|
||||
}
|
||||
|
||||
export function addBranchMainApplicationImport(
|
||||
src: string,
|
||||
packageId: string
|
||||
): MergeResults {
|
||||
const newSrc = ["import io.branch.rnbranch.RNBranchModule;"];
|
||||
function addGetAutoInstanceIfNeeded(
|
||||
mainApplication: string,
|
||||
isJava: boolean
|
||||
): string {
|
||||
if (mainApplication.match(/\s+RNBranchModule\.getAutoInstance\(/m)) {
|
||||
return mainApplication;
|
||||
}
|
||||
|
||||
return mergeContents({
|
||||
tag: "react-native-branch-import",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: `package ${packageId};`,
|
||||
offset: 1,
|
||||
comment: "//",
|
||||
});
|
||||
const semicolon = isJava ? ";" : "";
|
||||
return appendContentsInsideDeclarationBlock(
|
||||
mainApplication,
|
||||
"onCreate",
|
||||
` RNBranchModule.getAutoInstance(this)${semicolon}\n `
|
||||
);
|
||||
}
|
||||
|
||||
export function addBranchGetAutoInstance(src: string): MergeResults {
|
||||
const newSrc = [" RNBranchModule.getAutoInstance(this);"];
|
||||
export function modifyMainApplication(
|
||||
mainApplication: string,
|
||||
language: "java" | "kt"
|
||||
): string {
|
||||
const isJava = language === "java";
|
||||
|
||||
return mergeContents({
|
||||
tag: "react-native-branch-auto-instance",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: /super\.onCreate\(\);/,
|
||||
offset: 1,
|
||||
comment: "//",
|
||||
});
|
||||
}
|
||||
mainApplication = addImports(
|
||||
mainApplication,
|
||||
["io.branch.rnbranch.RNBranchModule"],
|
||||
isJava
|
||||
);
|
||||
mainApplication = addGetAutoInstanceIfNeeded(mainApplication, isJava);
|
||||
|
||||
export function addBranchMainActivityImport(
|
||||
src: string,
|
||||
packageId: string
|
||||
): MergeResults {
|
||||
const newSrc = [
|
||||
"import android.content.Intent;",
|
||||
"import io.branch.rnbranch.*;",
|
||||
];
|
||||
|
||||
return mergeContents({
|
||||
tag: "react-native-branch-import",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: `package ${packageId};`,
|
||||
offset: 1,
|
||||
comment: "//",
|
||||
});
|
||||
return mainApplication;
|
||||
}
|
||||
|
||||
export function addBranchInitSession(src: string): MergeResults {
|
||||
@@ -264,6 +224,23 @@ export function setBranchApiKey(
|
||||
return androidManifest;
|
||||
}
|
||||
|
||||
export const modifyMainActivity = (
|
||||
mainActivity: string,
|
||||
language: "java" | "kt"
|
||||
): string => {
|
||||
const isJava = language === "java";
|
||||
|
||||
mainActivity = addImports(
|
||||
mainActivity,
|
||||
["android.content.Intent", "io.branch.rnbranch.*"],
|
||||
isJava
|
||||
);
|
||||
mainActivity = addBranchInitSession(mainActivity).contents;
|
||||
mainActivity = addBranchOnNewIntent(mainActivity).contents;
|
||||
|
||||
return mainActivity;
|
||||
};
|
||||
|
||||
export const withBranchAndroid: ConfigPlugin<{ apiKey?: string }> = (
|
||||
config,
|
||||
data
|
||||
@@ -278,26 +255,23 @@ export const withBranchAndroid: ConfigPlugin<{ apiKey?: string }> = (
|
||||
return config;
|
||||
});
|
||||
|
||||
// Directly edit MainApplication.java
|
||||
config = withDangerousMod(config, [
|
||||
"android",
|
||||
async (config) => {
|
||||
const packageName = config.android?.package;
|
||||
assert(packageName, "android.package must be defined");
|
||||
config = withMainApplication(config, (config) => {
|
||||
config.modResults.contents = modifyMainApplication(
|
||||
config.modResults.contents,
|
||||
config.modResults.language
|
||||
);
|
||||
|
||||
await editMainApplication(config, (mainApplication) => {
|
||||
mainApplication = addBranchMainApplicationImport(
|
||||
mainApplication,
|
||||
packageName
|
||||
).contents;
|
||||
mainApplication = addBranchGetAutoInstance(mainApplication).contents;
|
||||
return config;
|
||||
});
|
||||
|
||||
return mainApplication;
|
||||
});
|
||||
config = withMainActivity(config, (config) => {
|
||||
config.modResults.contents = modifyMainActivity(
|
||||
config.modResults.contents,
|
||||
config.modResults.language
|
||||
);
|
||||
|
||||
return config;
|
||||
},
|
||||
]);
|
||||
return config;
|
||||
});
|
||||
|
||||
// Update proguard rules directly
|
||||
config = withDangerousMod(config, [
|
||||
@@ -316,24 +290,5 @@ export const withBranchAndroid: ConfigPlugin<{ apiKey?: string }> = (
|
||||
},
|
||||
]);
|
||||
|
||||
// Insert the required Branch code into MainActivity.java
|
||||
config = withMainActivity(config, (config) => {
|
||||
const packageName = config.android?.package;
|
||||
assert(packageName, "android.package must be defined");
|
||||
|
||||
config.modResults.contents = addBranchMainActivityImport(
|
||||
config.modResults.contents,
|
||||
packageName
|
||||
).contents;
|
||||
config.modResults.contents = addBranchInitSession(
|
||||
config.modResults.contents
|
||||
).contents;
|
||||
config.modResults.contents = addBranchOnNewIntent(
|
||||
config.modResults.contents
|
||||
).contents;
|
||||
|
||||
return config;
|
||||
});
|
||||
|
||||
return config;
|
||||
};
|
||||
@@ -1,81 +1,11 @@
|
||||
import {
|
||||
withAppDelegate,
|
||||
withInfoPlist,
|
||||
IOSConfig,
|
||||
} from "@expo/config-plugins";
|
||||
import { withAppDelegate, withInfoPlist } from "@expo/config-plugins";
|
||||
import type { ConfigPlugin, InfoPlist } from "@expo/config-plugins";
|
||||
import type { ExpoConfig } from "@expo/config-types";
|
||||
import { mergeContents } from "@expo/config-plugins/build/utils/generateCode";
|
||||
import type { MergeResults } from "@expo/config-plugins/build/utils/generateCode";
|
||||
import type { ConfigData } from "./types";
|
||||
|
||||
export function addBranchAppDelegateImport(src: string): MergeResults {
|
||||
const newSrc = ["#import <RNBranch/RNBranch.h>"];
|
||||
return mergeContents({
|
||||
tag: "react-native-branch-import",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: /#import "AppDelegate\.h"/,
|
||||
offset: 1,
|
||||
comment: "//",
|
||||
});
|
||||
}
|
||||
|
||||
// Match against `UMModuleRegistryAdapter` (unimodules), and React Native without unimodules (Expo Modules).
|
||||
const MATCH_INIT =
|
||||
/(?:(self\.|_)(\w+)\s?=\s?\[\[UMModuleRegistryAdapter alloc\])|(?:RCTBridge\s?\*\s?(\w+)\s?=\s?\[\[RCTBridge alloc\])|(?:RCTBridge\s?\*\s?(\w+)\s?=\s?\[self\.(\w+))/g;
|
||||
|
||||
export function addBranchAppDelegateInit(src: string): MergeResults {
|
||||
const newSrc = [];
|
||||
newSrc.push(
|
||||
" [RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:YES];"
|
||||
);
|
||||
|
||||
return mergeContents({
|
||||
tag: "react-native-branch-init",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: MATCH_INIT,
|
||||
offset: 0,
|
||||
comment: "//",
|
||||
});
|
||||
}
|
||||
|
||||
export function addBranchAppDelegateOpenURL(src: string): MergeResults {
|
||||
const newSrc = [
|
||||
" if ([RNBranch application:application openURL:url options:options]) {",
|
||||
" // do other deep link routing for the Facebook SDK, Pinterest SDK, etc",
|
||||
" }",
|
||||
];
|
||||
|
||||
return mergeContents({
|
||||
tag: "react-native-branch-open-url",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: /\(UIApplication \*\)application openURL:/,
|
||||
offset: 1,
|
||||
comment: "//",
|
||||
});
|
||||
}
|
||||
|
||||
export function addBranchAppDelegateContinueUserActivity(
|
||||
src: string
|
||||
): MergeResults {
|
||||
const newSrc = [
|
||||
" if ([RNBranch continueUserActivity:userActivity]) {",
|
||||
" return YES;",
|
||||
" }",
|
||||
];
|
||||
|
||||
return mergeContents({
|
||||
tag: "react-native-branch-continue-user-activity",
|
||||
src,
|
||||
newSrc: newSrc.join("\n"),
|
||||
anchor: /\(UIApplication \*\)application continueUserActivity:/,
|
||||
offset: 1,
|
||||
comment: "//",
|
||||
});
|
||||
}
|
||||
import {
|
||||
addObjcImports,
|
||||
insertContentsInsideObjcFunctionBlock,
|
||||
} from "@expo/config-plugins/build/ios/codeMod";
|
||||
|
||||
export function getBranchApiKey(config: Pick<ExpoConfig, "ios">) {
|
||||
return config.ios?.config?.branch?.apiKey ?? null;
|
||||
@@ -97,6 +27,71 @@ export function setBranchApiKey(
|
||||
};
|
||||
}
|
||||
|
||||
export function updateApplicationDidFinishLaunchingWithOptions(
|
||||
contents: string
|
||||
): string {
|
||||
// application:didFinishLaunchingWithOptions:
|
||||
const initSessionWithLaunchOptions =
|
||||
"[RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:YES];";
|
||||
if (!contents.includes(` ${initSessionWithLaunchOptions}`)) {
|
||||
contents = insertContentsInsideObjcFunctionBlock(
|
||||
contents,
|
||||
"application:didFinishLaunchingWithOptions:",
|
||||
initSessionWithLaunchOptions,
|
||||
{ position: "head", indent: 2 }
|
||||
);
|
||||
}
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
export function updateApplicationOpenURLWithOptions(contents: string): string {
|
||||
// application:openURL:options:
|
||||
const branchOpenURLWithOptions =
|
||||
"[RNBranch application:application openURL:url options:options];";
|
||||
if (!contents.includes(` ${branchOpenURLWithOptions}`)) {
|
||||
contents = insertContentsInsideObjcFunctionBlock(
|
||||
contents,
|
||||
"application:openURL:options:",
|
||||
branchOpenURLWithOptions,
|
||||
{ position: "tailBeforeLastReturn", indent: 2 }
|
||||
);
|
||||
}
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
export function updateApplicationContinueUserActivity(
|
||||
contents: string
|
||||
): string {
|
||||
// application:continueUserActivity:restorationHandler:
|
||||
const branchContinueUserActivity =
|
||||
"if ([RNBranch continueUserActivity:userActivity]) { return YES; }";
|
||||
if (!contents.includes(` ${branchContinueUserActivity}`)) {
|
||||
contents = insertContentsInsideObjcFunctionBlock(
|
||||
contents,
|
||||
"application:continueUserActivity:restorationHandler:",
|
||||
branchContinueUserActivity,
|
||||
{ position: "tailBeforeLastReturn", indent: 2 }
|
||||
);
|
||||
}
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
export const modifyAppDelegateObjc = (contents: string): string => {
|
||||
// Add imports if needed
|
||||
if (!contents.match(/^#import\s+<RNBranch\/RNBranch\.h>\s*$/m)) {
|
||||
contents = addObjcImports(contents, ["<RNBranch/RNBranch.h>"]);
|
||||
}
|
||||
|
||||
contents = updateApplicationDidFinishLaunchingWithOptions(contents);
|
||||
contents = updateApplicationOpenURLWithOptions(contents);
|
||||
contents = updateApplicationContinueUserActivity(contents);
|
||||
|
||||
return contents;
|
||||
};
|
||||
|
||||
export const withBranchIOS: ConfigPlugin<ConfigData> = (config, data) => {
|
||||
// Ensure object exist
|
||||
if (!config.ios) {
|
||||
@@ -125,20 +120,14 @@ export const withBranchIOS: ConfigPlugin<ConfigData> = (config, data) => {
|
||||
return config;
|
||||
});
|
||||
|
||||
// Update the AppDelegate.m
|
||||
config = withAppDelegate(config, (config) => {
|
||||
config.modResults.contents = addBranchAppDelegateImport(
|
||||
if (config.modResults.language === "swift") {
|
||||
throw new Error("Branch is not supported in Swift");
|
||||
}
|
||||
|
||||
config.modResults.contents = modifyAppDelegateObjc(
|
||||
config.modResults.contents
|
||||
).contents;
|
||||
config.modResults.contents = addBranchAppDelegateInit(
|
||||
config.modResults.contents
|
||||
).contents;
|
||||
config.modResults.contents = addBranchAppDelegateOpenURL(
|
||||
config.modResults.contents
|
||||
).contents;
|
||||
config.modResults.contents = addBranchAppDelegateContinueUserActivity(
|
||||
config.modResults.contents
|
||||
).contents;
|
||||
);
|
||||
|
||||
return config;
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user