Search in sources :

Example 21 with DslObject

use of org.gradle.api.internal.plugins.DslObject in project gradle by gradle.

the class AsmBackedClassGeneratorTest method honorsPublicType.

@Test
public void honorsPublicType() throws Exception {
    DslObject dslObject = new DslObject(newInstance(BeanWithPublicType.class));
    assertEquals(typeOf(Bean.class), dslObject.getPublicType());
}
Also used : DslObject(org.gradle.api.internal.plugins.DslObject) Test(org.junit.Test)

Example 22 with DslObject

use of org.gradle.api.internal.plugins.DslObject in project gradle by gradle.

the class AsmBackedClassGeneratorTest method canAddDynamicPropertiesAndMethodsToJavaObject.

@Test
public void canAddDynamicPropertiesAndMethodsToJavaObject() throws Exception {
    Bean bean = newInstance(Bean.class);
    DynamicObjectAware dynamicObjectAware = (DynamicObjectAware) bean;
    ConventionObject conventionObject = new ConventionObject();
    new DslObject(dynamicObjectAware).getConvention().getPlugins().put("plugin", conventionObject);
    call("{ it.conventionProperty = 'value' }", bean);
    assertThat(conventionObject.getConventionProperty(), equalTo("value"));
    assertThat(call("{ it.hasProperty('conventionProperty') }", bean), notNullValue());
    assertThat(call("{ it.conventionProperty }", bean), equalTo((Object) "value"));
    assertThat(call("{ it.conventionMethod('value') }", bean), equalTo((Object) "[value]"));
    assertThat(call("{ it.invokeMethod('conventionMethod', 'value') }", bean), equalTo((Object) "[value]"));
}
Also used : DslObject(org.gradle.api.internal.plugins.DslObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) DslObject(org.gradle.api.internal.plugins.DslObject) ModelObject(org.gradle.internal.state.ModelObject) GroovyObject(groovy.lang.GroovyObject) DynamicObject(org.gradle.internal.metaobject.DynamicObject) ExtensibleDynamicObject(org.gradle.internal.extensibility.ExtensibleDynamicObject) DynamicObjectAware(org.gradle.api.internal.DynamicObjectAware) Test(org.junit.Test)

Example 23 with DslObject

use of org.gradle.api.internal.plugins.DslObject in project gradle by gradle.

the class AntlrPlugin method apply.

@SuppressWarnings("deprecation")
@Override
public void apply(final Project project) {
    project.getPluginManager().apply(JavaLibraryPlugin.class);
    // set up a configuration named 'antlr' for the user to specify the antlr libs to use in case
    // they want a specific version etc.
    final Configuration antlrConfiguration = project.getConfigurations().create(ANTLR_CONFIGURATION_NAME).setVisible(false).setDescription("The Antlr libraries to be used for this project.");
    ((DeprecatableConfiguration) antlrConfiguration).deprecateForConsumption(deprecation -> deprecation.willBecomeAnErrorInGradle8().withUpgradeGuideSection(7, "plugin_configuration_consumption"));
    antlrConfiguration.defaultDependencies(dependencies -> dependencies.add(project.getDependencies().create("antlr:antlr:2.7.7@jar")));
    Configuration apiConfiguration = project.getConfigurations().getByName(JavaPlugin.API_CONFIGURATION_NAME);
    apiConfiguration.extendsFrom(antlrConfiguration);
    // Wire the antlr configuration into all antlr tasks
    project.getTasks().withType(AntlrTask.class).configureEach(antlrTask -> antlrTask.getConventionMapping().map("antlrClasspath", () -> project.getConfigurations().getByName(ANTLR_CONFIGURATION_NAME)));
    project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets().all(new Action<SourceSet>() {

        @Override
        public void execute(final SourceSet sourceSet) {
            // for each source set we will:
            // 1) Add a new 'antlr' virtual directory mapping
            org.gradle.api.plugins.antlr.internal.AntlrSourceVirtualDirectoryImpl antlrDirectoryDelegate = new org.gradle.api.plugins.antlr.internal.AntlrSourceVirtualDirectoryImpl(((DefaultSourceSet) sourceSet).getDisplayName(), objectFactory);
            new DslObject(sourceSet).getConvention().getPlugins().put(AntlrSourceVirtualDirectory.NAME, antlrDirectoryDelegate);
            sourceSet.getExtensions().add(AntlrSourceDirectorySet.class, AntlrSourceVirtualDirectory.NAME, antlrDirectoryDelegate.getAntlr());
            final String srcDir = "src/" + sourceSet.getName() + "/antlr";
            antlrDirectoryDelegate.getAntlr().srcDir(srcDir);
            sourceSet.getAllSource().source(antlrDirectoryDelegate.getAntlr());
            // 2) create an AntlrTask for this sourceSet following the gradle
            // naming conventions via call to sourceSet.getTaskName()
            final String taskName = sourceSet.getTaskName("generate", "GrammarSource");
            // 3) Set up the Antlr output directory (adding to javac inputs!)
            final String outputDirectoryName = project.getBuildDir() + "/generated-src/antlr/" + sourceSet.getName();
            final File outputDirectory = new File(outputDirectoryName);
            sourceSet.getJava().srcDir(outputDirectory);
            project.getTasks().register(taskName, AntlrTask.class, new Action<AntlrTask>() {

                @Override
                public void execute(AntlrTask antlrTask) {
                    antlrTask.setDescription("Processes the " + sourceSet.getName() + " Antlr grammars.");
                    // 4) set up convention mapping for default sources (allows user to not have to specify)
                    antlrTask.setSource(antlrDirectoryDelegate.getAntlr());
                    antlrTask.setOutputDirectory(outputDirectory);
                }
            });
            // 5) register fact that antlr should be run before compiling
            project.getTasks().named(sourceSet.getCompileJavaTaskName(), new Action<Task>() {

                @Override
                public void execute(Task task) {
                    task.dependsOn(taskName);
                }
            });
        }
    });
}
Also used : Action(org.gradle.api.Action) Task(org.gradle.api.Task) Configuration(org.gradle.api.artifacts.Configuration) DeprecatableConfiguration(org.gradle.internal.deprecation.DeprecatableConfiguration) DslObject(org.gradle.api.internal.plugins.DslObject) DeprecatableConfiguration(org.gradle.internal.deprecation.DeprecatableConfiguration) DefaultSourceSet(org.gradle.api.internal.tasks.DefaultSourceSet) SourceSet(org.gradle.api.tasks.SourceSet) DefaultSourceSet(org.gradle.api.internal.tasks.DefaultSourceSet) File(java.io.File)

Example 24 with DslObject

use of org.gradle.api.internal.plugins.DslObject in project webpieces by deanhiller.

the class TemplateCompilerPlugin method configureSourceSetDefaults.

private void configureSourceSetDefaults(Project project) {
    log.debug("setup configure source set defaults");
    JavaPluginExtension javaPluginExtension = javaPluginExtension();
    // SourceSetContainer sourceSets = javaPluginExtension.getSourceSets();
    // SourceSet main = sourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME);
    // SourceSet test = sourceSets.findByName(SourceSet.TEST_SOURCE_SET_NAME);
    // 
    // test.setRuntimeClasspath(project.getObjects().fileCollection().from(test.getOutput(), main.getOutput(), project.getConfigurations().getByName(JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME)));
    // // Register the project's source set output directories
    // sourceSets.all(sourceSet ->
    // buildOutputCleanupRegistry.registerOutputs(sourceSet.getOutput())
    // );
    javaPluginExtension.getSourceSets().all(sourceSet -> {
        String name = "templates";
        final DefaultTemplateSourceSet groovySourceSet = new DefaultTemplateSourceSet(name, ((DefaultSourceSet) sourceSet).getDisplayName(), objectFactory);
        (new DslObject(sourceSet)).getConvention().getPlugins().put("templates", groovySourceSet);
        // copied from JavaBasePlugin
        processCopyTemplateResources(project, sourceSet);
        // sourceSet.getExtensions().add(GroovySourceDirectorySet.class, "groovy", groovySourceSet.getGroovy()); //We do not need this one
        SourceDirectorySet groovySource = groovySourceSet.getTemplateDirSet();
        groovySource.srcDir("src/" + sourceSet.getName() + "/java");
        sourceSet.getResources().getFilter().exclude(SerializableLambdas.spec((element) -> {
            return groovySource.contains(element.getFile());
        }));
        sourceSet.getAllJava().source(groovySource);
        sourceSet.getAllSource().source(groovySource);
        // copy over but comment as I think no longer needed anymore
        // NEEDED?: configureOutputDirectoryForSourceSet(sourceSet, groovySourceSet.getTemplateDirSet(), project);
        final TaskProvider<TemplateCompile> compileTask = project.getTasks().register(sourceSet.getCompileTaskName("templates"), TemplateCompile.class, compile -> {
            JvmPluginsHelper.configureForSourceSet(sourceSet, groovySource, compile, compile.getOptions(), project);
            // copy over but comment as I think no longer needed anymore
            // NEEDED?: compile.dependsOn(sourceSet.getCompileJavaTaskName());
            compile.setDescription("Compiles the " + sourceSet.getName() + " Webpieces Templates.");
            compile.setSource(groovySource);
        // copy over but comment as I think no longer needed anymore
        // NEEDED?: compile.setDestinationDir(new File(project.getBuildDir(), "classes/" + groovySourceSet.getTemplateDirSet().getName() + "/" + sourceSet.getName()));
        // we do not support different versions of java from gradle right now...
        // compile.getJavaLauncher().convention(getToolchainTool(project, JavaToolchainService::launcherFor));
        });
        JvmPluginsHelper.configureOutputDirectoryForSourceSet(sourceSet, groovySource, this.project, compileTask, compileTask.map(TemplateCompile::getOptions));
        this.jvmPluginServices.useDefaultTargetPlatformInference(this.project.getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName()), compileTask);
        this.jvmPluginServices.useDefaultTargetPlatformInference(this.project.getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName()), compileTask);
        this.project.getTasks().named(sourceSet.getClassesTaskName(), (task) -> {
            task.dependsOn(new Object[] { compileTask });
        });
    // Ties resources to be done first or something but we don't need this
    // this.project.getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName()).attributes((attrs) -> {
    // attrs.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, (LibraryElements)this.project.getObjects().named(LibraryElements.class, "classes+resources"));
    // });
    });
// this.project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(new ConfigureAction(this.project, objectFactory));
}
Also used : ProcessResources(org.gradle.language.jvm.tasks.ProcessResources) DefaultSourceSet(org.gradle.api.internal.tasks.DefaultSourceSet) DefaultSourceSetOutput(org.gradle.api.internal.tasks.DefaultSourceSetOutput) Closure(groovy.lang.Closure) JavaToolchainService(org.gradle.jvm.toolchain.JavaToolchainService) SerializableLambdas(org.gradle.api.internal.lambdas.SerializableLambdas) FileCollection(org.gradle.api.file.FileCollection) Logger(org.gradle.api.logging.Logger) LibraryElements(org.gradle.api.attributes.LibraryElements) Inject(javax.inject.Inject) DslObject(org.gradle.api.internal.plugins.DslObject) Configuration(org.gradle.api.artifacts.Configuration) Task(org.gradle.api.Task) Provider(org.gradle.api.provider.Provider) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) FileTreeElement(org.gradle.api.file.FileTreeElement) ConfigurationInternal(org.gradle.api.internal.artifacts.configurations.ConfigurationInternal) ModuleRegistry(org.gradle.api.internal.classpath.ModuleRegistry) FileCopyDetails(org.gradle.api.file.FileCopyDetails) LogLevel(org.gradle.api.logging.LogLevel) Action(org.gradle.api.Action) org.gradle.api.plugins(org.gradle.api.plugins) Project(org.gradle.api.Project) SourceDirectorySet(org.gradle.api.file.SourceDirectorySet) JvmPluginsHelper(org.gradle.api.plugins.internal.JvmPluginsHelper) Cast(org.gradle.internal.Cast) IConventionAware(org.gradle.api.internal.IConventionAware) File(java.io.File) ConventionMapping(org.gradle.api.internal.ConventionMapping) JvmEcosystemUtilities(org.gradle.api.plugins.jvm.internal.JvmEcosystemUtilities) org.gradle.api.tasks(org.gradle.api.tasks) ObjectFactory(org.gradle.api.model.ObjectFactory) Spec(org.gradle.api.specs.Spec) Logging(org.gradle.api.logging.Logging) GroovyCompile(org.gradle.api.tasks.compile.GroovyCompile) Plugin(org.gradle.api.Plugin) JvmPluginServices(org.gradle.api.plugins.jvm.internal.JvmPluginServices) DeprecatableConfiguration(org.gradle.internal.deprecation.DeprecatableConfiguration) DslObject(org.gradle.api.internal.plugins.DslObject) SourceDirectorySet(org.gradle.api.file.SourceDirectorySet)

Example 25 with DslObject

use of org.gradle.api.internal.plugins.DslObject in project gradle-apt-plugin by tbroyer.

the class AptPlugin34to42 method configureCompileTaskForSourceSet.

@Override
protected void configureCompileTaskForSourceSet(Project project, final SourceSet sourceSet, AbstractCompile task, CompileOptions compileOptions) {
    if (!(task instanceof JavaCompile)) {
        prevImpl.configureCompileTaskForSourceSet(project, sourceSet, task, compileOptions);
        return;
    }
    compileOptions.setAnnotationProcessorPath(project.files(new Callable<FileCollection>() {

        @Override
        public FileCollection call() {
            return new DslObject(sourceSet).getConvention().getPlugin(AptPlugin.AptSourceSetConvention.class).getAnnotationProcessorPath();
        }
    }));
    task.getConvention().getPlugin(AptPlugin.AptConvention.class).setGeneratedSourcesDestinationDir(new Callable<File>() {

        @Override
        public File call() {
            return new DslObject(sourceSet.getOutput()).getConvention().getPlugin(AptPlugin.AptSourceSetOutputConvention.class).getGeneratedSourcesDir();
        }
    });
}
Also used : DslObject(org.gradle.api.internal.plugins.DslObject) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) File(java.io.File) Callable(java.util.concurrent.Callable)

Aggregations

DslObject (org.gradle.api.internal.plugins.DslObject)26 File (java.io.File)10 Test (org.junit.Test)8 Action (org.gradle.api.Action)6 Configuration (org.gradle.api.artifacts.Configuration)5 ConventionMapping (org.gradle.api.internal.ConventionMapping)5 Project (org.gradle.api.Project)4 GroovyObject (groovy.lang.GroovyObject)3 Task (org.gradle.api.Task)3 SourceSet (org.gradle.api.tasks.SourceSet)3 BeanDynamicObject (org.gradle.internal.metaobject.BeanDynamicObject)3 DynamicObject (org.gradle.internal.metaobject.DynamicObject)3 Node (groovy.util.Node)2 NodeList (groovy.util.NodeList)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Callable (java.util.concurrent.Callable)2 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)2 XmlProvider (org.gradle.api.XmlProvider)2 DynamicObjectAware (org.gradle.api.internal.DynamicObjectAware)2