Skip to content

Commit ddbd12a

Browse files
authored
Merge pull request #295 from OP-Engineering/oscar/traverse-directories-for-config-in-android
Traverse directories on Android upwards to find op-sqlite
2 parents dfcd28e + 0aa4e8b commit ddbd12a

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

android/build.gradle

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,37 @@ def useSqliteVec = false
3737
def enableRtree = false
3838
def tokenizers = []
3939

40-
def isInsideNodeModules = rootDir.absolutePath.contains("node_modules")
41-
def packageJson
42-
43-
if ( isInsideNodeModules ) {
44-
def packageJsonFile = new File("$rootDir/../../../package.json")
45-
packageJson = new JsonSlurper().parseText(packageJsonFile.text)
40+
// On the example app, the package.json is located at the root of the project
41+
// On the user app, the package.json is located at the root of the node_modules directory
42+
def isUserApp = rootDir.absolutePath.contains("node_modules")
43+
def packageJsonFile
44+
45+
if (isUserApp) {
46+
// Start from the root + 1 level up (to avoid detecting the op-sqlite/package.json) and traverse upwards to find the first package.json
47+
File currentDir = new File("$rootDir/../")
48+
packageJsonFile = null
49+
50+
// Try to find package.json by traversing upwards
51+
while (currentDir != null) {
52+
File potential = new File(currentDir, "package.json")
53+
if (potential.exists()) {
54+
packageJsonFile = potential
55+
break
56+
}
57+
currentDir = currentDir.parentFile
58+
}
4659
} else {
47-
def packageJsonFile = new File("$rootDir/../package.json")
48-
packageJson = new JsonSlurper().parseText(packageJsonFile.text)
60+
packageJsonFile = new File("$rootDir/../package.json")
4961
}
5062

63+
64+
def packageJson = new JsonSlurper().parseText(packageJsonFile.text)
65+
5166
def opsqliteConfig = packageJson["op-sqlite"]
67+
5268
if(opsqliteConfig) {
69+
println "[OP-SQLITE] Detected op-sqlite config from package.json at: " + packageJsonFile.absolutePath
70+
5371
useSQLCipher = opsqliteConfig["sqlcipher"]
5472
useCRSQLite = opsqliteConfig["crsqlite"]
5573
useSqliteVec = opsqliteConfig["sqliteVec"]
@@ -177,7 +195,7 @@ android {
177195
// def tokenizerInitStrings = 0
178196
def tokenizersHeaderPath = 0
179197
if (!tokenizers.isEmpty()) {
180-
def sourceDir = isInsideNodeModules ? file("$rootDir/../../../c_sources") : file("$rootDir/../c_sources")
198+
def sourceDir = isUserApp ? file("$rootDir/../../../c_sources") : file("$rootDir/../c_sources")
181199
def destDir = file("$buildscript.sourceFile.parentFile/c_sources")
182200
copy {
183201
from sourceDir

0 commit comments

Comments
 (0)