Skip to content

Commit

Permalink
Introduce findPackageJsonFile to address issue with GenerateCodegenAr…
Browse files Browse the repository at this point in the history
…tifactsTaskTest (#34321)

Summary:
While doing some testing with cipolleschi on React Native 0.70.0 we realized that custom Java Package is not properly propagated when invoking codegen for external libraries.

This PR fixes it.

## Changelog

[Internal] - Introduce findPackageJsonFile to address issue with GenerateCodegenArtifactsTaskTest

Pull Request resolved: #34321

Test Plan: Added JUnit tests + Tested locally with cipolleschi

Reviewed By: cipolleschi

Differential Revision: D38314770

Pulled By: cortinico

fbshipit-source-id: ec2de1ba59ffc8fb0644f422521ced642b38d2c7
  • Loading branch information
cortinico authored and facebook-github-bot committed Aug 1, 2022
1 parent 5c81866 commit 6f76122
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.facebook.react.tasks.BuildCodegenCLITask
import com.facebook.react.tasks.GenerateCodegenArtifactsTask
import com.facebook.react.tasks.GenerateCodegenSchemaTask
import com.facebook.react.utils.JsonUtils
import com.facebook.react.utils.findPackageJsonFile
import java.io.File
import kotlin.system.exitProcess
import org.gradle.api.Plugin
Expand Down Expand Up @@ -93,15 +94,7 @@ class ReactPlugin : Plugin<Project> {
// We're reading the package.json at configuration time to properly feed
// the `jsRootDir` @Input property of this task. Therefore, the
// parsePackageJson should be invoked inside this lambda.
// We look for `package.json` in the parent folder of this Gradle module
// (generally the case for library projects) or we fallback to looking into the `root`
// folder of a React Native project (generally the case for app projects).
val packageJson =
if (project.file("../package.json").exists()) {
project.file("../package.json")
} else {
extension.root.file("package.json").orNull?.asFile
}
val packageJson = findPackageJsonFile(project, extension)
val parsedPackageJson = packageJson?.let { JsonUtils.fromCodegenJson(it) }

val jsSrcsDirInPackageJson = parsedPackageJson?.codegenConfig?.jsSrcsDir
Expand All @@ -122,7 +115,7 @@ class ReactPlugin : Plugin<Project> {
it.nodeExecutableAndArgs.set(extension.nodeExecutableAndArgs)
it.codegenDir.set(extension.codegenDir)
it.generatedSrcDir.set(generatedSrcDir)
it.packageJsonFile.set(extension.root.file("package.json"))
it.packageJsonFile.set(findPackageJsonFile(project, extension))
it.codegenJavaPackageName.set(extension.codegenJavaPackageName)
it.libraryName.set(extension.libraryName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package com.facebook.react.utils

import com.facebook.react.ReactExtension
import java.io.File
import org.gradle.api.Project

/**
* Computes the entry file for React Native. The Algo follows this order:
Expand Down Expand Up @@ -190,6 +191,18 @@ internal fun projectPathToLibraryName(projectPath: String): String =
.joinToString("") { token -> token.replaceFirstChar { it.uppercase() } }
.plus("Spec")

/**
* Function to look for the relevant `package.json`. We first look in the parent folder of this
* Gradle module (generally the case for library projects) or we fallback to looking into the `root`
* folder of a React Native project (generally the case for app projects).
*/
internal fun findPackageJsonFile(project: Project, extension: ReactExtension): File? =
if (project.file("../package.json").exists()) {
project.file("../package.json")
} else {
extension.root.file("package.json").orNull?.asFile
}

private const val HERMESC_IN_REACT_NATIVE_PATH =
"node_modules/react-native/sdks/hermesc/%OS-BIN%/hermesc"
private const val HERMESC_BUILT_FROM_SOURCE_PATH =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.facebook.react.utils

import com.facebook.react.ReactExtension
import com.facebook.react.TestReactExtension
import com.facebook.react.tests.OS
import com.facebook.react.tests.OsRule
Expand Down Expand Up @@ -242,4 +243,29 @@ class PathUtilsTest {
File("/home/circleci/hermes/build/bin/hermesc"),
getBuiltHermescFile(tempFolder.root, "/home/circleci/hermes"))
}

@Test
fun findPackageJsonFile_withFileInParentFolder_picksItUp() {
tempFolder.newFile("package.json")
val moduleFolder = tempFolder.newFolder("awesome-module")

val project = ProjectBuilder.builder().withProjectDir(moduleFolder).build()
project.plugins.apply("com.facebook.react")
val extension = project.extensions.getByType(ReactExtension::class.java)

assertEquals(project.file("../package.json"), findPackageJsonFile(project, extension))
}

@Test
fun findPackageJsonFile_withFileConfiguredInExtension_picksItUp() {
val moduleFolder = tempFolder.newFolder("awesome-module")
val localFile = File(moduleFolder, "package.json").apply { writeText("{}") }

val project = ProjectBuilder.builder().withProjectDir(moduleFolder).build()
project.plugins.apply("com.facebook.react")
val extension =
project.extensions.getByType(ReactExtension::class.java).apply { root.set(moduleFolder) }

assertEquals(localFile, findPackageJsonFile(project, extension))
}
}

0 comments on commit 6f76122

Please sign in to comment.