use of com.linkedin.pegasus.gradle.tasks.GenerateRestClientTask in project rest.li by linkedin.
the class PegasusPlugin method configureRestClientGeneration.
// Generate rest client from idl files generated from java source files in the specified source set.
//
// This generates rest client source files from idl file generated from java source files
// in the source set. The generated rest client source files will be in a new source set.
// It also compiles the rest client source files into classes, and creates both the
// rest model and rest client jar files.
//
@SuppressWarnings("deprecation")
protected void configureRestClientGeneration(Project project, SourceSet sourceSet) {
// idl directory for api project
File idlDir = project.file(getIdlPath(project, sourceSet));
if (SharedFileUtils.getSuffixedFiles(project, idlDir, IDL_FILE_SUFFIX).isEmpty()) {
return;
}
File generatedRestClientDir = project.file(getGeneratedDirPath(project, sourceSet, REST_GEN_TYPE) + File.separatorChar + "java");
// always include imported data template jars in compileClasspath of rest client
FileCollection dataModelConfig = getDataModelConfig(project, sourceSet);
// if data templates generated from this source set, add the generated data template jar to compileClasspath
// of rest client.
String dataTemplateSourceSetName = getGeneratedSourceSetName(sourceSet, DATA_TEMPLATE_GEN_TYPE);
Jar dataTemplateJarTask = null;
SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
FileCollection dataModels;
if (sourceSets.findByName(dataTemplateSourceSetName) != null) {
if (debug) {
System.out.println("sourceSet " + sourceSet.getName() + " has generated sourceSet " + dataTemplateSourceSetName);
}
dataTemplateJarTask = (Jar) project.getTasks().getByName(sourceSet.getName() + "DataTemplateJar");
// FIXME change to #getArchiveFile(); breaks backwards-compatibility before 5.1
dataModels = dataModelConfig.plus(project.files(dataTemplateJarTask.getArchivePath()));
} else {
dataModels = dataModelConfig;
}
// create source set for generated rest model, rest client source and class files.
String targetSourceSetName = getGeneratedSourceSetName(sourceSet, REST_GEN_TYPE);
SourceSet targetSourceSet = sourceSets.create(targetSourceSetName, ss -> {
ss.java(sourceDirectorySet -> sourceDirectorySet.srcDir(generatedRestClientDir));
ss.setCompileClasspath(dataModels.plus(project.getConfigurations().getByName("restClientCompile")));
});
project.getPlugins().withType(EclipsePlugin.class, eclipsePlugin -> {
EclipseModel eclipseModel = (EclipseModel) project.getExtensions().findByName("eclipse");
eclipseModel.getClasspath().getPlusConfigurations().add(project.getConfigurations().getByName("restClientCompile"));
});
// idea plugin needs to know about new rest client source directory and its dependencies
addGeneratedDir(project, targetSourceSet, Arrays.asList(getDataModelConfig(project, sourceSet), project.getConfigurations().getByName("restClientCompile")));
// generate the rest client source files
GenerateRestClientTask generateRestClientTask = project.getTasks().create(targetSourceSet.getTaskName("generate", "restClient"), GenerateRestClientTask.class, task -> {
task.dependsOn(project.getConfigurations().getByName("dataTemplate"));
task.setInputDir(idlDir);
task.setResolverPath(dataModels.plus(project.getConfigurations().getByName("restClientCompile")));
task.setRuntimeClasspath(project.getConfigurations().getByName("dataModel").plus(project.getConfigurations().getByName("dataTemplate").getArtifacts().getFiles()));
task.setCodegenClasspath(project.getConfigurations().getByName(PEGASUS_PLUGIN_CONFIGURATION));
task.setDestinationDir(generatedRestClientDir);
task.setRestli2FormatSuppressed(project.hasProperty(SUPPRESS_REST_CLIENT_RESTLI_2));
task.setRestli1FormatSuppressed(project.hasProperty(SUPPRESS_REST_CLIENT_RESTLI_1));
if (isPropertyTrue(project, ENABLE_ARG_FILE)) {
task.setEnableArgFile(true);
}
if (isPropertyTrue(project, CODE_GEN_PATH_CASE_SENSITIVE)) {
task.setGenerateLowercasePath(false);
}
if (isPropertyTrue(project, ENABLE_FLUENT_API)) {
task.setGenerateFluentApi(true);
}
task.doFirst(new CacheableAction<>(t -> project.delete(generatedRestClientDir)));
});
if (dataTemplateJarTask != null) {
generateRestClientTask.dependsOn(dataTemplateJarTask);
}
// TODO: Tighten the types so that _generateSourcesJarTask must be of type Jar.
((Jar) _generateSourcesJarTask).from(generateRestClientTask.getDestinationDir());
_generateSourcesJarTask.dependsOn(generateRestClientTask);
_generateJavadocTask.source(generateRestClientTask.getDestinationDir());
_generateJavadocTask.setClasspath(_generateJavadocTask.getClasspath().plus(project.getConfigurations().getByName("restClientCompile")).plus(generateRestClientTask.getResolverPath()));
_generateJavadocTask.dependsOn(generateRestClientTask);
// make sure rest client source files have been generated before compiling them
JavaCompile compileGeneratedRestClientTask = (JavaCompile) project.getTasks().getByName(targetSourceSet.getCompileJavaTaskName());
compileGeneratedRestClientTask.dependsOn(generateRestClientTask);
compileGeneratedRestClientTask.getOptions().getCompilerArgs().add("-Xlint:-deprecation");
// create the rest model jar file
Task restModelJarTask = project.getTasks().create(sourceSet.getName() + "RestModelJar", Jar.class, task -> {
task.from(idlDir, copySpec -> {
copySpec.eachFile(fileCopyDetails -> project.getLogger().info("Add idl file: {}", fileCopyDetails));
copySpec.setIncludes(Collections.singletonList('*' + IDL_FILE_SUFFIX));
});
// FIXME change to #getArchiveAppendix().set(...); breaks backwards-compatibility before 5.1
task.setAppendix(getAppendix(sourceSet, "rest-model"));
task.setDescription("Generate rest model jar");
});
// create the rest client jar file
Task restClientJarTask = project.getTasks().create(sourceSet.getName() + "RestClientJar", Jar.class, task -> {
task.dependsOn(compileGeneratedRestClientTask);
task.from(idlDir, copySpec -> {
copySpec.eachFile(fileCopyDetails -> {
project.getLogger().info("Add interface file: {}", fileCopyDetails);
fileCopyDetails.setPath("idl" + File.separatorChar + fileCopyDetails.getPath());
});
copySpec.setIncludes(Collections.singletonList('*' + IDL_FILE_SUFFIX));
});
task.from(targetSourceSet.getOutput());
// FIXME change to #getArchiveAppendix().set(...); breaks backwards-compatibility before 5.1
task.setAppendix(getAppendix(sourceSet, "rest-client"));
task.setDescription("Generate rest client jar");
});
// add the rest model jar and the rest client jar to the list of project artifacts.
if (!isTestSourceSet(sourceSet)) {
project.getArtifacts().add("restModel", restModelJarTask);
project.getArtifacts().add("restClient", restClientJarTask);
} else {
project.getArtifacts().add("testRestModel", restModelJarTask);
project.getArtifacts().add("testRestClient", restClientJarTask);
}
}
Aggregations