package org.implab.gradle.common.sources;

import org.gradle.api.GradleException;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Plugin;
import org.gradle.api.Project;

public abstract class VariantsPlugin implements Plugin<Project> {
    public static final String VARIANTS_EXTENSION_NAME = "variants";
    public static final String OUTGOING_VARIANTS_EXTENSION_NAME = "outgoingVariants";

    @Override
    public void apply(Project target) {
        var variants = target.getObjects().domainObjectContainer(BuildVariant.class);
        target.getExtensions().add(VARIANTS_EXTENSION_NAME, variants);
    }

    public static NamedDomainObjectContainer<BuildVariant> getVariantsExtension(Project target) {
        var extensions = target.getExtensions();

        @SuppressWarnings("unchecked")
        var extension = (NamedDomainObjectContainer<BuildVariant>)extensions.getByName(VARIANTS_EXTENSION_NAME);

        if (extension == null)
            throw new GradleException("Variants extension isn't found");
        return extension;
    }

}
