Search in sources :

Example 11 with DslObject

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

the class AsmBackedClassGeneratorTest method generatesDslObjectCompatibleObject.

@Test
public void generatesDslObjectCompatibleObject() throws Exception {
    DslObject dslObject = new DslObject(generator.generate(Bean.class).newInstance());
    assertEquals(Bean.class, dslObject.getDeclaredType());
    assertNotNull(dslObject.getConventionMapping());
    assertNotNull(dslObject.getConvention());
    assertNotNull(dslObject.getExtensions());
    assertNotNull(dslObject.getAsDynamicObject());
}
Also used : DslObject(org.gradle.api.internal.plugins.DslObject) Test(org.junit.Test)

Example 12 with DslObject

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

the class AsmBackedClassGeneratorTest method canAddDynamicPropertiesAndMethodsToGroovyObject.

@Test
public void canAddDynamicPropertiesAndMethodsToGroovyObject() throws Exception {
    TestDecoratedGroovyBean bean = generator.generate(TestDecoratedGroovyBean.class).newInstance();
    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) GroovyObject(groovy.lang.GroovyObject) DslObject(org.gradle.api.internal.plugins.DslObject) DynamicObject(org.gradle.internal.metaobject.DynamicObject) Test(org.junit.Test)

Example 13 with DslObject

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

the class AntlrPlugin method apply.

public void apply(final Project project) {
    project.getPluginManager().apply(JavaPlugin.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.");
    antlrConfiguration.defaultDependencies(new Action<DependencySet>() {

        @Override
        public void execute(DependencySet dependencies) {
            dependencies.add(project.getDependencies().create("antlr:antlr:2.7.7@jar"));
        }
    });
    project.getConfigurations().getByName(COMPILE_CONFIGURATION_NAME).extendsFrom(antlrConfiguration);
    // Wire the antlr configuration into all antlr tasks
    project.getTasks().withType(AntlrTask.class, new Action<AntlrTask>() {

        public void execute(AntlrTask antlrTask) {
            antlrTask.getConventionMapping().map("antlrClasspath", new Callable<Object>() {

                public Object call() throws Exception {
                    return project.getConfigurations().getByName(ANTLR_CONFIGURATION_NAME);
                }
            });
        }
    });
    project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(new Action<SourceSet>() {

        public void execute(SourceSet sourceSet) {
            // for each source set we will:
            // 1) Add a new 'antlr' virtual directory mapping
            final AntlrSourceVirtualDirectoryImpl antlrDirectoryDelegate = new AntlrSourceVirtualDirectoryImpl(((DefaultSourceSet) sourceSet).getDisplayName(), sourceDirectorySetFactory);
            new DslObject(sourceSet).getConvention().getPlugins().put(AntlrSourceVirtualDirectory.NAME, antlrDirectoryDelegate);
            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");
            AntlrTask antlrTask = project.getTasks().create(taskName, AntlrTask.class);
            antlrTask.setDescription("Processes the " + sourceSet.getName() + " Antlr grammars.");
            // 3) set up convention mapping for default sources (allows user to not have to specify)
            antlrTask.setSource(antlrDirectoryDelegate.getAntlr());
            // 4) 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);
            antlrTask.setOutputDirectory(outputDirectory);
            sourceSet.getJava().srcDir(outputDirectory);
            // 6) register fact that antlr should be run before compiling
            project.getTasks().getByName(sourceSet.getCompileJavaTaskName()).dependsOn(taskName);
        }
    });
}
Also used : Configuration(org.gradle.api.artifacts.Configuration) DslObject(org.gradle.api.internal.plugins.DslObject) DependencySet(org.gradle.api.artifacts.DependencySet) Callable(java.util.concurrent.Callable) DefaultSourceSet(org.gradle.api.internal.tasks.DefaultSourceSet) SourceSet(org.gradle.api.tasks.SourceSet) AntlrSourceVirtualDirectoryImpl(org.gradle.api.plugins.antlr.internal.AntlrSourceVirtualDirectoryImpl) DefaultSourceSet(org.gradle.api.internal.tasks.DefaultSourceSet) File(java.io.File)

Example 14 with DslObject

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

the class BuildDashboardPlugin method apply.

public void apply(final Project project) {
    project.getPluginManager().apply(ReportingBasePlugin.class);
    final GenerateBuildDashboard buildDashboardTask = project.getTasks().create(BUILD_DASHBOARD_TASK_NAME, GenerateBuildDashboard.class);
    buildDashboardTask.setDescription("Generates a dashboard of all the reports produced by this build.");
    buildDashboardTask.setGroup("reporting");
    DirectoryReport htmlReport = buildDashboardTask.getReports().getHtml();
    ConventionMapping htmlReportConventionMapping = new DslObject(htmlReport).getConventionMapping();
    htmlReportConventionMapping.map("destination", new Callable<Object>() {

        public Object call() throws Exception {
            return project.getExtensions().getByType(ReportingExtension.class).file("buildDashboard");
        }
    });
    Action<Task> captureReportingTasks = new Action<Task>() {

        public void execute(Task task) {
            if (!(task instanceof Reporting)) {
                return;
            }
            Reporting reporting = (Reporting) task;
            buildDashboardTask.aggregate(reporting);
            if (!task.equals(buildDashboardTask)) {
                task.finalizedBy(buildDashboardTask);
            }
        }
    };
    for (Project aProject : project.getAllprojects()) {
        aProject.getTasks().all(captureReportingTasks);
    }
}
Also used : DslObject(org.gradle.api.internal.plugins.DslObject) DslObject(org.gradle.api.internal.plugins.DslObject) Reporting(org.gradle.api.reporting.Reporting) DirectoryReport(org.gradle.api.reporting.DirectoryReport) ConventionMapping(org.gradle.api.internal.ConventionMapping) GenerateBuildDashboard(org.gradle.api.reporting.GenerateBuildDashboard)

Aggregations

DslObject (org.gradle.api.internal.plugins.DslObject)14 File (java.io.File)5 Configuration (org.gradle.api.artifacts.Configuration)3 DynamicObject (org.gradle.internal.metaobject.DynamicObject)3 Test (org.junit.Test)3 GroovyObject (groovy.lang.GroovyObject)2 ConventionMapping (org.gradle.api.internal.ConventionMapping)2 SourceSet (org.gradle.api.tasks.SourceSet)2 BeanDynamicObject (org.gradle.internal.metaobject.BeanDynamicObject)2 Field (java.lang.reflect.Field)1 Callable (java.util.concurrent.Callable)1 Action (org.gradle.api.Action)1 Task (org.gradle.api.Task)1 DependencySet (org.gradle.api.artifacts.DependencySet)1 FileCollection (org.gradle.api.file.FileCollection)1 DefaultSourceSet (org.gradle.api.internal.tasks.DefaultSourceSet)1 AntlrSourceVirtualDirectoryImpl (org.gradle.api.plugins.antlr.internal.AntlrSourceVirtualDirectoryImpl)1 DefaultMavenRepositoryHandlerConvention (org.gradle.api.publication.maven.internal.DefaultMavenRepositoryHandlerConvention)1 DirectoryReport (org.gradle.api.reporting.DirectoryReport)1 GenerateBuildDashboard (org.gradle.api.reporting.GenerateBuildDashboard)1