package org.implab.gradle.variants.sources;

import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectContainer;
import org.implab.gradle.common.core.lang.Closures;

import groovy.lang.Closure;

public interface VariantSourcesExtension {

    /**
     * Projection rules keyed by layer name.
     */
    NamedDomainObjectContainer<LayerProjectionRule> getLayerRules();

    /**
     * Creates or returns an existing layer rule and configures it.
     */
    default LayerProjectionRule layerRule(String name) {
        return getLayerRules().maybeCreate(name);
    }

    /**
     * Creates or returns an existing layer rule and configures it.
     */
    default LayerProjectionRule layer(String name, Action<? super LayerProjectionRule> action) {
        LayerProjectionRule rule = layerRule(name);
        action.execute(rule);
        return rule;
    }

    void whenFinalized(Action<? super VariantSourcesContext> action);

    default void whenFinalized(Closure<?> closure) {
        whenFinalized(Closures.action(closure));
    }
}