From e301f9e3c574c024d521f674b76d813e3cab7291 Mon Sep 17 00:00:00 2001 From: Krisztiaan Date: Thu, 27 Jan 2022 23:38:44 +0100 Subject: [PATCH 1/5] fix: `cliPath` should handle absolute paths --- react.gradle | 3 +++ 1 file changed, 3 insertions(+) diff --git a/react.gradle b/react.gradle index 0ace9be826f805..de8a8c3a668fb3 100644 --- a/react.gradle +++ b/react.gradle @@ -26,6 +26,9 @@ def detectEntryFile(config) { */ def detectCliPath(config) { if (config.cliPath) { + if (config.cliPath.startsWith("/") { + return config.cliPath + } return "${projectDir}/${config.cliPath}" } if (new File("${projectDir}/../../node_modules/react-native/cli.js").exists()) { From bbe5c531cb2a43244fc6486c0478e8f75e5fac90 Mon Sep 17 00:00:00 2001 From: Krisztiaan Date: Thu, 27 Jan 2022 23:56:48 +0100 Subject: [PATCH 2/5] fix: groovy --- react.gradle | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/react.gradle b/react.gradle index de8a8c3a668fb3..d5ca139e910e0b 100644 --- a/react.gradle +++ b/react.gradle @@ -26,10 +26,7 @@ def detectEntryFile(config) { */ def detectCliPath(config) { if (config.cliPath) { - if (config.cliPath.startsWith("/") { - return config.cliPath - } - return "${projectDir}/${config.cliPath}" + return (config.cliPath.startsWith("/")) ? config.cliPath : "${projectDir}/${config.cliPath}" } if (new File("${projectDir}/../../node_modules/react-native/cli.js").exists()) { return "${projectDir}/../../node_modules/react-native/cli.js" From 70e57c0827d214f2a9997f76bfc78c84a4bd63e9 Mon Sep 17 00:00:00 2001 From: Krisztiaan Date: Fri, 28 Jan 2022 22:40:32 +0100 Subject: [PATCH 3/5] feat: extend and harmonize cli detection android --- .../com/facebook/react/utils/PathUtils.kt | 18 ++++++++- .../com/facebook/react/utils/PathUtilsTest.kt | 38 ++++++++++++++++++- react.gradle | 31 +++++++++++++-- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt index eb965bc0602e6e..1f620aabb35b8f 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt @@ -68,7 +68,18 @@ private fun detectCliPath( ): String { // 1. preconfigured path if (preconfiguredCliPath != null) { - return File(projectDir, preconfiguredCliPath).toString() + val preconfiguredCliJsAbsolute = File(preconfiguredCliPath) + if (preconfiguredCliJsAbsolute.exists()) { + return preconfiguredCliJsAbsolute.absolutePath + } + val preconfiguredCliJsRelativeToReactRoot = File(reactRoot, preconfiguredCliPath) + if (preconfiguredCliJsRelativeToReactRoot.exists()) { + return preconfiguredCliJsRelativeToReactRoot.absolutePath + } + val preconfiguredCliJsRelativeToProject = File(projectDir, preconfiguredCliPath) + if (preconfiguredCliJsRelativeToProject.exists()) { + return preconfiguredCliJsRelativeToProject.absolutePath + } } // 2. node module path @@ -82,7 +93,10 @@ private fun detectCliPath( val nodeProcessOutput = nodeProcess.inputStream.use { it.bufferedReader().readText().trim() } if (nodeProcessOutput.isNotEmpty()) { - return nodeProcessOutput + val nodeModuleCliJs = File(nodeProcessOutput) + if (nodeModuleCliJs.exists()) { + return nodeModuleCliJs.absolutePath + } } // 3. cli.js in the root folder diff --git a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt index c87d5fa3c6e676..4ce8b1d2129c3d 100644 --- a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt +++ b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt @@ -52,10 +52,44 @@ class PathUtilsTest { } @Test - fun detectedCliPath_withCliPathFromExtension() { + fun detectedCliPath_withCliPathFromExtensionAbsolute() { val project = ProjectBuilder.builder().build() val extension = TestReactExtension(project) - val expected = File(project.projectDir, "fake-cli.sh") + val expected = File(project.projectDir, "abs/fake-cli.sh").apply { + parentFile.mkdirs() + writeText("") + } + extension.cliPath.set(project.projectDir + "/abs/fake-cli.sh") + + val actual = detectedCliPath(project.projectDir, extension) + + assertEquals(expected.toString(), actual) + } + + @Test + fun detectedCliPath_withCliPathFromExtensionInReactFolder() { + val project = ProjectBuilder.builder().build() + val extension = TestReactExtension(project) + val expected = File(project.projectDir, "/react-root/fake-cli.sh").apply { + parentFile.mkdirs() + writeText("") + } + extension.cliPath.set("fake-cli.sh") + extension.reactRoot.set(project.projectDir + "/react-root") + + val actual = detectedCliPath(project.projectDir, extension) + + assertEquals(expected.toString(), actual) + } + + @Test + fun detectedCliPath_withCliPathFromExtensionInProjectFolder() { + val project = ProjectBuilder.builder().build() + val extension = TestReactExtension(project) + val expected = File(project.projectDir, "fake-cli.sh").apply { + parentFile.mkdirs() + writeText("") + } extension.cliPath.set("fake-cli.sh") val actual = detectedCliPath(project.projectDir, extension) diff --git a/react.gradle b/react.gradle index d5ca139e910e0b..922be453199fd4 100644 --- a/react.gradle +++ b/react.gradle @@ -25,14 +25,37 @@ def detectEntryFile(config) { * Detects CLI location in a similar fashion to the React Native CLI */ def detectCliPath(config) { + // 1. preconfigured path if (config.cliPath) { - return (config.cliPath.startsWith("/")) ? config.cliPath : "${projectDir}/${config.cliPath}" + def cliJsAbsolute = new File(config.cliPath) + if (cliJsAbsolute.exists()) { + return cliJsAbsolute.getAbsolutePath() + } + def cliJsRelativeToRoot = new File("${rootDir}/${config.cliPath}") + if (cliJsRelativeToRoot.exists()) { + return cliJsRelativeToRoot.getAbsolutePath() + } + def cliJsRelativeToProject = new File("${projectDir}/${config.cliPath}") + if (cliJsRelativeToProject.exists()) { + return cliJsRelativeToProject.getAbsolutePath() + } } - if (new File("${projectDir}/../../node_modules/react-native/cli.js").exists()) { - return "${projectDir}/../../node_modules/react-native/cli.js" + + // 2. node module path + def cliJsFromNode = new File(["node", "--print", "require.resolve('react-native/cli').bin"].execute(null, rootDir).text.trim()) + if (cliJsFromNode.exists()) { + return cliJsFromNode.getAbsolutePath() } + + // 3. cli.js in the root folder + def rootCliJs = new File(reactRoot, "node_modules/react-native/cli.js") + if (rootCliJs.exists()) { + return rootCliJs.getAbsolutePath() + } + throw new Exception("Couldn't determine CLI location. " + - "Please set `project.ext.react.cliPath` to the path of the react-native cli.js file. This file typically resides in `node_modules/react-native/cli.js`"); + "Please set `project.ext.react.cliPath` to the path of the react-native cli.js file. " + + "This file typically resides in `node_modules/react-native/cli.js`"); } def composeSourceMapsPath = config.composeSourceMapsPath ?: "node_modules/react-native/scripts/compose-source-maps.js" From 2cdb0415a4ad1bbee10a11ad566d4cc1217d0e7b Mon Sep 17 00:00:00 2001 From: Krisztiaan Date: Mon, 31 Jan 2022 18:07:37 +0100 Subject: [PATCH 4/5] fix: variable needed for `detectCliPath` --- react.gradle | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/react.gradle b/react.gradle index 922be453199fd4..c877faa22d1f1c 100644 --- a/react.gradle +++ b/react.gradle @@ -21,6 +21,16 @@ def detectEntryFile(config) { return "index.js"; } +def composeSourceMapsPath = config.composeSourceMapsPath ?: "node_modules/react-native/scripts/compose-source-maps.js" +def bundleAssetName = config.bundleAssetName ?: "index.android.bundle" +def entryFile = detectEntryFile(config) +def bundleCommand = config.bundleCommand ?: "bundle" +def reactRoot = file(config.root ?: "../../") +def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"] +def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ; +def enableVmCleanup = config.enableVmCleanup == null ? true : config.enableVmCleanup +def hermesCommand = config.hermesCommand ?: "../../node_modules/hermes-engine/%OS-BIN%/hermesc" + /** * Detects CLI location in a similar fashion to the React Native CLI */ @@ -58,16 +68,6 @@ def detectCliPath(config) { "This file typically resides in `node_modules/react-native/cli.js`"); } -def composeSourceMapsPath = config.composeSourceMapsPath ?: "node_modules/react-native/scripts/compose-source-maps.js" -def bundleAssetName = config.bundleAssetName ?: "index.android.bundle" -def entryFile = detectEntryFile(config) -def bundleCommand = config.bundleCommand ?: "bundle" -def reactRoot = file(config.root ?: "../../") -def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"] -def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ; -def enableVmCleanup = config.enableVmCleanup == null ? true : config.enableVmCleanup -def hermesCommand = config.hermesCommand ?: "../../node_modules/hermes-engine/%OS-BIN%/hermesc" - def reactNativeDevServerPort() { def value = project.getProperties().get("reactNativeDevServerPort") return value != null ? value : "8081" From 9197f8926b253ddcb77d508a17d8d2d78147055b Mon Sep 17 00:00:00 2001 From: Krisztiaan Date: Mon, 14 Feb 2022 09:44:32 +0100 Subject: [PATCH 5/5] fix: pass reactRoot to detectCliPath --- react.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/react.gradle b/react.gradle index c877faa22d1f1c..ae65cc4b338471 100644 --- a/react.gradle +++ b/react.gradle @@ -34,7 +34,7 @@ def hermesCommand = config.hermesCommand ?: "../../node_modules/hermes-engine/%O /** * Detects CLI location in a similar fashion to the React Native CLI */ -def detectCliPath(config) { +def detectCliPath(config, reactRoot) { // 1. preconfigured path if (config.cliPath) { def cliJsAbsolute = new File(config.cliPath) @@ -173,7 +173,7 @@ afterEvaluate { // Additional node and packager commandline arguments def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"] - def cliPath = detectCliPath(config) + def cliPath = detectCliPath(config, reactRoot) def execCommand = []