Search in sources :

Example 1 with BiAction

use of org.gradle.internal.BiAction in project gradle by gradle.

the class ClosureBackedRuleFactory method toAction.

public <T> DeferredModelAction toAction(final Class<T> subjectType, final Closure<?> closure) {
    final TransformedClosure transformedClosure = (TransformedClosure) closure;
    SourceLocation sourceLocation = ruleLocationExtractor.transform(transformedClosure);
    final ModelRuleDescriptor descriptor = sourceLocation.asDescriptor();
    return new DeferredModelAction() {

        @Override
        public ModelRuleDescriptor getDescriptor() {
            return descriptor;
        }

        @Override
        public void execute(MutableModelNode node, ModelActionRole role) {
            final boolean supportsNestedRules = node.canBeViewedAs(MANAGED_INSTANCE_TYPE);
            InputReferences inputs = transformedClosure.inputReferences();
            List<InputReference> inputReferences = supportsNestedRules ? inputs.getOwnReferences() : inputs.getAllReferences();
            final Map<String, PotentialInput> inputValues = Maps.newLinkedHashMap();
            List<ModelReference<?>> inputModelReferences = Lists.newArrayList();
            for (InputReference inputReference : inputReferences) {
                String description = "@ line " + inputReference.getLineNumber();
                String path = inputReference.getPath();
                if (!inputValues.containsKey(path)) {
                    inputValues.put(path, new PotentialInput(inputModelReferences.size()));
                    inputModelReferences.add(ModelReference.untyped(ModelPath.path(path), description));
                }
            }
            node.applyToSelf(role, InputUsingModelAction.of(ModelReference.of(node.getPath(), subjectType), descriptor, inputModelReferences, new BiAction<T, List<ModelView<?>>>() {

                @Override
                public void execute(T t, List<ModelView<?>> modelViews) {
                    // Make a copy of the closure, attach inputs and execute
                    Closure<?> cloned = closure.rehydrate(null, closure.getThisObject(), closure.getThisObject());
                    ((TransformedClosure) cloned).makeRule(new PotentialInputs(modelViews, inputValues), supportsNestedRules ? ClosureBackedRuleFactory.this : null);
                    ClosureBackedAction.execute(t, cloned);
                }
            }));
        }
    };
}
Also used : PotentialInput(org.gradle.model.dsl.internal.inputs.PotentialInput) BiAction(org.gradle.internal.BiAction) PotentialInputs(org.gradle.model.dsl.internal.inputs.PotentialInputs) List(java.util.List) ModelRuleDescriptor(org.gradle.model.internal.core.rule.describe.ModelRuleDescriptor)

Example 2 with BiAction

use of org.gradle.internal.BiAction in project gradle by gradle.

the class StructNodeInitializer method getActions.

@Override
public Multimap<ModelActionRole, ModelAction> getActions(ModelReference<?> subject, ModelRuleDescriptor descriptor) {
    return ImmutableSetMultimap.<ModelActionRole, ModelAction>builder().put(ModelActionRole.Discover, DirectNodeInputUsingModelAction.of(subject, descriptor, Arrays.<ModelReference<?>>asList(ModelReference.of(ManagedProxyFactory.class), ModelReference.of(TypeConverter.class)), new BiAction<MutableModelNode, List<ModelView<?>>>() {

        @Override
        public void execute(MutableModelNode modelNode, List<ModelView<?>> modelViews) {
            ManagedProxyFactory proxyFactory = getInstance(modelViews.get(0), ManagedProxyFactory.class);
            TypeConverter typeConverter = getInstance(modelViews, 1, TypeConverter.class);
            for (StructSchema<?> viewSchema : bindings.getDeclaredViewSchemas()) {
                addProjection(modelNode, viewSchema, proxyFactory, typeConverter);
            }
            modelNode.addProjection(new ModelElementProjection(bindings.getPublicSchema().getType()));
        }
    })).put(ModelActionRole.Create, DirectNodeInputUsingModelAction.of(subject, descriptor, Arrays.<ModelReference<?>>asList(ModelReference.of(ModelSchemaStore.class), ModelReference.of(NodeInitializerRegistry.class)), new BiAction<MutableModelNode, List<ModelView<?>>>() {

        @Override
        public void execute(MutableModelNode modelNode, List<ModelView<?>> modelViews) {
            ModelSchemaStore schemaStore = getInstance(modelViews, 0, ModelSchemaStore.class);
            NodeInitializerRegistry nodeInitializerRegistry = getInstance(modelViews, 1, NodeInitializerRegistry.class);
            addPropertyLinks(modelNode, schemaStore, nodeInitializerRegistry);
            initializePrivateData(modelNode);
        }
    })).build();
}
Also used : ManagedProxyFactory(org.gradle.model.internal.manage.instance.ManagedProxyFactory) BiAction(org.gradle.internal.BiAction) TypeConverter(org.gradle.internal.typeconversion.TypeConverter) List(java.util.List)

Aggregations

List (java.util.List)2 BiAction (org.gradle.internal.BiAction)2 TypeConverter (org.gradle.internal.typeconversion.TypeConverter)1 PotentialInput (org.gradle.model.dsl.internal.inputs.PotentialInput)1 PotentialInputs (org.gradle.model.dsl.internal.inputs.PotentialInputs)1 ModelRuleDescriptor (org.gradle.model.internal.core.rule.describe.ModelRuleDescriptor)1 ManagedProxyFactory (org.gradle.model.internal.manage.instance.ManagedProxyFactory)1