Search in sources :

Example 1 with GenerateDataTemplateTask

use of com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask in project rest.li by linkedin.

the class PegasusPlugin method configureDataTemplateGeneration.

@SuppressWarnings("deprecation")
protected GenerateDataTemplateTask configureDataTemplateGeneration(Project project, SourceSet sourceSet) {
    File dataSchemaDir = project.file(getDataSchemaPath(project, sourceSet));
    File generatedDataTemplateDir = project.file(getGeneratedDirPath(project, sourceSet, DATA_TEMPLATE_GEN_TYPE) + File.separatorChar + "java");
    File publishableSchemasBuildDir = project.file(project.getBuildDir().getAbsolutePath() + File.separatorChar + sourceSet.getName() + "Schemas");
    File publishableLegacySchemasBuildDir = project.file(project.getBuildDir().getAbsolutePath() + File.separatorChar + sourceSet.getName() + "LegacySchemas");
    File publishableExtensionSchemasBuildDir = project.file(project.getBuildDir().getAbsolutePath() + File.separatorChar + sourceSet.getName() + "ExtensionSchemas");
    // generate data template source files from data schema
    GenerateDataTemplateTask generateDataTemplatesTask = project.getTasks().create(sourceSet.getTaskName("generate", "dataTemplate"), GenerateDataTemplateTask.class, task -> {
        task.setInputDir(dataSchemaDir);
        task.setDestinationDir(generatedDataTemplateDir);
        task.setResolverPath(getDataModelConfig(project, sourceSet));
        task.setCodegenClasspath(project.getConfigurations().getByName(PEGASUS_PLUGIN_CONFIGURATION));
        if (isPropertyTrue(project, ENABLE_ARG_FILE)) {
            task.setEnableArgFile(true);
        }
        if (isPropertyTrue(project, CODE_GEN_PATH_CASE_SENSITIVE)) {
            task.setGenerateLowercasePath(false);
        }
        task.onlyIf(t -> {
            if (task.getInputDir().exists()) {
                @SuppressWarnings("unchecked") Map<String, PegasusOptions> pegasusOptions = (Map<String, PegasusOptions>) project.getExtensions().getExtraProperties().get("pegasus");
                return pegasusOptions.get(sourceSet.getName()).hasGenerationMode(PegasusOptions.GenerationMode.PEGASUS);
            }
            return false;
        });
        task.doFirst(new CacheableAction<>(t -> deleteGeneratedDir(project, sourceSet, DATA_TEMPLATE_GEN_TYPE)));
    });
    // TODO: Tighten the types so that _generateSourcesJarTask must be of type Jar.
    ((Jar) _generateSourcesJarTask).from(generateDataTemplatesTask.getDestinationDir());
    _generateSourcesJarTask.dependsOn(generateDataTemplatesTask);
    _generateJavadocTask.source(generateDataTemplatesTask.getDestinationDir());
    _generateJavadocTask.setClasspath(_generateJavadocTask.getClasspath().plus(project.getConfigurations().getByName("dataTemplateCompile")).plus(generateDataTemplatesTask.getResolverPath()));
    _generateJavadocTask.dependsOn(generateDataTemplatesTask);
    // Add extra dependencies for data model compilation
    project.getDependencies().add("dataTemplateCompile", "com.google.code.findbugs:jsr305:3.0.2");
    // create new source set for generated java source and class files
    String targetSourceSetName = getGeneratedSourceSetName(sourceSet, DATA_TEMPLATE_GEN_TYPE);
    SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
    SourceSet targetSourceSet = sourceSets.create(targetSourceSetName, ss -> {
        ss.java(sourceDirectorySet -> sourceDirectorySet.srcDir(generatedDataTemplateDir));
        ss.setCompileClasspath(getDataModelConfig(project, sourceSet).plus(project.getConfigurations().getByName("dataTemplateCompile")));
    });
    // idea plugin needs to know about new generated java source directory and its dependencies
    addGeneratedDir(project, targetSourceSet, Arrays.asList(getDataModelConfig(project, sourceSet), project.getConfigurations().getByName("dataTemplateCompile")));
    // Set source compatibility to 1.8 as the data-templates now generate code with Java 8 features.
    JavaCompile compileTask = project.getTasks().withType(JavaCompile.class).getByName(targetSourceSet.getCompileJavaTaskName());
    compileTask.doFirst(new CacheableAction<>(task -> {
        ((JavaCompile) task).setSourceCompatibility("1.8");
        ((JavaCompile) task).setTargetCompatibility("1.8");
    }));
    // make sure that java source files have been generated before compiling them
    compileTask.dependsOn(generateDataTemplatesTask);
    // Dummy task to maintain backward compatibility
    // TODO: Delete this task once use cases have had time to reference the new task
    Task destroyStaleFiles = project.getTasks().create(sourceSet.getName() + "DestroyStaleFiles", Delete.class);
    destroyStaleFiles.onlyIf(task -> {
        project.getLogger().lifecycle("{} task is a NO-OP task.", task.getPath());
        return false;
    });
    // Dummy task to maintain backward compatibility, as this task was replaced by CopySchemas
    // TODO: Delete this task once use cases have had time to reference the new task
    Task copyPdscSchemasTask = project.getTasks().create(sourceSet.getName() + "CopyPdscSchemas", Copy.class);
    copyPdscSchemasTask.dependsOn(destroyStaleFiles);
    copyPdscSchemasTask.onlyIf(task -> {
        project.getLogger().lifecycle("{} task is a NO-OP task.", task.getPath());
        return false;
    });
    // Prepare schema files for publication by syncing schema folders.
    Task prepareSchemasForPublishTask = project.getTasks().create(sourceSet.getName() + "CopySchemas", Sync.class, task -> {
        task.from(dataSchemaDir, syncSpec -> DATA_TEMPLATE_FILE_SUFFIXES.forEach(suffix -> syncSpec.include("**/*" + suffix)));
        task.into(publishableSchemasBuildDir);
    });
    prepareSchemasForPublishTask.dependsOn(copyPdscSchemasTask);
    Collection<Task> dataTemplateJarDepends = new ArrayList<>();
    dataTemplateJarDepends.add(compileTask);
    dataTemplateJarDepends.add(prepareSchemasForPublishTask);
    // Convert all PDL files back to PDSC for publication
    // TODO: Remove this conversion permanently once translated PDSCs are no longer needed.
    Task prepareLegacySchemasForPublishTask = project.getTasks().create(sourceSet.getName() + "TranslateSchemas", TranslateSchemasTask.class, task -> {
        task.setInputDir(dataSchemaDir);
        task.setDestinationDir(publishableLegacySchemasBuildDir);
        task.setResolverPath(getDataModelConfig(project, sourceSet));
        task.setCodegenClasspath(project.getConfigurations().getByName(PEGASUS_PLUGIN_CONFIGURATION));
        task.setSourceFormat(SchemaFileType.PDL);
        task.setDestinationFormat(SchemaFileType.PDSC);
        task.setKeepOriginal(true);
        task.setSkipVerification(true);
        if (isPropertyTrue(project, ENABLE_ARG_FILE)) {
            task.setEnableArgFile(true);
        }
    });
    prepareLegacySchemasForPublishTask.dependsOn(destroyStaleFiles);
    dataTemplateJarDepends.add(prepareLegacySchemasForPublishTask);
    // extension schema directory
    File extensionSchemaDir = project.file(getExtensionSchemaPath(project, sourceSet));
    if (!SharedFileUtils.getSuffixedFiles(project, extensionSchemaDir, PDL_FILE_SUFFIX).isEmpty()) {
        // Validate extension schemas if extension schemas are provided.
        ValidateExtensionSchemaTask validateExtensionSchemaTask = project.getTasks().create(sourceSet.getTaskName("validate", "ExtensionSchemas"), ValidateExtensionSchemaTask.class, task -> {
            task.setInputDir(extensionSchemaDir);
            task.setResolverPath(getDataModelConfig(project, sourceSet).plus(project.files(getDataSchemaPath(project, sourceSet))));
            task.setClassPath(project.getConfigurations().getByName(PEGASUS_PLUGIN_CONFIGURATION));
            if (isPropertyTrue(project, ENABLE_ARG_FILE)) {
                task.setEnableArgFile(true);
            }
        });
        Task prepareExtensionSchemasForPublishTask = project.getTasks().create(sourceSet.getName() + "CopyExtensionSchemas", Sync.class, task -> {
            task.from(extensionSchemaDir, syncSpec -> syncSpec.include("**/*" + PDL_FILE_SUFFIX));
            task.into(publishableExtensionSchemasBuildDir);
        });
        prepareExtensionSchemasForPublishTask.dependsOn(validateExtensionSchemaTask);
        prepareExtensionSchemasForPublishTask.dependsOn(copyPdscSchemasTask);
        dataTemplateJarDepends.add(prepareExtensionSchemasForPublishTask);
    }
    // include pegasus files in the output of this SourceSet
    project.getTasks().withType(ProcessResources.class).getByName(targetSourceSet.getProcessResourcesTaskName(), it -> {
        it.from(prepareSchemasForPublishTask, copy -> copy.into("pegasus"));
        // TODO: Remove this permanently once translated PDSCs are no longer needed.
        it.from(prepareLegacySchemasForPublishTask, copy -> copy.into(TRANSLATED_SCHEMAS_DIR));
        Sync copyExtensionSchemasTask = project.getTasks().withType(Sync.class).findByName(sourceSet.getName() + "CopyExtensionSchemas");
        if (copyExtensionSchemasTask != null) {
            it.from(copyExtensionSchemasTask, copy -> copy.into("extensions"));
        }
    });
    // create data template jar file
    Jar dataTemplateJarTask = project.getTasks().create(sourceSet.getName() + "DataTemplateJar", Jar.class, task -> {
        task.dependsOn(dataTemplateJarDepends);
        task.from(targetSourceSet.getOutput());
        // FIXME change to #getArchiveAppendix().set(...); breaks backwards-compatibility before 5.1
        task.setAppendix(getAppendix(sourceSet, "data-template"));
        task.setDescription("Generate a data template jar");
    });
    // add the data model and date template jars to the list of project artifacts.
    if (!isTestSourceSet(sourceSet)) {
        project.getArtifacts().add("dataTemplate", dataTemplateJarTask);
    } else {
        project.getArtifacts().add("testDataTemplate", dataTemplateJarTask);
    }
    // include additional dependencies into the appropriate configuration used to compile the input source set
    // must include the generated data template classes and their dependencies the configuration
    String compileConfigName = isTestSourceSet(sourceSet) ? "testCompile" : "compile";
    Configuration compileConfig = project.getConfigurations().maybeCreate(compileConfigName);
    compileConfig.extendsFrom(getDataModelConfig(project, sourceSet), project.getConfigurations().getByName("dataTemplateCompile"));
    // FIXME change to #getArchiveFile(); breaks backwards-compatibility before 5.1
    project.getDependencies().add(compileConfigName, project.files(dataTemplateJarTask.getArchivePath()));
    // The below Action is only applied when the 'ivy-publish' is applied by the consumer.
    // If the consumer does not use ivy-publish, this is a noop.
    // this Action prepares the project applying the pegasus plugin to publish artifacts using these steps:
    // 1. Registers "feature variants" for pegasus-specific artifacts;
    // see https://docs.gradle.org/6.1/userguide/feature_variants.html
    // 2. Wires legacy configurations like `dataTemplateCompile` to auto-generated feature variant *Api and
    // *Implementation configurations for backwards compatibility.
    // 3. Configures the Ivy Publication to include auto-generated feature variant *Api and *Implementation
    // configurations and their dependencies.
    project.getPlugins().withType(IvyPublishPlugin.class, ivyPublish -> {
        if (!isAtLeastGradle61()) {
            throw new GradleException("Using the ivy-publish plugin with the pegasus plugin requires Gradle 6.1 or higher " + "at build time.  Please upgrade.");
        }
        JavaPluginExtension java = project.getExtensions().getByType(JavaPluginExtension.class);
        // create new capabilities per source set; automatically creates api and implementation configurations
        String featureName = mapSourceSetToFeatureName(targetSourceSet);
        try {
            /*
         reflection is required to preserve compatibility with Gradle 5.2.1 and below
         TODO once Gradle 5.3+ is required, remove reflection and replace with:
           java.registerFeature(featureName, featureSpec -> {
             featureSpec.usingSourceSet(targetSourceSet);
            });
        */
            Method registerFeature = JavaPluginExtension.class.getDeclaredMethod("registerFeature", String.class, Action.class);
            Action<?> /*<org.gradle.api.plugins.FeatureSpec>*/
            featureSpecAction = createFeatureVariantFromSourceSet(targetSourceSet);
            registerFeature.invoke(java, featureName, featureSpecAction);
        } catch (ReflectiveOperationException e) {
            throw new GradleException("Unable to register new feature variant", e);
        }
        // expose transitive dependencies to consumers via variant configurations
        Configuration featureConfiguration = project.getConfigurations().getByName(featureName);
        Configuration mainGeneratedDataTemplateApi = project.getConfigurations().getByName(targetSourceSet.getApiConfigurationName());
        featureConfiguration.extendsFrom(mainGeneratedDataTemplateApi);
        mainGeneratedDataTemplateApi.extendsFrom(getDataModelConfig(project, targetSourceSet), project.getConfigurations().getByName("dataTemplateCompile"));
        // Configure the existing IvyPublication
        // For backwards-compatibility, make the legacy dataTemplate/testDataTemplate configurations extend
        // their replacements, auto-created when we registered the new feature variant
        project.afterEvaluate(p -> {
            PublishingExtension publishing = p.getExtensions().getByType(PublishingExtension.class);
            // When configuring a Gradle Publication, use this value to find the name of the publication to configure.  Defaults to "ivy".
            String publicationName = p.getExtensions().getExtraProperties().getProperties().getOrDefault("PegasusPublicationName", "ivy").toString();
            IvyPublication ivyPublication = publishing.getPublications().withType(IvyPublication.class).getByName(publicationName);
            ivyPublication.configurations(configurations -> configurations.create(featureName, legacyConfiguration -> {
                legacyConfiguration.extend(p.getConfigurations().getByName(targetSourceSet.getApiElementsConfigurationName()).getName());
                legacyConfiguration.extend(p.getConfigurations().getByName(targetSourceSet.getRuntimeElementsConfigurationName()).getName());
            }));
        });
    });
    if (debug) {
        System.out.println("configureDataTemplateGeneration sourceSet " + sourceSet.getName());
        System.out.println(compileConfigName + ".allDependencies : " + project.getConfigurations().getByName(compileConfigName).getAllDependencies());
        System.out.println(compileConfigName + ".extendsFrom: " + project.getConfigurations().getByName(compileConfigName).getExtendsFrom());
        System.out.println(compileConfigName + ".transitive: " + project.getConfigurations().getByName(compileConfigName).isTransitive());
    }
    project.getTasks().getByName(sourceSet.getCompileJavaTaskName()).dependsOn(dataTemplateJarTask);
    return generateDataTemplatesTask;
}
Also used : CompatibilityLogChecker(com.linkedin.pegasus.gradle.internal.CompatibilityLogChecker) Arrays(java.util.Arrays) PublishRestModelTask(com.linkedin.pegasus.gradle.tasks.PublishRestModelTask) ValidateExtensionSchemaTask(com.linkedin.pegasus.gradle.tasks.ValidateExtensionSchemaTask) JavaPlugin(org.gradle.api.plugins.JavaPlugin) IdeaPlugin(org.gradle.plugins.ide.idea.IdeaPlugin) CheckSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckSnapshotTask) EclipsePlugin(org.gradle.plugins.ide.eclipse.EclipsePlugin) Task(org.gradle.api.Task) PublishingExtension(org.gradle.api.publish.PublishingExtension) Javadoc(org.gradle.api.tasks.javadoc.Javadoc) JavaBasePlugin(org.gradle.api.plugins.JavaBasePlugin) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) TaskProvider(org.gradle.api.tasks.TaskProvider) Locale(java.util.Locale) Map(java.util.Map) TranslateSchemasTask(com.linkedin.pegasus.gradle.tasks.TranslateSchemasTask) CheckPegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckPegasusSnapshotTask) Method(java.lang.reflect.Method) Path(java.nio.file.Path) GradleVersion(org.gradle.util.GradleVersion) IdeaModule(org.gradle.plugins.ide.idea.model.IdeaModule) Delete(org.gradle.api.tasks.Delete) Project(org.gradle.api.Project) Collection(java.util.Collection) ChangedFileReportTask(com.linkedin.pegasus.gradle.tasks.ChangedFileReportTask) Set(java.util.Set) IvyPublishPlugin(org.gradle.api.publish.ivy.plugins.IvyPublishPlugin) GeneratePegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.GeneratePegasusSnapshotTask) Collectors(java.util.stream.Collectors) IvyPublication(org.gradle.api.publish.ivy.IvyPublication) List(java.util.List) IdlOptions(com.linkedin.pegasus.gradle.PegasusOptions.IdlOptions) JavaPluginExtension(org.gradle.api.plugins.JavaPluginExtension) Pattern(java.util.regex.Pattern) ProcessResources(org.gradle.language.jvm.tasks.ProcessResources) GenerateRestClientTask(com.linkedin.pegasus.gradle.tasks.GenerateRestClientTask) GenerateRestModelTask(com.linkedin.pegasus.gradle.tasks.GenerateRestModelTask) HashMap(java.util.HashMap) Function(java.util.function.Function) FileCollection(org.gradle.api.file.FileCollection) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) SourceSet(org.gradle.api.tasks.SourceSet) HashSet(java.util.HashSet) Configuration(org.gradle.api.artifacts.Configuration) LifecycleBasePlugin(org.gradle.language.base.plugins.LifecycleBasePlugin) Sync(org.gradle.api.tasks.Sync) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) CheckRestModelTask(com.linkedin.pegasus.gradle.tasks.CheckRestModelTask) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) Properties(java.util.Properties) Jar(org.gradle.api.tasks.bundling.Jar) Action(org.gradle.api.Action) Files(java.nio.file.Files) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) ValidateSchemaAnnotationTask(com.linkedin.pegasus.gradle.tasks.ValidateSchemaAnnotationTask) IOException(java.io.IOException) EclipseModel(org.gradle.plugins.ide.eclipse.model.EclipseModel) File(java.io.File) CheckIdlTask(com.linkedin.pegasus.gradle.tasks.CheckIdlTask) GenerateDataTemplateTask(com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask) GenerateAvroSchemaTask(com.linkedin.pegasus.gradle.tasks.GenerateAvroSchemaTask) GradleException(org.gradle.api.GradleException) Collections(java.util.Collections) Plugin(org.gradle.api.Plugin) Copy(org.gradle.api.tasks.Copy) InputStream(java.io.InputStream) PublishRestModelTask(com.linkedin.pegasus.gradle.tasks.PublishRestModelTask) ValidateExtensionSchemaTask(com.linkedin.pegasus.gradle.tasks.ValidateExtensionSchemaTask) CheckSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckSnapshotTask) Task(org.gradle.api.Task) TranslateSchemasTask(com.linkedin.pegasus.gradle.tasks.TranslateSchemasTask) CheckPegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckPegasusSnapshotTask) ChangedFileReportTask(com.linkedin.pegasus.gradle.tasks.ChangedFileReportTask) GeneratePegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.GeneratePegasusSnapshotTask) GenerateRestClientTask(com.linkedin.pegasus.gradle.tasks.GenerateRestClientTask) GenerateRestModelTask(com.linkedin.pegasus.gradle.tasks.GenerateRestModelTask) CheckRestModelTask(com.linkedin.pegasus.gradle.tasks.CheckRestModelTask) ValidateSchemaAnnotationTask(com.linkedin.pegasus.gradle.tasks.ValidateSchemaAnnotationTask) CheckIdlTask(com.linkedin.pegasus.gradle.tasks.CheckIdlTask) GenerateDataTemplateTask(com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask) GenerateAvroSchemaTask(com.linkedin.pegasus.gradle.tasks.GenerateAvroSchemaTask) Configuration(org.gradle.api.artifacts.Configuration) ArrayList(java.util.ArrayList) JavaPluginExtension(org.gradle.api.plugins.JavaPluginExtension) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) SourceSet(org.gradle.api.tasks.SourceSet) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) Sync(org.gradle.api.tasks.Sync) PublishingExtension(org.gradle.api.publish.PublishingExtension) ValidateExtensionSchemaTask(com.linkedin.pegasus.gradle.tasks.ValidateExtensionSchemaTask) GenerateDataTemplateTask(com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask) Method(java.lang.reflect.Method) GradleException(org.gradle.api.GradleException) ProcessResources(org.gradle.language.jvm.tasks.ProcessResources) Jar(org.gradle.api.tasks.bundling.Jar) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) IvyPublication(org.gradle.api.publish.ivy.IvyPublication)

Example 2 with GenerateDataTemplateTask

use of com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask in project rest.li by linkedin.

the class PegasusPlugin method apply.

@Override
public void apply(Project project) {
    checkGradleVersion(project);
    project.getPlugins().apply(JavaPlugin.class);
    // this HashMap will have a PegasusOptions per sourceSet
    project.getExtensions().getExtraProperties().set("pegasus", new HashMap<>());
    // this map will extract PegasusOptions.GenerationMode to project property
    project.getExtensions().getExtraProperties().set("PegasusGenerationMode", Arrays.stream(PegasusOptions.GenerationMode.values()).collect(Collectors.toMap(PegasusOptions.GenerationMode::name, Function.identity())));
    synchronized (STATIC_PROJECT_EVALUATED_LOCK) {
        // multiple sub-projects applied the plugin.
        if (!project.getRootProject().hasProperty(RUN_ONCE) || !Boolean.parseBoolean(String.valueOf(project.getRootProject().property(RUN_ONCE)))) {
            project.getGradle().projectsEvaluated(gradle -> gradle.getRootProject().subprojects(subproject -> UNUSED_CONFIGURATIONS.forEach(configurationName -> {
                Configuration conf = subproject.getConfigurations().findByName(configurationName);
                if (conf != null && !conf.getDependencies().isEmpty()) {
                    subproject.getLogger().warn("*** Project {} declares dependency to unused configuration \"{}\". " + "This configuration is deprecated and you can safely remove the dependency. ***", subproject.getPath(), configurationName);
                }
            })));
            // Re-initialize the static variables as they might have stale values from previous run. With Gradle 3.0 and
            // gradle daemon enabled, the plugin class might not be loaded for every run.
            DATA_TEMPLATE_FILE_SUFFIXES.clear();
            DATA_TEMPLATE_FILE_SUFFIXES.add(DATA_TEMPLATE_FILE_SUFFIX);
            DATA_TEMPLATE_FILE_SUFFIXES.add(PDL_FILE_SUFFIX);
            _restModelCompatMessage = new StringBuffer();
            _needCheckinFiles.clear();
            _needBuildFolders.clear();
            _possibleMissingFilesInEarlierCommit.clear();
            project.getGradle().buildFinished(result -> {
                StringBuilder endOfBuildMessage = new StringBuilder();
                if (_restModelCompatMessage.length() > 0) {
                    endOfBuildMessage.append(_restModelCompatMessage);
                }
                if (!_needCheckinFiles.isEmpty()) {
                    endOfBuildMessage.append(createModifiedFilesMessage(_needCheckinFiles, _needBuildFolders));
                }
                if (!_possibleMissingFilesInEarlierCommit.isEmpty()) {
                    endOfBuildMessage.append(createPossibleMissingFilesMessage(_possibleMissingFilesInEarlierCommit));
                }
                if (endOfBuildMessage.length() > 0) {
                    result.getGradle().getRootProject().getLogger().quiet(endOfBuildMessage.toString());
                }
            });
            // Set an extra property on the root project to indicate the initialization is complete for the current build.
            project.getRootProject().getExtensions().getExtraProperties().set(RUN_ONCE, true);
        }
    }
    ConfigurationContainer configurations = project.getConfigurations();
    // configuration for getting the required classes to make pegasus call main methods
    configurations.maybeCreate(PEGASUS_PLUGIN_CONFIGURATION);
    // configuration for compiling generated data templates
    Configuration dataTemplateCompile = configurations.maybeCreate("dataTemplateCompile");
    dataTemplateCompile.setVisible(false);
    // configuration for running rest client generator
    Configuration restClientCompile = configurations.maybeCreate("restClientCompile");
    restClientCompile.setVisible(false);
    // configuration for running data template generator
    // DEPRECATED! This configuration is no longer used. Please stop using it.
    Configuration dataTemplateGenerator = configurations.maybeCreate("dataTemplateGenerator");
    dataTemplateGenerator.setVisible(false);
    // configuration for running rest client generator
    // DEPRECATED! This configuration is no longer used. Please stop using it.
    Configuration restTools = configurations.maybeCreate("restTools");
    restTools.setVisible(false);
    // configuration for running Avro schema generator
    // DEPRECATED! To skip avro schema generation, use PegasusOptions.generationModes
    Configuration avroSchemaGenerator = configurations.maybeCreate("avroSchemaGenerator");
    avroSchemaGenerator.setVisible(false);
    // configuration for depending on data schemas and potentially generated data templates
    // and for publishing jars containing data schemas to the project artifacts for including in the ivy.xml
    Configuration dataModel = configurations.maybeCreate("dataModel");
    Configuration testDataModel = configurations.maybeCreate("testDataModel");
    testDataModel.extendsFrom(dataModel);
    // configuration for depending on data schemas and potentially generated data templates
    // and for publishing jars containing data schemas to the project artifacts for including in the ivy.xml
    Configuration avroSchema = configurations.maybeCreate("avroSchema");
    Configuration testAvroSchema = configurations.maybeCreate("testAvroSchema");
    testAvroSchema.extendsFrom(avroSchema);
    // configuration for depending on rest idl and potentially generated client builders
    // and for publishing jars containing rest idl to the project artifacts for including in the ivy.xml
    Configuration restModel = configurations.maybeCreate("restModel");
    Configuration testRestModel = configurations.maybeCreate("testRestModel");
    testRestModel.extendsFrom(restModel);
    // configuration for publishing jars containing data schemas and generated data templates
    // to the project artifacts for including in the ivy.xml
    // 
    // published data template jars depends on the configurations used to compile the classes
    // in the jar, this includes the data models/templates used by the data template generator
    // and the classes used to compile the generated classes.
    Configuration dataTemplate = configurations.maybeCreate("dataTemplate");
    dataTemplate.extendsFrom(dataTemplateCompile, dataModel);
    Configuration testDataTemplate = configurations.maybeCreate("testDataTemplate");
    testDataTemplate.extendsFrom(dataTemplate, testDataModel);
    // configuration for processing and validating schema annotation during build time.
    // 
    // The configuration contains dependencies to schema annotation handlers which would process schema annotations
    // and validate.
    Configuration schemaAnnotationHandler = configurations.maybeCreate(SCHEMA_ANNOTATION_HANDLER_CONFIGURATION);
    // configuration for publishing jars containing rest idl and generated client builders
    // to the project artifacts for including in the ivy.xml
    // 
    // published client builder jars depends on the configurations used to compile the classes
    // in the jar, this includes the data models/templates (potentially generated by this
    // project and) used by the data template generator and the classes used to compile
    // the generated classes.
    Configuration restClient = configurations.maybeCreate("restClient");
    restClient.extendsFrom(restClientCompile, dataTemplate);
    Configuration testRestClient = configurations.maybeCreate("testRestClient");
    testRestClient.extendsFrom(restClient, testDataTemplate);
    Properties properties = new Properties();
    InputStream inputStream = getClass().getResourceAsStream("/pegasus-version.properties");
    if (inputStream != null) {
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            throw new GradleException("Unable to read pegasus-version.properties file.", e);
        }
        String version = properties.getProperty("pegasus.version");
        project.getDependencies().add(PEGASUS_PLUGIN_CONFIGURATION, "com.linkedin.pegasus:data:" + version);
        project.getDependencies().add(PEGASUS_PLUGIN_CONFIGURATION, "com.linkedin.pegasus:data-avro-generator:" + version);
        project.getDependencies().add(PEGASUS_PLUGIN_CONFIGURATION, "com.linkedin.pegasus:generator:" + version);
        project.getDependencies().add(PEGASUS_PLUGIN_CONFIGURATION, "com.linkedin.pegasus:restli-tools:" + version);
    } else {
        project.getLogger().lifecycle("Unable to add pegasus dependencies to {}. Please be sure that " + "'com.linkedin.pegasus:data', 'com.linkedin.pegasus:data-avro-generator', 'com.linkedin.pegasus:generator', 'com.linkedin.pegasus:restli-tools'" + " are available on the configuration pegasusPlugin", project.getPath());
    }
    project.getDependencies().add(PEGASUS_PLUGIN_CONFIGURATION, "org.slf4j:slf4j-simple:1.7.2");
    project.getDependencies().add(PEGASUS_PLUGIN_CONFIGURATION, project.files(System.getProperty("java.home") + "/../lib/tools.jar"));
    // this call has to be here because:
    // 1) artifact cannot be published once projects has been evaluated, so we need to first
    // create the tasks and artifact handler, then progressively append sources
    // 2) in order to append sources progressively, the source and documentation tasks and artifacts must be
    // configured/created before configuring and creating the code generation tasks.
    configureGeneratedSourcesAndJavadoc(project);
    ChangedFileReportTask changedFileReportTask = project.getTasks().create("changedFilesReport", ChangedFileReportTask.class);
    project.getTasks().getByName("check").dependsOn(changedFileReportTask);
    SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
    sourceSets.all(sourceSet -> {
        if (sourceSet.getName().toLowerCase(Locale.US).contains("generated")) {
            return;
        }
        checkAvroSchemaExist(project, sourceSet);
        // the idl Generator input options will be inside the PegasusOptions class. Users of the
        // plugin can set the inputOptions in their build.gradle
        @SuppressWarnings("unchecked") Map<String, PegasusOptions> pegasusOptions = (Map<String, PegasusOptions>) project.getExtensions().getExtraProperties().get("pegasus");
        pegasusOptions.put(sourceSet.getName(), new PegasusOptions());
        // rest model generation could fail on incompatibility
        // if it can fail, fail it early
        configureRestModelGeneration(project, sourceSet);
        // Do compatibility check for schemas under "pegasus" directory if the configuration property is provided.
        if (isPropertyTrue(project, ENABLE_PEGASUS_SCHEMA_COMPATIBILITY_CHECK)) {
            configurePegasusSchemaSnapshotGeneration(project, sourceSet, false);
        }
        configurePegasusSchemaSnapshotGeneration(project, sourceSet, true);
        configureConversionUtilities(project, sourceSet);
        GenerateDataTemplateTask generateDataTemplateTask = configureDataTemplateGeneration(project, sourceSet);
        configureAvroSchemaGeneration(project, sourceSet);
        configureRestClientGeneration(project, sourceSet);
        if (!isPropertyTrue(project, DISABLE_SCHEMA_ANNOTATION_VALIDATION)) {
            configureSchemaAnnotationValidation(project, sourceSet, generateDataTemplateTask);
        }
        Task cleanGeneratedDirTask = project.task(sourceSet.getTaskName("clean", "GeneratedDir"));
        cleanGeneratedDirTask.doLast(new CacheableAction<>(task -> {
            deleteGeneratedDir(project, sourceSet, REST_GEN_TYPE);
            deleteGeneratedDir(project, sourceSet, AVRO_SCHEMA_GEN_TYPE);
            deleteGeneratedDir(project, sourceSet, DATA_TEMPLATE_GEN_TYPE);
        }));
        // make clean depends on deleting the generated directories
        project.getTasks().getByName("clean").dependsOn(cleanGeneratedDirTask);
        // Set data schema directories as resource roots
        configureDataSchemaResourcesRoot(project, sourceSet);
    });
    project.getExtensions().getExtraProperties().set(GENERATOR_CLASSLOADER_NAME, getClass().getClassLoader());
}
Also used : CompatibilityLogChecker(com.linkedin.pegasus.gradle.internal.CompatibilityLogChecker) Arrays(java.util.Arrays) PublishRestModelTask(com.linkedin.pegasus.gradle.tasks.PublishRestModelTask) ValidateExtensionSchemaTask(com.linkedin.pegasus.gradle.tasks.ValidateExtensionSchemaTask) JavaPlugin(org.gradle.api.plugins.JavaPlugin) IdeaPlugin(org.gradle.plugins.ide.idea.IdeaPlugin) CheckSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckSnapshotTask) EclipsePlugin(org.gradle.plugins.ide.eclipse.EclipsePlugin) Task(org.gradle.api.Task) PublishingExtension(org.gradle.api.publish.PublishingExtension) Javadoc(org.gradle.api.tasks.javadoc.Javadoc) JavaBasePlugin(org.gradle.api.plugins.JavaBasePlugin) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) TaskProvider(org.gradle.api.tasks.TaskProvider) Locale(java.util.Locale) Map(java.util.Map) TranslateSchemasTask(com.linkedin.pegasus.gradle.tasks.TranslateSchemasTask) CheckPegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckPegasusSnapshotTask) Method(java.lang.reflect.Method) Path(java.nio.file.Path) GradleVersion(org.gradle.util.GradleVersion) IdeaModule(org.gradle.plugins.ide.idea.model.IdeaModule) Delete(org.gradle.api.tasks.Delete) Project(org.gradle.api.Project) Collection(java.util.Collection) ChangedFileReportTask(com.linkedin.pegasus.gradle.tasks.ChangedFileReportTask) Set(java.util.Set) IvyPublishPlugin(org.gradle.api.publish.ivy.plugins.IvyPublishPlugin) GeneratePegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.GeneratePegasusSnapshotTask) Collectors(java.util.stream.Collectors) IvyPublication(org.gradle.api.publish.ivy.IvyPublication) List(java.util.List) IdlOptions(com.linkedin.pegasus.gradle.PegasusOptions.IdlOptions) JavaPluginExtension(org.gradle.api.plugins.JavaPluginExtension) Pattern(java.util.regex.Pattern) ProcessResources(org.gradle.language.jvm.tasks.ProcessResources) GenerateRestClientTask(com.linkedin.pegasus.gradle.tasks.GenerateRestClientTask) GenerateRestModelTask(com.linkedin.pegasus.gradle.tasks.GenerateRestModelTask) HashMap(java.util.HashMap) Function(java.util.function.Function) FileCollection(org.gradle.api.file.FileCollection) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) SourceSet(org.gradle.api.tasks.SourceSet) HashSet(java.util.HashSet) Configuration(org.gradle.api.artifacts.Configuration) LifecycleBasePlugin(org.gradle.language.base.plugins.LifecycleBasePlugin) Sync(org.gradle.api.tasks.Sync) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) CheckRestModelTask(com.linkedin.pegasus.gradle.tasks.CheckRestModelTask) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) Properties(java.util.Properties) Jar(org.gradle.api.tasks.bundling.Jar) Action(org.gradle.api.Action) Files(java.nio.file.Files) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) ValidateSchemaAnnotationTask(com.linkedin.pegasus.gradle.tasks.ValidateSchemaAnnotationTask) IOException(java.io.IOException) EclipseModel(org.gradle.plugins.ide.eclipse.model.EclipseModel) File(java.io.File) CheckIdlTask(com.linkedin.pegasus.gradle.tasks.CheckIdlTask) GenerateDataTemplateTask(com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask) GenerateAvroSchemaTask(com.linkedin.pegasus.gradle.tasks.GenerateAvroSchemaTask) GradleException(org.gradle.api.GradleException) Collections(java.util.Collections) Plugin(org.gradle.api.Plugin) Copy(org.gradle.api.tasks.Copy) InputStream(java.io.InputStream) PublishRestModelTask(com.linkedin.pegasus.gradle.tasks.PublishRestModelTask) ValidateExtensionSchemaTask(com.linkedin.pegasus.gradle.tasks.ValidateExtensionSchemaTask) CheckSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckSnapshotTask) Task(org.gradle.api.Task) TranslateSchemasTask(com.linkedin.pegasus.gradle.tasks.TranslateSchemasTask) CheckPegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckPegasusSnapshotTask) ChangedFileReportTask(com.linkedin.pegasus.gradle.tasks.ChangedFileReportTask) GeneratePegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.GeneratePegasusSnapshotTask) GenerateRestClientTask(com.linkedin.pegasus.gradle.tasks.GenerateRestClientTask) GenerateRestModelTask(com.linkedin.pegasus.gradle.tasks.GenerateRestModelTask) CheckRestModelTask(com.linkedin.pegasus.gradle.tasks.CheckRestModelTask) ValidateSchemaAnnotationTask(com.linkedin.pegasus.gradle.tasks.ValidateSchemaAnnotationTask) CheckIdlTask(com.linkedin.pegasus.gradle.tasks.CheckIdlTask) GenerateDataTemplateTask(com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask) GenerateAvroSchemaTask(com.linkedin.pegasus.gradle.tasks.GenerateAvroSchemaTask) Configuration(org.gradle.api.artifacts.Configuration) InputStream(java.io.InputStream) GenerateDataTemplateTask(com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask) IOException(java.io.IOException) Properties(java.util.Properties) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) ChangedFileReportTask(com.linkedin.pegasus.gradle.tasks.ChangedFileReportTask) GradleException(org.gradle.api.GradleException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

IdlOptions (com.linkedin.pegasus.gradle.PegasusOptions.IdlOptions)2 CompatibilityLogChecker (com.linkedin.pegasus.gradle.internal.CompatibilityLogChecker)2 ChangedFileReportTask (com.linkedin.pegasus.gradle.tasks.ChangedFileReportTask)2 CheckIdlTask (com.linkedin.pegasus.gradle.tasks.CheckIdlTask)2 CheckPegasusSnapshotTask (com.linkedin.pegasus.gradle.tasks.CheckPegasusSnapshotTask)2 CheckRestModelTask (com.linkedin.pegasus.gradle.tasks.CheckRestModelTask)2 CheckSnapshotTask (com.linkedin.pegasus.gradle.tasks.CheckSnapshotTask)2 GenerateAvroSchemaTask (com.linkedin.pegasus.gradle.tasks.GenerateAvroSchemaTask)2 GenerateDataTemplateTask (com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask)2 GeneratePegasusSnapshotTask (com.linkedin.pegasus.gradle.tasks.GeneratePegasusSnapshotTask)2 GenerateRestClientTask (com.linkedin.pegasus.gradle.tasks.GenerateRestClientTask)2 GenerateRestModelTask (com.linkedin.pegasus.gradle.tasks.GenerateRestModelTask)2 PublishRestModelTask (com.linkedin.pegasus.gradle.tasks.PublishRestModelTask)2 TranslateSchemasTask (com.linkedin.pegasus.gradle.tasks.TranslateSchemasTask)2 ValidateExtensionSchemaTask (com.linkedin.pegasus.gradle.tasks.ValidateExtensionSchemaTask)2 ValidateSchemaAnnotationTask (com.linkedin.pegasus.gradle.tasks.ValidateSchemaAnnotationTask)2 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Method (java.lang.reflect.Method)2