Search in sources :

Example 11 with DynamicObject

use of org.gradle.internal.metaobject.DynamicObject in project gradle by gradle.

the class AntFileCollectionMatchingTaskBuilder method addToAntBuilder.

public Object addToAntBuilder(final Object node, final String childNodeName) {
    final DynamicObject dynamicObject = new BeanDynamicObject(node);
    final Iterable<DirectoryFileTree> existing = Lists.newLinkedList(FluentIterable.from(fileTrees).filter(new Predicate<DirectoryFileTree>() {

        @Override
        public boolean apply(DirectoryFileTree input) {
            return input.getDir().exists();
        }
    }));
    for (DirectoryFileTree fileTree : existing) {
        dynamicObject.invokeMethod(childNodeName, Collections.singletonMap("location", fileTree.getDir()));
    }
    dynamicObject.invokeMethod("or", new Closure<Void>(this) {

        public Object doCall(Object ignore) {
            for (final DirectoryFileTree fileTree : existing) {
                dynamicObject.invokeMethod("and", new Closure<Void>(this) {

                    public Object doCall(Object ignore) {
                        dynamicObject.invokeMethod("gradleBaseDirSelector", Collections.singletonMap("baseDir", fileTree.getDir()));
                        fileTree.getPatterns().addToAntBuilder(node, null);
                        return null;
                    }
                });
            }
            return null;
        }
    });
    return node;
}
Also used : DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Closure(groovy.lang.Closure) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) DirectoryFileTree(org.gradle.api.internal.file.collections.DirectoryFileTree) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Predicate(com.google.common.base.Predicate)

Example 12 with DynamicObject

use of org.gradle.internal.metaobject.DynamicObject in project gradle by gradle.

the class AntFileTreeBuilder method addToAntBuilder.

@Override
public Object addToAntBuilder(Object node, String childNodeName) {
    final DynamicObject dynamicObject = new BeanDynamicObject(node);
    dynamicObject.invokeMethod(childNodeName == null ? "resources" : childNodeName, new Closure(this) {

        public Object doCall(Object ignore) {
            for (Map.Entry<String, File> entry : files.entrySet()) {
                String name = entry.getKey();
                File file = entry.getValue();
                // gradleFileResource type is mapped to AntFileResource
                dynamicObject.invokeMethod("gradleFileResource", ImmutableMap.of("file", file.getAbsolutePath(), "name", name));
            }
            return null;
        }
    });
    return node;
}
Also used : DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Closure(groovy.lang.Closure) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) File(java.io.File)

Example 13 with DynamicObject

use of org.gradle.internal.metaobject.DynamicObject in project gradle by gradle.

the class SettingsFactory method createSettings.

public SettingsInternal createSettings(GradleInternal gradle, File settingsDir, ScriptSource settingsScript, Map<String, String> gradleProperties, StartParameter startParameter, ClassLoaderScope buildRootClassLoaderScope) {
    ClassLoaderScope settingsClassLoaderScope = buildRootClassLoaderScope.createChild("settings");
    ScriptHandlerInternal settingsScriptHandler = scriptHandlerFactory.create(settingsScript, settingsClassLoaderScope);
    DefaultSettings settings = instantiator.newInstance(DefaultSettings.class, serviceRegistryFactory, gradle, settingsClassLoaderScope, buildRootClassLoaderScope, settingsScriptHandler, settingsDir, settingsScript, startParameter);
    DynamicObject dynamicObject = ((DynamicObjectAware) settings).getAsDynamicObject();
    ((ExtensibleDynamicObject) dynamicObject).addProperties(gradleProperties);
    return settings;
}
Also used : ExtensibleDynamicObject(org.gradle.api.internal.ExtensibleDynamicObject) DynamicObject(org.gradle.internal.metaobject.DynamicObject) ClassLoaderScope(org.gradle.api.internal.initialization.ClassLoaderScope) ExtensibleDynamicObject(org.gradle.api.internal.ExtensibleDynamicObject) ScriptHandlerInternal(org.gradle.api.internal.initialization.ScriptHandlerInternal) DynamicObjectAware(org.gradle.api.internal.DynamicObjectAware)

Example 14 with DynamicObject

use of org.gradle.internal.metaobject.DynamicObject in project gradle by gradle.

the class ConfigureUtil method configureByMap.

public static <T> T configureByMap(Map<?, ?> properties, T delegate) {
    if (properties.isEmpty()) {
        return delegate;
    }
    DynamicObject dynamicObject = DynamicObjectUtil.asDynamicObject(delegate);
    for (Map.Entry<?, ?> entry : properties.entrySet()) {
        String name = entry.getKey().toString();
        Object value = entry.getValue();
        DynamicInvokeResult result = dynamicObject.trySetProperty(name, value);
        if (result.isFound()) {
            continue;
        }
        result = dynamicObject.tryInvokeMethod(name, value);
        if (!result.isFound()) {
            throw dynamicObject.setMissingProperty(name);
        }
    }
    return delegate;
}
Also used : DynamicObject(org.gradle.internal.metaobject.DynamicObject) DynamicInvokeResult(org.gradle.internal.metaobject.DynamicInvokeResult) DynamicObject(org.gradle.internal.metaobject.DynamicObject) Map(java.util.Map)

Example 15 with DynamicObject

use of org.gradle.internal.metaobject.DynamicObject in project gradle by gradle.

the class ExtensibleDynamicObjectTest method parentPropertiesAreInherited.

@Test
public void parentPropertiesAreInherited() {
    Bean parent = new Bean();
    parent.defineProperty("parentProperty", "value");
    Bean bean = new Bean();
    bean.setParent(parent.getAsDynamicObject());
    DynamicObject inherited = bean.getInheritable();
    assertTrue(inherited.hasProperty("parentProperty"));
    assertThat(inherited.getProperty("parentProperty"), equalTo((Object) "value"));
    assertThat(inherited.getProperties().get("parentProperty"), equalTo((Object) "value"));
}
Also used : DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Test(org.junit.Test)

Aggregations

DynamicObject (org.gradle.internal.metaobject.DynamicObject)25 BeanDynamicObject (org.gradle.internal.metaobject.BeanDynamicObject)21 Test (org.junit.Test)18 Convention (org.gradle.api.plugins.Convention)5 Closure (groovy.lang.Closure)3 File (java.io.File)2 Callable (java.util.concurrent.Callable)2 DefaultProviderFactory (org.gradle.api.internal.provider.DefaultProviderFactory)2 Predicate (com.google.common.base.Predicate)1 MissingMethodException (groovy.lang.MissingMethodException)1 Map (java.util.Map)1 DynamicObjectAware (org.gradle.api.internal.DynamicObjectAware)1 ExtensibleDynamicObject (org.gradle.api.internal.ExtensibleDynamicObject)1 DirectoryFileTree (org.gradle.api.internal.file.collections.DirectoryFileTree)1 ClassLoaderScope (org.gradle.api.internal.initialization.ClassLoaderScope)1 ScriptHandlerInternal (org.gradle.api.internal.initialization.ScriptHandlerInternal)1 DslObject (org.gradle.api.internal.plugins.DslObject)1 DynamicInvokeResult (org.gradle.internal.metaobject.DynamicInvokeResult)1