From 9f844514c44fbbb6397c0e3fd3a022a691475339 Mon Sep 17 00:00:00 2001 From: christianEconify Date: Tue, 15 Oct 2024 11:31:56 +0100 Subject: [PATCH 1/2] feat: add amazon oncreate function to expo plugin --- plugin/build/withIAP.d.ts | 1 + plugin/build/withIAP.js | 29 +++++++++++++++++++++- plugin/src/withIAP.ts | 52 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/plugin/build/withIAP.d.ts b/plugin/build/withIAP.d.ts index f37335a96..7810585ef 100644 --- a/plugin/build/withIAP.d.ts +++ b/plugin/build/withIAP.d.ts @@ -2,6 +2,7 @@ import { ConfigPlugin } from 'expo/config-plugins'; type PaymentProvider = 'Amazon AppStore' | 'both' | 'Play Store'; export declare const modifyAppBuildGradle: (buildGradle: string, paymentProvider: PaymentProvider) => string; export declare const modifyProjectBuildGradle: (buildGradle: string) => string; +export declare const modifyMainActivity: (mainActivity: string, paymentProvider: PaymentProvider) => string; interface Props { paymentProvider?: PaymentProvider; } diff --git a/plugin/build/withIAP.js b/plugin/build/withIAP.js index afb79112c..213daa8e7 100644 --- a/plugin/build/withIAP.js +++ b/plugin/build/withIAP.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.modifyProjectBuildGradle = exports.modifyAppBuildGradle = void 0; +exports.modifyMainActivity = exports.modifyProjectBuildGradle = exports.modifyAppBuildGradle = void 0; const config_plugins_1 = require("expo/config-plugins"); const config_plugins_2 = require("expo/config-plugins"); const pkg = require('../../package.json'); @@ -31,6 +31,13 @@ const addToBuildGradle = (newLine, anchor, offset, buildGradle) => { lines.splice(lineIndex + offset, 0, newLine); return lines.join('\n'); }; +const addToMainActivity = (newLine, anchor, offset, mainActivity) => { + const lines = mainActivity.split('\n'); + const lineIndex = lines.findIndex((line) => line.match(anchor)); + // add after given line + lines.splice(lineIndex + offset, 0, newLine); + return lines.join('\n'); +}; const modifyAppBuildGradle = (buildGradle, paymentProvider) => { if (paymentProvider === 'both') { if (buildGradle.includes(`flavorDimensions "appstore"`)) { @@ -53,6 +60,21 @@ const modifyProjectBuildGradle = (buildGradle) => { return addToBuildGradle(supportLibVersion, 'ext', 1, buildGradle); }; exports.modifyProjectBuildGradle = modifyProjectBuildGradle; +const modifyMainActivity = (mainActivity, paymentProvider) => { + // These lines only need to be added if Amazon Store is a target + if (paymentProvider === 'Play Store') + return mainActivity; + const importLine = 'import com.dooboolab.rniap.RNIapActivityListener'; + const listener = 'RNIapActivityListener.registerActivity(this)'; + if (!mainActivity.includes(importLine)) { + mainActivity = addToMainActivity(importLine, 'import expo.modules.ReactActivityDelegateWrapper', 1, mainActivity); + } + if (!mainActivity.includes(listener)) { + mainActivity = addToMainActivity(listener, 'super.onCreate', 1, mainActivity); + } + return mainActivity; +}; +exports.modifyMainActivity = modifyMainActivity; const withIAPAndroid = (config, { paymentProvider }) => { // eslint-disable-next-line @typescript-eslint/no-shadow config = (0, config_plugins_1.withAppBuildGradle)(config, (config) => { @@ -64,6 +86,11 @@ const withIAPAndroid = (config, { paymentProvider }) => { config.modResults.contents = (0, exports.modifyProjectBuildGradle)(config.modResults.contents); return config; }); + // eslint-disable-next-line @typescript-eslint/no-shadow + config = (0, config_plugins_1.withMainActivity)(config, (config) => { + config.modResults.contents = (0, exports.modifyMainActivity)(config.modResults.contents, paymentProvider); + return config; + }); return config; }; const withIAP = (config, props) => { diff --git a/plugin/src/withIAP.ts b/plugin/src/withIAP.ts index 0a2cc4bf2..ae1c55c8d 100644 --- a/plugin/src/withIAP.ts +++ b/plugin/src/withIAP.ts @@ -1,6 +1,7 @@ import { WarningAggregator, withAppBuildGradle, + withMainActivity, withProjectBuildGradle, } from 'expo/config-plugins'; import {ConfigPlugin, createRunOncePlugin} from 'expo/config-plugins'; @@ -46,6 +47,19 @@ const addToBuildGradle = ( return lines.join('\n'); }; +const addToMainActivity = ( + newLine: string, + anchor: RegExp | string, + offset: number, + mainActivity: string, +) => { + const lines = mainActivity.split('\n'); + const lineIndex = lines.findIndex((line) => line.match(anchor)); + // add after given line + lines.splice(lineIndex + offset, 0, newLine); + return lines.join('\n'); +}; + export const modifyAppBuildGradle = ( buildGradle: string, paymentProvider: PaymentProvider, @@ -82,6 +96,34 @@ export const modifyProjectBuildGradle = (buildGradle: string) => { return addToBuildGradle(supportLibVersion, 'ext', 1, buildGradle); }; +export const modifyMainActivity = ( + mainActivity: string, + paymentProvider: PaymentProvider, +) => { + // These lines only need to be added if Amazon Store is a target + if (paymentProvider === 'Play Store') return mainActivity; + + const importLine = 'import com.dooboolab.rniap.RNIapActivityListener'; + const listener = 'RNIapActivityListener.registerActivity(this)'; + if (!mainActivity.includes(importLine)) { + mainActivity = addToMainActivity( + importLine, + 'import expo.modules.ReactActivityDelegateWrapper', + 1, + mainActivity, + ); + } + if (!mainActivity.includes(listener)) { + mainActivity = addToMainActivity( + listener, + 'super.onCreate', + 1, + mainActivity, + ); + } + return mainActivity; +}; + const withIAPAndroid: ConfigPlugin<{paymentProvider: PaymentProvider}> = ( config, {paymentProvider}, @@ -102,6 +144,16 @@ const withIAPAndroid: ConfigPlugin<{paymentProvider: PaymentProvider}> = ( ); return config; }); + + // eslint-disable-next-line @typescript-eslint/no-shadow + config = withMainActivity(config, (config) => { + config.modResults.contents = modifyMainActivity( + config.modResults.contents, + paymentProvider, + ); + return config; + }); + return config; }; From 72c68cbea15f54ac0f6335bd345c0bd24fed8b25 Mon Sep 17 00:00:00 2001 From: christianEconify Date: Mon, 4 Nov 2024 08:53:25 +0000 Subject: [PATCH 2/2] chore: refactor modifiying src files --- plugin/src/withIAP.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/plugin/src/withIAP.ts b/plugin/src/withIAP.ts index ae1c55c8d..a0c53b046 100644 --- a/plugin/src/withIAP.ts +++ b/plugin/src/withIAP.ts @@ -34,30 +34,40 @@ productFlavors { }`, }; -const addToBuildGradle = ( +const addLineToFile = ( newLine: string, anchor: RegExp | string, offset: number, - buildGradle: string, + fileContents: string, ) => { - const lines = buildGradle.split('\n'); + const lines = fileContents.split('\n'); const lineIndex = lines.findIndex((line) => line.match(anchor)); - // add after given line + + if (lineIndex === -1) { + console.warn(`Anchor "${anchor}" not found, skipping modification.`); + return fileContents; + } + lines.splice(lineIndex + offset, 0, newLine); return lines.join('\n'); }; +const addToBuildGradle = ( + newLine: string, + anchor: RegExp | string, + offset: number, + buildGradle: string, +) => { + return addLineToFile(newLine, anchor, offset, buildGradle); +}; + const addToMainActivity = ( newLine: string, anchor: RegExp | string, offset: number, mainActivity: string, ) => { - const lines = mainActivity.split('\n'); - const lineIndex = lines.findIndex((line) => line.match(anchor)); - // add after given line - lines.splice(lineIndex + offset, 0, newLine); - return lines.join('\n'); + return addLineToFile(newLine, anchor, offset, mainActivity); }; export const modifyAppBuildGradle = (