diff --git a/android/build.gradle b/android/build.gradle index 0bebcee..6c60361 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -7,10 +7,40 @@ import java.nio.file.SimpleFileVisitor import java.nio.file.attribute.BasicFileAttributes String getPackageJsonPath() { - return findProperty("APPSFLYER_PACKAGE_JSON") ?: "$rootDir/../node_modules/appsflyer-capacitor-plugin/package.json" + // If APPSFLYER_PACKAGE_JSON_PATH property is set by the developer, use it. + String envPath = findProperty("APPSFLYER_PACKAGE_JSON_PATH") as String + if (envPath) { + logger.lifecycle("package.json path from APPSFLYER_PACKAGE_JSON_PATH: $envPath") + return envPath + } + + // Potential relative paths to the package.json. + def possiblePackageJsonPaths = [ + "${projectDir.parentFile}/package.json", + "${buildDir.parentFile.parentFile}/package.json", + "${rootDir.parentFile}/node_modules/appsflyer-capacitor-plugin/package.json", + ] + + // Loop through the possible paths and find the first one that exists + for (possiblePath in possiblePackageJsonPaths) { + File possibleFile = file(possiblePath) + if (possibleFile.exists()) { + logger.lifecycle("Found package.json at: ${possibleFile.absolutePath}") + return possibleFile.absolutePath + } + } + + logger.lifecycle("Did not locate package.json in any of possiblePackageJsonPaths and APPSFLYER_PACKAGE_JSON_PATH is not specified.") + return null } -def findNodeModulesDir(File currentDir) { +/** + * Finds the node_modules directory by traversing up the directory hierarchy starting from the given directory. + * + * @param currentDir The directory from which to start the search. + * @return The discovered node_modules directory, or null if not found. + */ +static def findNodeModulesDir(File currentDir) { def dir = currentDir while (dir != null) { def nodeModulesDir = new File(dir, 'node_modules') @@ -22,10 +52,20 @@ def findNodeModulesDir(File currentDir) { return null } +/** + * Searches for a package.json file corresponding to the given package name within the node_modules directory. + * + * The search begins from the root project directory and recursively checks each node_modules folder found + * in parent directories up until the root of the file system hierarchy. + * + * @param packageName The name of the package for which to find package.json. + * @return The contents of the package.json as a Map, or null if not found. + */ def findPackageJsonInDep(String packageName) { + // Start the search from the root project directory def nodeModulesDir = findNodeModulesDir(project.rootDir) if (nodeModulesDir == null) { - println "node_modules directory not found in any parent directories." + logger.lifecycle("node_modules directory not found in any parent directories.") return null } @@ -34,68 +74,67 @@ def findPackageJsonInDep(String packageName) { def walker = new SimpleFileVisitor() { @Override FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { + // Looking specifically for the package.json of the target package if (file.toAbsolutePath().endsWith("appsflyer-capacitor-plugin/package.json")) { try { def content = new JsonSlurper().parseText(file.toFile().text) if (content.name == packageName) { - println "Found package.json: ${file.toAbsolutePath()}" + logger.lifecycle("Found package.json at: ${file.toAbsolutePath()}") json = content return FileVisitResult.TERMINATE } } catch (Exception e) { - println "Error parsing JSON in file: ${file.toAbsolutePath().toString()}\n${e.message}\n\t" + logger.lifecycle("Error parsing JSON in file: ${file.toAbsolutePath().toString()}\n${e.message}\n\t") } } return FileVisitResult.CONTINUE } } + // Recursively walk through the file system starting from the node_modules directory while (json == null && nodeModulesDir != null) { Files.walkFileTree(nodeModulesDir.toPath(), walker) - // parentFile will give us exact same directory so we have to go 2 level upper - // and find another node_modules + // Search for another node_modules directory two levels up nodeModulesDir = findNodeModulesDir(nodeModulesDir.parentFile.parentFile) } return json } def getPackageJson() { - def packageJson - def inputFile = new File(getPackageJsonPath()) - if (inputFile.exists()) { - println "found package.json from ENV variable" - packageJson = new JsonSlurper().parseText(inputFile.text) + // Use the safe navigation operator when working with possible null values + String packageJsonPath = getPackageJsonPath() + File inputFile = packageJsonPath ? new File(packageJsonPath) : null + + // Attempt to load the package.json if the path is found and the file exists + if (inputFile?.exists()) { + try { + return new JsonSlurper().parseText(inputFile.getText('UTF-8')) + } catch (Exception e) { + logger.error("Failed to parse package.json at: ${inputFile.absolutePath}", e) + } } else { - println "could not found package.json from ENV variable" - println "searching for package.json recursively" - packageJson = findPackageJsonInDep("appsflyer-capacitor-plugin") + // package.json not found; search recursively + logger.lifecycle("Searching for package.json recursively in node_modules directories...") + def foundJson = findPackageJsonInDep("appsflyer-capacitor-plugin") + if (foundJson) { + return foundJson + } } - return packageJson -} -// Create an easy to use function -def getVersionFromNpm() { - // Return the version, you can get any value this way - return getPackageJson()["version"] -} - -// Create an easy to use function -def getSDKVersionFromNpm() { - // Return the version, you can get any value this way - return getPackageJson()["androidSdkVersion"] -} - -def getPluginBuildVersionFromNpm() { - // Return the version, you can get any value this way - return getPackageJson()["buildNumber"] + // If we got here the package.json was not loaded, log an error and return null + logger.error("The plugin package.json could not be loaded properly; appsflyer-capacitor-plugin may not function as expected.") + return null } ext { + // Initialize only once and reuse it for all subsequent calls + packageJson = getPackageJson() + junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2' androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.6.1' androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.5' androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.5.1' - af_sdk_version = getSDKVersionFromNpm() - plugin_version = getVersionFromNpm() - plugin_build_version = getPluginBuildVersionFromNpm() + af_sdk_version = packageJson?.androidSdkVersion + plugin_version = packageJson?.version + plugin_build_version = packageJson?.buildNumber } buildscript {