Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 4x 5x 5x 9x 9x 9x 6x 3x 3x 9x 9x 9x 4x 5x 5x 9x 6x 3x 3x 9x 6x 3x 3x 9x 6x 3x 3x 9x | import { projectConfig } from '@react-native-community/cli-platform-android';
import fs from 'fs-extra';
import path from 'path';
import StateManager from '../StateManager';
import { ResizeMode } from '../constants';
import { insert, replace } from '../string-helpers';
/**
* Injects specific code to MainActivity that would trigger SplashScreen mounting process.
*/
export default async function configureMainActivity(
projectRootPath: string,
resizeMode: ResizeMode
) {
// eslint-disable-next-line
const mainApplicationPath = projectConfig(projectRootPath)?.mainFilePath;
Iif (!mainApplicationPath) {
throw new Error(`Failed to configure 'MainActivity'.`);
}
const mainActivityPathJava = path.resolve(mainApplicationPath, '../MainActivity.java');
const mainActivityPathKotlin = path.resolve(mainApplicationPath, '../MainActivity.kt');
const isJava = await fs.pathExists(mainActivityPathJava);
const isKotlin = !isJava && (await fs.pathExists(mainActivityPathKotlin));
Iif (!isJava && !isKotlin) {
throw new Error(`Failed to find 'MainActivity' file.`);
}
const fileContent = await fs.readFile(
isJava ? mainActivityPathJava : mainActivityPathKotlin,
'utf-8'
);
const { state: newFileContent } = new StateManager<string, boolean>(fileContent)
// importing SplashScreen
.applyAction(content => {
const [succeeded, newContent] = replace(content, {
replacePattern: /^import expo\.modules\.splashscreen\.SplashScreen.*?\nimport expo\.modules\.splashscreen\.SplashScreenImageResizeMode.*?$/m,
replaceContent: `import expo.modules.splashscreen.SplashScreen${isJava ? ';' : ''}
import expo.modules.splashscreen.SplashScreenImageResizeMode${isJava ? ';' : ''}`,
});
return [newContent, 'replacedSplashImports', succeeded];
})
.applyAction((content, { replacedSplashImports }) => {
if (replacedSplashImports) {
return [content, 'insertedSplashImports', false];
}
const [succeeded, newContent] = insert(content, {
insertPattern: isJava ? /(?=public class .* extends .* {.*$)/m : /(?=class .* : .* {.*$)/m,
insertContent: `import expo.modules.splashscreen.SplashScreen${isJava ? ';' : ''}
import expo.modules.splashscreen.SplashScreenImageResizeMode${isJava ? ';' : ''}
`,
});
return [newContent, 'insertedSplashImports', succeeded];
})
// importing ReactRootView
.applyAction(content => {
const [succeeded, newContent] = replace(content, {
replacePattern: /^import com\.facebook\.react\.ReactRootView.*?$/m,
replaceContent: `import com.facebook.react.ReactRootView${isJava ? ';' : ''}`,
});
return [newContent, 'replacedReactImport', succeeded];
})
.applyAction((content, { replacedReactImport }) => {
if (replacedReactImport) {
return [content, 'insertedReactImport', false];
}
const [succeeded, newContent] = insert(content, {
insertPattern: /(?<=import com\.facebook\.react\.ReactActivity.*?$)/m,
insertContent: `\nimport com.facebook.react.ReactRootView${isJava ? ';' : ''}`,
});
return [newContent, 'insertedReactImport', succeeded];
})
// registering SplashScreen in onCreate()
.applyAction(content => {
const [succeeded, newContent] = replace(content, {
replacePattern: /(?<=super\.onCreate(.|\n)*?)SplashScreen\.show\(this, SplashScreenImageResizeMode\..*\).*$/m,
replaceContent: `SplashScreen.show(this, SplashScreenImageResizeMode.${resizeMode.toUpperCase()}, ${
isJava ? 'ReactRootView.class);' : 'ReactRootView::class.java)'
}`,
});
return [newContent, 'replacedInOnCreate', succeeded];
})
.applyAction((content, { replacedInOnCreate }) => {
if (replacedInOnCreate) {
return [content, 'insertedInOnCreate', false];
}
const [succeeded, newContent] = insert(content, {
insertPattern: /(?<=^.*super\.onCreate.*$)/m, // insert just below super.onCreate
insertContent: `
// SplashScreen.show(...) has to be called after super.onCreate(...)
// Below line is handled by '@expo/configure-splash-screen' command and it's discouraged to modify it manually
SplashScreen.show(this, SplashScreenImageResizeMode.${resizeMode.toUpperCase()}, ${
isJava ? 'ReactRootView.class);' : 'ReactRootView::class.java)'
}`,
});
return [newContent, 'insertedInOnCreate', succeeded];
})
// inserting basic onCreate()
.applyAction((content, { replacedInOnCreate, insertedInOnCreate }) => {
if (replacedInOnCreate || insertedInOnCreate) {
return [content, 'insertedOnCreate', false];
}
const [succeeded, newContent] = insert(content, {
insertPattern: isJava
? /(?<=public class .* extends .* {.*$)/m
: /(?<=class .* : .* {.*$)/m,
insertContent: `
${
isJava
? `@Override
protected void onCreate(Bundle savedInstanceState`
: 'override fun onCreate(savedInstanceState: Bundle?'
}) {
super.onCreate(savedInstanceState)${isJava ? ';' : ''}
// SplashScreen.show(...) has to be called after super.onCreate(...)
// Below line is handled by '@expo/configure-splash-screen' command and it's discouraged to modify it manually
SplashScreen.show(this, SplashScreenImageResizeMode.${resizeMode.toUpperCase()}, ${
isJava ? 'ReactRootView.class);' : 'ReactRootView::class.java)'
}
}
`,
});
return [newContent, 'insertedOnCreate', succeeded];
})
// importing Bundle
.applyAction((content, { replacedInOnCreate, insertedInOnCreate }) => {
if (replacedInOnCreate || insertedInOnCreate) {
return [content, 'replacedBundleImport', false];
}
const [succeeded, newContent] = replace(content, {
replacePattern: /import android\.os\.Bundle/m,
replaceContent: 'import android.os.Bundle',
});
return [newContent, 'replacedBundleImport', succeeded];
})
.applyAction((content, { replacedInOnCreate, insertedInOnCreate }) => {
if (replacedInOnCreate || insertedInOnCreate) {
return [content, 'insertedBundleImport', false];
}
const [succeeded, newContent] = insert(content, {
insertPattern: /(?<=(^.*?package .*?$))/m,
insertContent: `\n\nimport android.os.Bundle${isJava ? ';' : ''}`,
});
return [newContent, 'insertedBundleImport', succeeded];
});
await fs.writeFile(isJava ? mainActivityPathJava : mainActivityPathKotlin, newFileContent);
}
|