11const fs = require ( 'fs-extra' ) ;
2- const path = require ( 'path' ) ;
32const compose = require ( 'lodash.flowright' ) ;
4-
5- const SETTINGS_PATCH_PATTERN = `include ':app'` ;
6- const BUILD_PATCH_PATTERN = `dependencies {` ;
7- const MAIN_ACTIVITY_IMPORT_PATTERN = `import android.app.Activity;` ;
8- const MAIN_ACTIVITY_PACKAGE_PATTERN = `.addPackage(new MainReactPackage())` ;
3+ const getPrefix = require ( './getPrefix' ) ;
94
105const readFile = ( file ) =>
116 ( ) => fs . readFileSync ( file , 'utf8' ) ;
@@ -14,81 +9,49 @@ const writeFile = (file, content) => content ?
149 fs . writeFileSync ( file , content , 'utf8' ) :
1510 ( c ) => fs . writeFileSync ( file , c , 'utf8' ) ;
1611
17- const replace = ( scope , pattern , patch ) =>
18- scope . replace ( pattern , `${ pattern } \n${ patch } ` ) ;
19-
2012module . exports = function registerNativeAndroidModule ( name , dependencyConfig , projectConfig ) {
21- const BUILD_PATCH = ` compile project(':${ name } ')` ;
22- const SETTINGS_PATCH = `include ':${ name } '\n` +
23- `project(':${ name } ').projectDir = ` +
24- `new File(rootProject.projectDir, '../node_modules/${ name } /${ dependencyConfig . sourceDir } ')` ;
25-
26- /**
27- * Replace SETTINGS_PATCH_PATTERN by patch in the passed content
28- * @param {String } content Content of the Settings.gradle file
29- * @return {String } Patched content of Settings.gradle
30- */
31- const patchProjectSettings = ( content ) =>
32- replace ( content , SETTINGS_PATCH_PATTERN , SETTINGS_PATCH ) ;
33-
34- /**
35- * Replace BUILD_PATCH_PATTERN by patch in the passed content
36- * @param {String } content Content of the Build.gradle file
37- * @return {String } Patched content of Build.gradle
38- */
39- const patchProjectBuild = ( content ) =>
40- replace ( content , BUILD_PATCH_PATTERN , BUILD_PATCH ) ;
41-
42- const getMainActivityPatch = ( ) =>
43- ` .addPackage(${ dependencyConfig . packageInstance } )` ;
13+ const prefix = getPrefix ( projectConfig ) ;
4414
45- /**
46- * Make a MainActivity.java program patcher
47- * @param {String } importPath Import path, e.g. com.oblador.vectoricons.VectorIconsPackage;
48- * @param {String } instance Code to instance a package, e.g. new VectorIconsPackage();
49- * @return {Function } Patcher function
50- */
51- const makeMainActivityPatcher = ( content ) => {
52- const patched = replace (
53- content , MAIN_ACTIVITY_IMPORT_PATTERN , dependencyConfig . packageImportPath
54- ) ;
15+ const makeSettingsPatch = require ( `./patches/makeSettingsPatch` ) ;
16+ const makeBuildPatch = require ( `./patches/makeBuildPatch` ) ;
17+ const makeMainActivityPatch = require ( `./${ prefix } /makeMainActivityPatch` ) ;
5518
56- return replace (
57- patched , MAIN_ACTIVITY_PACKAGE_PATTERN , getMainActivityPatch ( )
58- ) ;
59- } ;
19+ const applySettingsPatch = makeSettingsPatch . apply ( null , arguments ) ;
20+ const applyBuildPath = makeBuildPatch ( name ) ;
21+ const applyMainActivityPatch = makeMainActivityPatch ( dependencyConfig ) ;
6022
61- const applySettingsGradlePatch = compose (
23+ const performSettingsGradlePatch = compose (
6224 writeFile ( projectConfig . settingsGradlePath ) ,
63- patchProjectSettings ,
25+ applySettingsPatch ,
6426 readFile ( projectConfig . settingsGradlePath )
6527 ) ;
6628
67- const applyBuildGradlePatch = compose (
29+ const performBuildGradlePatch = compose (
6830 writeFile ( projectConfig . buildGradlePath ) ,
69- patchProjectBuild ,
31+ applyBuildPath ,
7032 readFile ( projectConfig . buildGradlePath )
7133 ) ;
7234
73- const applyMainActivityPatch = compose (
35+ const performMainActivityPatch = compose (
7436 writeFile ( projectConfig . mainActivityPath ) ,
75- makeMainActivityPatcher ,
37+ applyMainActivityPatch ,
7638 readFile ( projectConfig . mainActivityPath )
7739 ) ;
7840
7941 /**
8042 * Check if module has been installed already
8143 */
8244 const isInstalled = compose (
83- ( content ) => ~ content . indexOf ( getMainActivityPatch ( ) ) ,
45+ ( content ) => ~ content . indexOf ( dependencyConfig . packageInstance ) ,
8446 readFile ( projectConfig . mainActivityPath )
8547 ) ;
8648
8749 if ( ! isInstalled ( name ) ) {
8850 compose (
89- applySettingsGradlePatch ,
90- applyBuildGradlePatch ,
91- applyMainActivityPatch
51+ performSettingsGradlePatch ,
52+ performBuildGradlePatch ,
53+ performMainActivityPatch
9254 ) ( ) ;
9355 }
9456} ;
57+
0 commit comments