diff --git a/variants/src/main/java/org/implab/gradle/variants/VariantSourcesPlugin.java b/variants/src/main/java/org/implab/gradle/variants/VariantSourcesPlugin.java --- a/variants/src/main/java/org/implab/gradle/variants/VariantSourcesPlugin.java +++ b/variants/src/main/java/org/implab/gradle/variants/VariantSourcesPlugin.java @@ -16,8 +16,8 @@ import org.implab.gradle.variants.core.V import org.implab.gradle.variants.core.VariantsExtension; import org.implab.gradle.variants.core.VariantsView; import org.implab.gradle.variants.sources.CompileUnit; +import org.implab.gradle.variants.sources.CompileUnitSourceSetSpec; import org.implab.gradle.variants.sources.CompileUnitsView; -import org.implab.gradle.variants.sources.GenericSourceSet; import org.implab.gradle.variants.sources.RoleProjectionsView; import org.implab.gradle.variants.sources.VariantSourcesContext; import org.implab.gradle.variants.sources.VariantSourcesExtension; @@ -92,7 +92,7 @@ public abstract class VariantSourcesPlug } @Override - public void variant(String variantName, Action action) { + public void variant(String variantName, Action action) { Strings.argumentNotNullOrBlank(variantName, "variantName"); Objects.requireNonNull(action, "action can't be null"); @@ -102,7 +102,7 @@ public abstract class VariantSourcesPlug } @Override - public void layer(String layerName, Action action) { + public void layer(String layerName, Action action) { // protect external DSL Strings.argumentNotNullOrBlank(layerName, "layerName"); Objects.requireNonNull(action, "action can't be null"); @@ -113,7 +113,7 @@ public abstract class VariantSourcesPlug } @Override - public void unit(String variantName, String layerName, Action action) { + public void unit(String variantName, String layerName, Action action) { Strings.argumentNotNullOrBlank(layerName, "layerName"); Strings.argumentNotNullOrBlank(variantName, "variantName"); Objects.requireNonNull(action, "action can't be null"); @@ -122,6 +122,15 @@ public abstract class VariantSourcesPlug whenAvailable(ctx -> ctx.configureUnit(resolveCompileUnit(ctx, variantName, layerName), action)); } + + @Override + public void configureEach(Action action) { + Objects.requireNonNull(action, "action can't be null"); + + lateConfigurationPolicy.finalizePolicy(); + + whenAvailable(ctx -> ctx.configureEach(action)); + } }; extensions.add(VariantSourcesExtension.class, VARIANT_SOURCES_EXTENSION, variantSourcesExtension); diff --git a/variants/src/main/java/org/implab/gradle/variants/sources/CompileUnitSourceSetSpec.java b/variants/src/main/java/org/implab/gradle/variants/sources/CompileUnitSourceSetSpec.java new file mode 100644 --- /dev/null +++ b/variants/src/main/java/org/implab/gradle/variants/sources/CompileUnitSourceSetSpec.java @@ -0,0 +1,69 @@ +package org.implab.gradle.variants.sources; + +import org.gradle.api.Action; +import org.implab.gradle.common.core.lang.Closures; +import org.implab.gradle.variants.core.Layer; +import org.implab.gradle.variants.core.Variant; + +import groovy.lang.Closure; + +/** + * Source-set materialization context for one compile unit. + * + *

This type keeps compile-unit identity next to the materialized + * {@link GenericSourceSet} without turning the selector DSL into a second + * source-set API. Use {@link #getCompileUnit()} for selection metadata and + * {@link #sourceSet(Action)} when the underlying source set body must be + * configured. + */ +public interface CompileUnitSourceSetSpec { + /** + * Returns the compile unit represented by this source set. + * + * @return compile-unit identity + */ + CompileUnit getCompileUnit(); + + /** + * Returns the variant part of the compile-unit identity. + * + * @return variant identity + */ + default Variant getVariant() { + return getCompileUnit().variant(); + } + + /** + * Returns the layer part of the compile-unit identity. + * + * @return layer identity + */ + default Layer getLayer() { + return getCompileUnit().layer(); + } + + /** + * Returns the materialized source set body. + * + * @return source set body + */ + GenericSourceSet getSourceSet(); + + /** + * Configures the materialized source set body. + * + * @param action source set configuration action + */ + default void sourceSet(Action action) { + action.execute(getSourceSet()); + } + + /** + * Configures the materialized source set body. + * + * @param closure source set configuration closure + */ + default void sourceSet(Closure closure) { + sourceSet(Closures.action(closure)); + } +} diff --git a/variants/src/main/java/org/implab/gradle/variants/sources/GenericSourceSet.java b/variants/src/main/java/org/implab/gradle/variants/sources/GenericSourceSet.java --- a/variants/src/main/java/org/implab/gradle/variants/sources/GenericSourceSet.java +++ b/variants/src/main/java/org/implab/gradle/variants/sources/GenericSourceSet.java @@ -26,10 +26,6 @@ import org.gradle.api.file.ProjectLayout import org.gradle.api.file.SourceDirectorySet; import org.gradle.api.model.ObjectFactory; import org.gradle.api.tasks.TaskProvider; -import org.gradle.util.Configurable; -import org.implab.gradle.common.core.lang.Closures; - -import groovy.lang.Closure; /** * A configurable source set abstraction with named outputs. @@ -50,8 +46,7 @@ import groovy.lang.Closure; * {@link InvalidUserDataException}. *

*/ -public abstract class GenericSourceSet - implements Named, Configurable { +public abstract class GenericSourceSet implements Named { private final String name; private final NamedDomainObjectContainer sourceDirectorySets; @@ -172,16 +167,6 @@ public abstract class GenericSourceSet .builtBy(task); } - /** - * Applies a Groovy closure to this source set, enabling DSL-style - * configuration. - */ - @Override - public GenericSourceSet configure(Closure configure) { - Closures.apply(configure, this); - return this; - } - private SourceDirectorySet createSourceDirectorySet(String name) { return objects.sourceDirectorySet(name, name); } diff --git a/variants/src/main/java/org/implab/gradle/variants/sources/VariantSourcesContext.java b/variants/src/main/java/org/implab/gradle/variants/sources/VariantSourcesContext.java --- a/variants/src/main/java/org/implab/gradle/variants/sources/VariantSourcesContext.java +++ b/variants/src/main/java/org/implab/gradle/variants/sources/VariantSourcesContext.java @@ -35,7 +35,7 @@ public interface VariantSourcesContext { SourceSetMaterializer getSourceSets(); /** - * Configures all GenericSourceSets produced from the given layer. + * Configures all source sets produced from the given layer. * * The action is applied: * - to already materialized source sets of this layer @@ -52,10 +52,10 @@ public interface VariantSourcesContext { * @throws org.gradle.api.InvalidUserDataException if the layer is not part of * the finalized variant model */ - void configureLayer(Layer layer, Action action); + void configureLayer(Layer layer, Action action); /** - * Configures all GenericSourceSets produced from the given variant. + * Configures all source sets produced from the given variant. * *

Late application semantics for already materialized source sets are * governed by @@ -64,10 +64,10 @@ public interface VariantSourcesContext { * @throws org.gradle.api.InvalidUserDataException if the variant is not part * of the finalized variant model */ - void configureVariant(Variant variant, Action action); + void configureVariant(Variant variant, Action action); /** - * Configures the GenericSourceSet produced from the given compile unit. + * Configures the source set produced from the given compile unit. * *

Late application semantics for already materialized source sets are * governed by @@ -76,6 +76,18 @@ public interface VariantSourcesContext { * @throws org.gradle.api.InvalidUserDataException if the compile unit is not * part of the finalized variant model */ - void configureUnit(CompileUnit unit, Action action); + void configureUnit(CompileUnit unit, Action action); + + /** + * Configures every source set materialized from the finalized compile-unit + * model. + * + *

This selector is applied before variant-, layer-, and unit-specific + * selectors. + * + * @throws org.gradle.api.InvalidUserDataException if late configuration is + * rejected by the selected policy + */ + void configureEach(Action action); } diff --git a/variants/src/main/java/org/implab/gradle/variants/sources/VariantSourcesExtension.java b/variants/src/main/java/org/implab/gradle/variants/sources/VariantSourcesExtension.java --- a/variants/src/main/java/org/implab/gradle/variants/sources/VariantSourcesExtension.java +++ b/variants/src/main/java/org/implab/gradle/variants/sources/VariantSourcesExtension.java @@ -10,7 +10,7 @@ public interface VariantSourcesExtension /** * Selects how selector rules behave when they target an already materialized - * {@link GenericSourceSet}. + * source set. * *

This policy is single-valued: *