Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuring databases for Flipper on Android #14

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ In your app.json `plugins` array:

```json
{
"plugins": ["@morrowdigital/watermelondb-expo-plugin",
"plugins": [
[
"@morrowdigital/watermelondb-expo-plugin",
{
"databases": ["morrow.db"]
}
],
[
"expo-build-properties",
{
Expand Down
41 changes: 41 additions & 0 deletions build/withWatermelon.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,46 @@ function setAndroidMainApplication(config) {
},
]);
}
/**
* Platform: Android
* */
function addFlipperDb(config, databases) {
return (0, config_plugins_1.withDangerousMod)(config, [
"android",
async (config) => {
// short circuit if no databases specified
if (!databases?.length)
return config;
const root = config.modRequest.platformProjectRoot;
const filePath = `${root}/app/src/debug/java/${config?.android?.package?.replace(/\./g, "/")}/ReactNativeFlipper.java`;
const contents = await fs.readFile(filePath, "utf-8");
// Add imports
let updated = (0, insertLinesHelper_1.insertLinesHelper)(`import com.facebook.flipper.plugins.databases.impl.SqliteDatabaseDriver;
import com.facebook.flipper.plugins.databases.impl.SqliteDatabaseProvider;
import java.io.File;
import java.util.List;
import java.util.ArrayList;`, "import okhttp3.OkHttpClient;", contents);
// Replace DatabasesFlipperPlugin with custom driver
const addDatabases = databases
.map((d) => `databaseFiles.add(new File(context.getDatabasePath("${d}").getPath().replace("/databases", "")));`)
.join("\n ");
updated = (0, insertLinesHelper_1.insertLinesHelper)(` client.addPlugin(new DatabasesFlipperPlugin(new SqliteDatabaseDriver(context, new SqliteDatabaseProvider() {
@Override
public List<File> getDatabaseFiles() {
List<File> databaseFiles = new ArrayList<>();
for (String databaseName : context.databaseList()) {
databaseFiles.add(context.getDatabasePath(databaseName));
}
${addDatabases}
return databaseFiles;
}
})));`, "client.addPlugin(new DatabasesFlipperPlugin(context));", updated, 0, // replace original plugin
1);
await fs.writeFile(filePath, updated);
return config;
},
]);
}
/**
* Platform: iOS
* */
Expand Down Expand Up @@ -127,6 +167,7 @@ exports.default = (config, options) => {
// config = setAppSettingBuildGradle(config);
// config = setAppBuildGradle(config);
config = setAndroidMainApplication(config);
config = addFlipperDb(config, options?.databases ?? []);
config = setAppDelegate(config);
config = setWmelonBridgingHeader(config);
config = withCocoaPods(config);
Expand Down
7 changes: 6 additions & 1 deletion example/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
"favicon": "./assets/favicon.png"
},
"plugins": [
"@morrowdigital/watermelondb-expo-plugin",
[
"@morrowdigital/watermelondb-expo-plugin",
{
"databases": ["morrow.db"]
}
],
[
"expo-build-properties",
{
Expand Down
65 changes: 65 additions & 0 deletions src/withWatermelon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,70 @@ function setAndroidMainApplication(config: ExportedConfigWithProps) {
]);
}

/**
* Platform: Android
* */
function addFlipperDb(config: ExportedConfigWithProps, databases: string[]) {
return withDangerousMod(config, [
"android",
async (config) => {
// short circuit if no databases specified
if (!databases?.length) return config;

const root = config.modRequest.platformProjectRoot;
const filePath = `${root}/app/src/debug/java/${config?.android?.package?.replace(
/\./g,
"/"
)}/ReactNativeFlipper.java`;

const contents = await fs.readFile(filePath, "utf-8");

// Add imports

let updated = insertLinesHelper(
`import com.facebook.flipper.plugins.databases.impl.SqliteDatabaseDriver;
import com.facebook.flipper.plugins.databases.impl.SqliteDatabaseProvider;
import java.io.File;
import java.util.List;
import java.util.ArrayList;`,
"import okhttp3.OkHttpClient;",
contents
);

// Replace DatabasesFlipperPlugin with custom driver

const addDatabases = databases
.map(
(d) =>
`databaseFiles.add(new File(context.getDatabasePath("${d}").getPath().replace("/databases", "")));`
)
.join("\n ");

updated = insertLinesHelper(
` client.addPlugin(new DatabasesFlipperPlugin(new SqliteDatabaseDriver(context, new SqliteDatabaseProvider() {
@Override
public List<File> getDatabaseFiles() {
List<File> databaseFiles = new ArrayList<>();
for (String databaseName : context.databaseList()) {
databaseFiles.add(context.getDatabasePath(databaseName));
}
${addDatabases}
return databaseFiles;
}
})));`,
"client.addPlugin(new DatabasesFlipperPlugin(context));",
updated,
0, // replace original plugin
1
);

await fs.writeFile(filePath, updated);

return config;
},
]);
}

/**
* Platform: iOS
* */
Expand Down Expand Up @@ -170,6 +234,7 @@ export default (config, options) => {
// config = setAppSettingBuildGradle(config);
// config = setAppBuildGradle(config);
config = setAndroidMainApplication(config);
config = addFlipperDb(config, options?.databases ?? []);
config = setAppDelegate(config);
config = setWmelonBridgingHeader(config);
config = withCocoaPods(config);
Expand Down