Skip to content

Commit

Permalink
expo: add expo configuration support for batch 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-roland committed May 30, 2024
1 parent 4873e0e commit 6228fc2
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 9 deletions.
6 changes: 3 additions & 3 deletions plugin/src/android/withReactNativeBatchAppBuildGradle.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ConfigPlugin, withAppBuildGradle } from '@expo/config-plugins';

import { BATCH_SDK_VERISON, BATCH_DO_NOT_DISTURB_INITIAL_STATE } from '../constants';
import { BATCH_SDK_VERSION, BATCH_DO_NOT_DISTURB_INITIAL_STATE } from '../constants';
import { Props } from '../withReactNativeBatch';

export const pushDependencies = (contents: string, props: Props): string => {
let newContents = contents;
const doNotDisturb = props.enableDoNotDisturb || props.enableDoNotDistrub || BATCH_DO_NOT_DISTURB_INITIAL_STATE;
const doNotDisturb = props.enableDoNotDisturb !== undefined ? props.enableDoNotDisturb : BATCH_DO_NOT_DISTURB_INITIAL_STATE;
const versionNameLine = newContents.match(/versionName "([^"]*)"/);

if (versionNameLine) {
Expand Down Expand Up @@ -33,7 +33,7 @@ export const pushDependencies = (contents: string, props: Props): string => {
start +
`\n implementation platform('com.google.firebase:firebase-bom:25.12.0')
implementation "com.google.firebase:firebase-messaging"
api "com.batch.android:batch-sdk:${BATCH_SDK_VERISON}"` +
api "com.batch.android:batch-sdk:${BATCH_SDK_VERSION}"` +
end;
}
return newContents;
Expand Down
48 changes: 48 additions & 0 deletions plugin/src/android/withReactNativeBatchManifest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ConfigPlugin, AndroidManifest, withAndroidManifest } from '@expo/config-plugins';

import {
BATCH_DEFAULT_PROFILE_CUSTOM_DATA_MIGRATION,
BATCH_DEFAULT_PROFILE_CUSTOM_ID_MIGRATION,
BATCH_DEFAULT_OPT_OUT_INITIAL_STATE,
} from '../constants';
import { Props } from '../withReactNativeBatch';

export const modifyAndroidManifest = (modResults: AndroidManifest, props: Props): AndroidManifest => {
const profileCustomIdMigrationEnabled =
props.enableProfileCustomIDMigration !== undefined ? props.enableProfileCustomIDMigration : BATCH_DEFAULT_PROFILE_CUSTOM_ID_MIGRATION;
const profileCustomDataMigrationEnabled =
props.enableProfileCustomDataMigration !== undefined
? props.enableProfileCustomDataMigration
: BATCH_DEFAULT_PROFILE_CUSTOM_DATA_MIGRATION;
const defaultOptedOut = props.enableDefaultOptOut !== undefined ? props.enableDefaultOptOut : BATCH_DEFAULT_OPT_OUT_INITIAL_STATE;
modResults.manifest?.application?.map(element => {
if (element['meta-data']) {
element['meta-data'].push({
$: {
'android:name': 'batch.profile_custom_id_migration_enabled',
'android:value': String(profileCustomIdMigrationEnabled),
},
});
element['meta-data'].push({
$: {
'android:name': 'batch.profile_custom_data_migration_enabled',
'android:value': String(profileCustomDataMigrationEnabled),
},
});
element['meta-data'].push({
$: {
'android:name': 'batch_opted_out_by_default',
'android:value': String(defaultOptedOut),
},
});
}
});
return modResults;
};

export const withReactNativeBatchManifest: ConfigPlugin<Props> = (config, props) => {
return withAndroidManifest(config, config => {
config.modResults = modifyAndroidManifest(config.modResults, props);
return config;
});
};
5 changes: 4 additions & 1 deletion plugin/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//Define the version used in the pre-built gradle file for the batch native sdk dependency.
export const BATCH_SDK_VERISON = '1.21.+';
export const BATCH_SDK_VERSION = '2.0.+';
export const BATCH_DO_NOT_DISTURB_INITIAL_STATE = false;
export const BATCH_DEFAULT_OPT_OUT_INITIAL_STATE = false;
export const BATCH_DEFAULT_PROFILE_CUSTOM_ID_MIGRATION = true;
export const BATCH_DEFAULT_PROFILE_CUSTOM_DATA_MIGRATION = true;
4 changes: 2 additions & 2 deletions plugin/src/fixtures/buildGradle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BATCH_SDK_VERISON } from "../constants";
import { BATCH_SDK_VERSION } from "../constants";

const FLIPPER_VERSION = '1.0.0';
export const buildGradleFixture = `
Expand Down Expand Up @@ -213,7 +213,7 @@ android {
dependencies {
implementation platform('com.google.firebase:firebase-bom:25.12.0')
implementation "com.google.firebase:firebase-messaging"
api "com.batch.android:batch-sdk:${BATCH_SDK_VERISON}"
api "com.batch.android:batch-sdk:${BATCH_SDK_VERSION}"
implementation fileTree(dir: "libs", include: ["*.jar"])
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+" // From node_modules
Expand Down
18 changes: 16 additions & 2 deletions plugin/src/ios/withReactNativeBatchInfoPlist.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { ConfigPlugin, withInfoPlist, InfoPlist } from '@expo/config-plugins';

import { BATCH_DO_NOT_DISTURB_INITIAL_STATE } from '../constants';
import {
BATCH_DO_NOT_DISTURB_INITIAL_STATE,
BATCH_DEFAULT_OPT_OUT_INITIAL_STATE,
BATCH_DEFAULT_PROFILE_CUSTOM_ID_MIGRATION,
BATCH_DEFAULT_PROFILE_CUSTOM_DATA_MIGRATION,
} from '../constants';
import { Props } from '../withReactNativeBatch';

export const modifyInfoPlist = (infoPlist: InfoPlist, props: Props): InfoPlist => {
infoPlist.BatchAPIKey = props.iosApiKey;
infoPlist.BatchDoNotDisturbInitialState = props.enableDoNotDisturb || props.enableDoNotDistrub || BATCH_DO_NOT_DISTURB_INITIAL_STATE;
infoPlist.BatchDoNotDisturbInitialState =
props.enableDoNotDisturb !== undefined ? props.enableDoNotDisturb : BATCH_DO_NOT_DISTURB_INITIAL_STATE;
infoPlist.BatchProfileCustomIdMigrationEnabled =
props.enableProfileCustomIDMigration !== undefined ? props.enableProfileCustomIDMigration : BATCH_DEFAULT_PROFILE_CUSTOM_ID_MIGRATION;
infoPlist.BatchProfileCustomDataMigrationEnabled =
props.enableProfileCustomDataMigration !== undefined
? props.enableProfileCustomDataMigration
: BATCH_DEFAULT_PROFILE_CUSTOM_DATA_MIGRATION;
infoPlist.BATCH_OPTED_OUT_BY_DEFAULT =
props.enableDefaultOptOut !== undefined ? props.enableDefaultOptOut : BATCH_DEFAULT_OPT_OUT_INITIAL_STATE;
return infoPlist;
};

Expand Down
11 changes: 10 additions & 1 deletion plugin/src/withReactNativeBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ import { withClassPath, withApplyPlugin, withGoogleServicesFile } from '@expo/co

import { withReactNativeBatchAppBuildGradle } from './android/withReactNativeBatchAppBuildGradle';
import { withReactNativeBatchMainActivity } from './android/withReactNativeBatchMainActivity';
import { withReactNativeBatchManifest } from './android/withReactNativeBatchManifest';
import { withReactNativeBatchAppDelegate } from './ios/withReactNativeBatchAppDelegate';
import { withReactNativeBatchInfoPlist } from './ios/withReactNativeBatchInfoPlist';

export type Props = { androidApiKey: string; iosApiKey: string; enableDoNotDisturb?: boolean; enableDoNotDistrub?: boolean };
export type Props = {
androidApiKey: string;
iosApiKey: string;
enableDoNotDisturb?: boolean;
enableDefaultOptOut?: boolean;
enableProfileCustomIDMigration?: boolean;
enableProfileCustomDataMigration?: boolean;
};
/**
* Apply react-native-batch configuration for Expo SDK 42 projects.
*/
Expand All @@ -16,6 +24,7 @@ const withReactNativeBatch: ConfigPlugin<Props | void> = (config, props) => {
let newConfig = withGoogleServicesFile(config);
newConfig = withClassPath(newConfig);
newConfig = withApplyPlugin(newConfig);
newConfig = withReactNativeBatchManifest(newConfig, _props);
newConfig = withReactNativeBatchAppBuildGradle(newConfig, _props);
newConfig = withReactNativeBatchMainActivity(newConfig);
newConfig = withReactNativeBatchInfoPlist(newConfig, _props);
Expand Down

0 comments on commit 6228fc2

Please sign in to comment.