Search in sources :

Example 16 with DslObject

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

the class BridgedCollections method registration.

public static <I, C extends NamedDomainObjectCollection<I>> ModelRegistrations.Builder registration(final ModelReference<C> containerReference, final Transformer<? extends C, ? super MutableModelNode> containerFactory, final Namer<? super I> namer, String descriptor, final Transformer<String, String> itemDescriptorGenerator) {
    final ModelPath containerPath = containerReference.getPath();
    final ModelType<C> containerType = containerReference.getType();
    assert containerPath != null : "container reference path cannot be null";
    return ModelRegistrations.of(containerPath).action(ModelActionRole.Create, new Action<MutableModelNode>() {

        public void execute(final MutableModelNode containerNode) {
            final C container = containerFactory.transform(containerNode);
            containerNode.setPrivateData(containerType, container);
            container.all(new Action<I>() {

                public void execute(final I item) {
                    final String name = namer.determineName(item);
                    // For now, ignore elements added after the container has been closed
                    if (!containerNode.isMutable()) {
                        LOGGER.debug("Ignoring element '{}' added to '{}' after it is closed.", containerPath, name);
                        return;
                    }
                    if (!containerNode.hasLink(name)) {
                        ModelRegistration itemRegistration = ModelRegistrations.unmanagedInstanceOf(ModelReference.of(containerPath.child(name), new DslObject(item).getDeclaredType()), new ExtractFromParentContainer<I, C>(name, containerType)).descriptor(new SimpleModelRuleDescriptor(new Factory<String>() {

                            @Override
                            public String create() {
                                return itemDescriptorGenerator.transform(name);
                            }
                        })).build();
                        containerNode.addLink(itemRegistration);
                    }
                }
            });
            container.whenObjectRemoved(new Action<I>() {

                public void execute(I item) {
                    String name = namer.determineName(item);
                    containerNode.removeLink(name);
                }
            });
        }
    }).descriptor(descriptor);
}
Also used : Action(org.gradle.api.Action) DslObject(org.gradle.api.internal.plugins.DslObject) Factory(org.gradle.internal.Factory) LoggerFactory(org.slf4j.LoggerFactory) SimpleModelRuleDescriptor(org.gradle.model.internal.core.rule.describe.SimpleModelRuleDescriptor)

Example 17 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 18 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 19 with DslObject

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

the class GroovyBasePlugin method addSourceSetExtension.

private void addSourceSetExtension(org.gradle.api.tasks.SourceSet sourceSet, DefaultGroovySourceSet groovySourceSet) {
    new DslObject(sourceSet).getConvention().getPlugins().put("groovy", groovySourceSet);
    sourceSet.getExtensions().add(GroovySourceDirectorySet.class, "groovy", groovySourceSet.getGroovy());
}
Also used : DslObject(org.gradle.api.internal.plugins.DslObject)

Example 20 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 = newInstance(TestDecoratedGroovyBean.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)

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