11/**
2- * Recursively patches all generated Android files:
2+ * Recursively patches all generated Nitro files (Android & iOS):
3+ *
4+ * ANDROID
35 * - Replaces 'com.margelo.nitro.rngooglemapsplus' -> 'com.rngooglemapsplus'
46 * - Replaces 'com/margelo/nitro/rngooglemapsplus' -> 'com/rngooglemapsplus'
57 * - Removes 'margelo/nitro/' in RNGoogleMapsPlusOnLoad.cpp
6- * - Inserts `prepareToRecycleView()` under `onDropViewInstance()` if missing
8+ * - Inserts `prepareToRecycleView()`
9+ * nitrogen/generated/android/kotlin/com/margelo/nitro/rngooglemapsplus/views/HybridRNGoogleMapsPlusViewManager.kt
10+ *
11+ * iOS
12+ * - Inserts `+ (BOOL)shouldBeRecycled`
13+ * nitrogen/generated/ios/c++/views/HybridRNGoogleMapsPlusViewComponent.mm
714 */
815import { fileURLToPath } from 'url' ;
916import { basename } from 'path' ;
1017import path from 'node:path' ;
1118import { readdir , readFile , writeFile } from 'node:fs/promises' ;
1219
13- const ROOT_DIR = path . join ( process . cwd ( ) , 'nitrogen' , 'generated' , 'android' ) ;
14- console . log ( ROOT_DIR ) ;
15- const ANDROID_ONLOAD_FILE = path . join ( ROOT_DIR , 'RNGoogleMapsPlusOnLoad.cpp' ) ;
20+ const ROOT_ANDROID = path . join (
21+ process . cwd ( ) ,
22+ 'nitrogen' ,
23+ 'generated' ,
24+ 'android'
25+ ) ;
26+ const ROOT_IOS = path . join ( process . cwd ( ) , 'nitrogen' , 'generated' , 'ios' ) ;
27+ const ANDROID_ONLOAD_FILE = path . join (
28+ ROOT_ANDROID ,
29+ 'RNGoogleMapsPlusOnLoad.cpp'
30+ ) ;
1631
1732const HYBRID_VIEW_MANAGER = path . join (
18- ROOT_DIR ,
33+ ROOT_ANDROID ,
1934 'kotlin/com/margelo/nitro/rngooglemapsplus/views/HybridRNGoogleMapsPlusViewManager.kt'
2035) ;
2136
37+ const HYBRID_VIEW_COMPONENT_IOS = path . join (
38+ ROOT_IOS ,
39+ 'c++/views/HybridRNGoogleMapsPlusViewComponent.mm'
40+ ) ;
41+
2242const REPLACEMENTS = [
2343 {
2444 regex : / c o m \. m a r g e l o \. n i t r o \. r n g o o g l e m a p s p l u s / g,
@@ -33,14 +53,21 @@ const REPLACEMENTS = [
3353const __filename = fileURLToPath ( import . meta. url ) ;
3454const filename = basename ( __filename ) ;
3555
36- const RECYCLE_METHOD = `
56+ const RECYCLE_METHOD_ANDROID = `
3757 /// added by ${ filename }
3858 override fun prepareToRecycleView(reactContext: ThemedReactContext, view: View): View? {
3959 return null
4060 }
4161` ;
4262
43- // Patch-Routine
63+ const RECYCLE_METHOD_IOS = `
64+ /// added by ${ filename }
65+ + (BOOL)shouldBeRecycled
66+ {
67+ return NO;
68+ }
69+ ` ;
70+
4471async function processFile ( filePath ) {
4572 let content = await readFile ( filePath , 'utf8' ) ;
4673 let updated = content ;
@@ -53,16 +80,30 @@ async function processFile(filePath) {
5380 updated = updated . replace ( / m a r g e l o \/ n i t r o \/ / g, '' ) ;
5481 }
5582
56- console . log ( filePath ) ;
5783 if ( path . resolve ( filePath ) === path . resolve ( HYBRID_VIEW_MANAGER ) ) {
5884 if ( ! / o v e r r i d e f u n p r e p a r e T o R e c y c l e V i e w / . test ( updated ) ) {
5985 const pattern =
6086 / ( o v e r r i d e f u n o n D r o p V i e w I n s t a n c e \( v i e w : V i e w \) \s * \{ [ ^ } ] + \} \s * ) / m;
6187
6288 if ( pattern . test ( updated ) ) {
63- updated = updated . replace ( pattern , `$1${ RECYCLE_METHOD } \n` ) ;
89+ updated = updated . replace ( pattern , `$1${ RECYCLE_METHOD_ANDROID } \n` ) ;
90+ } else {
91+ throw new Error (
92+ `Pattern for "onDropViewInstance" not found in ${ filePath } `
93+ ) ;
94+ }
95+ }
96+ }
97+
98+ if ( path . resolve ( filePath ) === path . resolve ( HYBRID_VIEW_COMPONENT_IOS ) ) {
99+ if ( ! / \+ \s * \( B O O L \) \s * s h o u l d B e R e c y c l e d / . test ( updated ) ) {
100+ const pattern =
101+ / ( - \( i n s t a n c e t y p e \) \s * i n i t \s * \{ (?: [ ^ { } ] | \{ [ ^ { } ] * \} ) * \} ) / m;
102+
103+ if ( pattern . test ( updated ) ) {
104+ updated = updated . replace ( pattern , `$1\n${ RECYCLE_METHOD_IOS } ` ) ;
64105 } else {
65- updated = updated . replace ( / } \s * $ / m , ` ${ RECYCLE_METHOD } \n}\n `) ;
106+ throw new Error ( `Pattern for "init" not found in ${ filePath } `) ;
66107 }
67108 }
68109 }
@@ -87,8 +128,9 @@ async function start(dir) {
87128
88129( async ( ) => {
89130 try {
90- await start ( ROOT_DIR ) ;
91- console . log ( 'All occurrences patched successfully.' ) ;
131+ await start ( ROOT_ANDROID ) ;
132+ await start ( ROOT_IOS ) ;
133+ console . log ( 'All Nitrogen files patched successfully.' ) ;
92134 } catch ( err ) {
93135 console . error ( 'Error while processing files:' , err ) ;
94136 process . exit ( 1 ) ;
0 commit comments