Codegen Gradle: add toggle to activate JavaGenerator implementation

Summary:
JavaGenerator is a Java-based implementation for generating codegen output from the parsed schema file. Right now the output is a hardcoded Java file. In the next commits, proper JavaGenerator impl will be added.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D23100171

fbshipit-source-id: 1bef23e3dba4d8c222ebdece0edeb4435d388cd4
This commit is contained in:
Kevin Gozali 2020-08-13 14:49:52 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 1f15b8b6c2
Коммит 3f1a4535d9
5 изменённых файлов: 49 добавлений и 24 удалений

Просмотреть файл

@ -229,4 +229,5 @@ react {
enableCodegen = System.getenv("USE_CODEGEN") ?: false enableCodegen = System.getenv("USE_CODEGEN") ?: false
jsRootDir = file("$rootDir/RNTester") jsRootDir = file("$rootDir/RNTester")
reactNativeRootDir = file("$rootDir") reactNativeRootDir = file("$rootDir")
useJavaGenerator = System.getenv("USE_CODEGEN_JAVAPOET") ?: false
} }

Просмотреть файл

@ -476,4 +476,5 @@ react {
enableCodegen = System.getenv("USE_CODEGEN") ?: false enableCodegen = System.getenv("USE_CODEGEN") ?: false
jsRootDir = file("../Libraries") jsRootDir = file("../Libraries")
reactNativeRootDir = file("$rootDir") reactNativeRootDir = file("$rootDir")
useJavaGenerator = System.getenv("USE_CODEGEN_JAVAPOET") ?: false
} }

Просмотреть файл

@ -10,23 +10,24 @@ package com.facebook.react.codegen.generator;
import com.squareup.javapoet.JavaFile; import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec; import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec; import com.squareup.javapoet.TypeSpec;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Paths;
import javax.lang.model.element.Modifier; import javax.lang.model.element.Modifier;
// TODO: Implement proper generator - this is a sample usage of JavaPoet // TODO: Implement proper generator - this is a sample usage of JavaPoet
public final class JavaGenerator { public final class JavaGenerator {
private final File mSchemaFile;
private final String mJavaPackageName;
private final File mOutputDir;
private String mSchemaFilePath; public JavaGenerator(final File schemaFile, final String javaPackageName, final File outputDir) {
private String mOutputDir; mSchemaFile = schemaFile;
mJavaPackageName = javaPackageName;
public JavaGenerator(String schemaFilePath, String outputDir) {
mSchemaFilePath = schemaFilePath;
mOutputDir = outputDir; mOutputDir = outputDir;
} }
public void build() throws IOException { public void build() throws IOException {
MethodSpec main = final MethodSpec main =
MethodSpec.methodBuilder("main") MethodSpec.methodBuilder("main")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC) .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(void.class) .returns(void.class)
@ -34,18 +35,16 @@ public final class JavaGenerator {
.addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!") .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!")
.build(); .build();
TypeSpec helloWorld = final TypeSpec helloWorld =
TypeSpec.classBuilder("ReactNativeCodegen") TypeSpec.classBuilder("ReactNativeCodegen")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL) .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addMethod(main) .addMethod(main)
.build(); .build();
JavaFile javaFile = JavaFile.builder("com.facebook.react.codegen", helloWorld).build(); final JavaFile javaFile = JavaFile.builder(mJavaPackageName, helloWorld).build();
System.out.println(javaFile.toString()); System.out.println(javaFile.toString());
if (!mOutputDir.isEmpty()) { javaFile.writeTo(mOutputDir);
javaFile.writeToPath(Paths.get(mOutputDir));
}
} }
} }

Просмотреть файл

@ -8,9 +8,11 @@
package com.facebook.react.codegen.plugin; package com.facebook.react.codegen.plugin;
import com.android.build.gradle.BaseExtension; import com.android.build.gradle.BaseExtension;
import com.facebook.react.codegen.generator.JavaGenerator;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import java.io.File; import java.io.File;
import java.io.IOException;
import org.gradle.api.Plugin; import org.gradle.api.Plugin;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.Task; import org.gradle.api.Task;
@ -95,16 +97,25 @@ public class CodegenPlugin implements Plugin<Project> {
task.getInputs().files(generatedSchemaFile); task.getInputs().files(generatedSchemaFile);
task.getOutputs().dir(outputDir); task.getOutputs().dir(outputDir);
ImmutableList<String> execCommands = if (extension.useJavaGenerator) {
new ImmutableList.Builder<String>() generateJavaFromSchemaWithJavaGenerator(
.add("yarn") generatedSchemaFile,
.addAll(ImmutableList.copyOf(extension.nodeExecutableAndArgs)) extension.codegenJavaPackageName,
.add(extension.codegenGenerateNativeModuleSpecsCLI().getAbsolutePath()) new File(generatedSrcDir, "java"));
.add("android") // TODO: generate JNI C++ files.
.add(generatedSchemaFile.getAbsolutePath()) task.commandLine("echo");
.add(outputDir.getAbsolutePath()) } else {
.build(); ImmutableList<String> execCommands =
task.commandLine(execCommands); new ImmutableList.Builder<String>()
.add("yarn")
.addAll(ImmutableList.copyOf(extension.nodeExecutableAndArgs))
.add(extension.codegenGenerateNativeModuleSpecsCLI().getAbsolutePath())
.add("android")
.add(generatedSchemaFile.getAbsolutePath())
.add(outputDir.getAbsolutePath())
.build();
task.commandLine(execCommands);
}
}); });
// 4. Add dependencies & generated sources to the project. // 4. Add dependencies & generated sources to the project.
@ -140,4 +151,16 @@ public class CodegenPlugin implements Plugin<Project> {
// TODO: Add JNI sources. // TODO: Add JNI sources.
}); });
} }
// Use Java-based generator implementation to produce the source files, instead of using the
// JS-based generator.
private void generateJavaFromSchemaWithJavaGenerator(
final File schemaFile, final String javaPackageName, final File outputDir) {
final JavaGenerator generator = new JavaGenerator(schemaFile, javaPackageName, outputDir);
try {
generator.build();
} catch (final IOException e) {
// Ignore for now.
}
}
} }

Просмотреть файл

@ -13,12 +13,13 @@ import org.gradle.api.Project;
public class CodegenPluginExtension { public class CodegenPluginExtension {
// TODO: Remove beta. // TODO: Remove beta.
public String codegenJavaPackageName = "com.facebook.fbreact.specs.beta"; public String codegenJavaPackageName = "com.facebook.fbreact.specs.beta";
public boolean enableCodegen; public boolean enableCodegen = false;
public File jsRootDir; public File jsRootDir;
public String[] nodeExecutableAndArgs = {"node"}; public String[] nodeExecutableAndArgs = {"node"};
public File reactNativeRootDir; public File reactNativeRootDir;
public boolean useJavaGenerator = false;
public CodegenPluginExtension(Project project) { public CodegenPluginExtension(final Project project) {
this.reactNativeRootDir = new File(project.getRootDir(), "node_modules/react-native"); this.reactNativeRootDir = new File(project.getRootDir(), "node_modules/react-native");
} }