| @@ -0,0 +1,32 | |||
|
|
1 | package org.implab.gradle.common.sources; | |
|
|
2 | ||
|
|
3 | import java.util.HashMap; | |
|
|
4 | import java.util.Map; | |
|
|
5 | ||
|
|
6 | import org.gradle.api.Action; | |
|
|
7 | import org.gradle.api.Named; | |
|
|
8 | import org.gradle.api.NamedDomainObjectProvider; | |
|
|
9 | ||
|
|
10 | import org.gradle.api.artifacts.ConfigurationPublications; | |
|
|
11 | ||
|
|
12 | public abstract class BuildVariant implements Named { | |
|
|
13 | ||
|
|
14 | private final Map<String, VariantSourceSet> variantSourceSets = new HashMap<>(); | |
|
|
15 | ||
|
|
16 | private final SourceSetsSpec sourceSetsSpec = new SourceSetsSpec(); | |
|
|
17 | ||
|
|
18 | public void sourceSets(Action<? super SourceSetsSpec> action) { | |
|
|
19 | action.execute(sourceSetsSpec); | |
|
|
20 | } | |
|
|
21 | ||
|
|
22 | public void outgoing(Action<? super ConfigurationPublications> action) { | |
|
|
23 | ||
|
|
24 | } | |
|
|
25 | ||
|
|
26 | public class SourceSetsSpec { | |
|
|
27 | void add(NamedDomainObjectProvider<GenericSourceSet> sourceSet) { | |
|
|
28 | var variantSourceSet = new VariantSourceSet(sourceSet); | |
|
|
29 | variantSourceSets.put(sourceSet.getName(), variantSourceSet); | |
|
|
30 | } | |
|
|
31 | } | |
|
|
32 | } | |
| @@ -0,0 +1,19 | |||
|
|
1 | package org.implab.gradle.common.sources; | |
|
|
2 | ||
|
|
3 | import org.gradle.api.NamedDomainObjectProvider; | |
|
|
4 | ||
|
|
5 | public class VariantSourceSet { | |
|
|
6 | private final NamedDomainObjectProvider<GenericSourceSet> sourceSet; | |
|
|
7 | ||
|
|
8 | String getName() { | |
|
|
9 | return sourceSet.getName(); | |
|
|
10 | } | |
|
|
11 | ||
|
|
12 | public VariantSourceSet(NamedDomainObjectProvider<GenericSourceSet> sourceSet) { | |
|
|
13 | this.sourceSet = sourceSet; | |
|
|
14 | } | |
|
|
15 | ||
|
|
16 | public NamedDomainObjectProvider<GenericSourceSet> getSourceSet() { | |
|
|
17 | return sourceSet; | |
|
|
18 | } | |
|
|
19 | } | |
| @@ -0,0 +1,29 | |||
|
|
1 | package org.implab.gradle.common.sources; | |
|
|
2 | ||
|
|
3 | import org.gradle.api.GradleException; | |
|
|
4 | import org.gradle.api.NamedDomainObjectContainer; | |
|
|
5 | import org.gradle.api.Plugin; | |
|
|
6 | import org.gradle.api.Project; | |
|
|
7 | ||
|
|
8 | public abstract class VariantsPlugin implements Plugin<Project> { | |
|
|
9 | public static final String VARIANTS_EXTENSION_NAME = "variants"; | |
|
|
10 | public static final String OUTGOING_VARIANTS_EXTENSION_NAME = "outgoingVariants"; | |
|
|
11 | ||
|
|
12 | @Override | |
|
|
13 | public void apply(Project target) { | |
|
|
14 | var variants = target.getObjects().domainObjectContainer(BuildVariant.class); | |
|
|
15 | target.getExtensions().add(VARIANTS_EXTENSION_NAME, variants); | |
|
|
16 | } | |
|
|
17 | ||
|
|
18 | public static NamedDomainObjectContainer<BuildVariant> getVariantsExtension(Project target) { | |
|
|
19 | var extensions = target.getExtensions(); | |
|
|
20 | ||
|
|
21 | @SuppressWarnings("unchecked") | |
|
|
22 | var extension = (NamedDomainObjectContainer<BuildVariant>)extensions.getByName(VARIANTS_EXTENSION_NAME); | |
|
|
23 | ||
|
|
24 | if (extension == null) | |
|
|
25 | throw new GradleException("Variants extension isn't found"); | |
|
|
26 | return extension; | |
|
|
27 | } | |
|
|
28 | ||
|
|
29 | } | |
| @@ -1,59 +1,36 | |||
|
|
1 | 1 | package org.implab.gradle.common.sources; |
|
|
2 | 2 | |
|
|
3 |
import org.gradle.api. |
|
|
|
3 | import org.gradle.api.GradleException; | |
|
|
4 | import org.gradle.api.NamedDomainObjectContainer; | |
|
|
4 | 5 | import org.gradle.api.Plugin; |
|
|
5 | 6 | import org.gradle.api.Project; |
|
|
6 | 7 | import org.gradle.api.logging.Logger; |
|
|
7 | 8 | import org.gradle.api.logging.Logging; |
|
|
8 | import org.gradle.api.tasks.Copy; | |
|
|
9 | import org.gradle.api.tasks.Delete; | |
|
|
10 | import org.gradle.api.tasks.TaskContainer; | |
|
|
11 | import org.gradle.api.tasks.TaskProvider; | |
|
|
12 | import org.gradle.language.base.plugins.LifecycleBasePlugin; | |
|
|
13 | import org.implab.gradle.common.core.lang.Strings; | |
|
|
14 | 9 | |
|
|
15 | 10 | /** |
|
|
16 | 11 | * This plugin creates a {@code sources} extension which is |
|
|
17 | 12 | * a container for {@link GenericSourceSet}. |
|
|
18 | 13 | * |
|
|
19 | 14 | */ |
|
|
20 | 15 | public abstract class SourcesPlugin implements Plugin<Project> { |
|
|
21 | private final Logger logger = Logging.getLogger(SourcesPlugin.class); | |
|
|
16 | private static final Logger logger = Logging.getLogger(SourcesPlugin.class); | |
|
|
22 | 17 | |
|
|
23 |
private final String |
|
|
|
24 | ||
|
|
25 | private final String EXTENSION_NAME = "sources"; | |
|
|
18 | private static final String SOURCES_EXTENSION_NAME = "sources"; | |
|
|
26 | 19 | |
|
|
27 | 20 | @Override |
|
|
28 | 21 | public void apply(Project target) { |
|
|
29 | target.getPlugins().apply(LifecycleBasePlugin.class); | |
|
|
30 | ||
|
|
31 | 22 | var sources = target.getObjects().domainObjectContainer(GenericSourceSet.class); |
|
|
32 | var tasks = target.getTasks(); | |
|
|
33 | var clean = tasks.named(LifecycleBasePlugin.CLEAN_TASK_NAME); | |
|
|
34 | ||
|
|
35 | target.getExtensions().add(EXTENSION_NAME, sources); | |
|
|
36 | ||
|
|
37 | sources.configureEach(configureSourceSet(tasks, clean)); | |
|
|
23 | target.getExtensions().add(SOURCES_EXTENSION_NAME, sources); | |
|
|
38 | 24 | } |
|
|
39 | 25 | |
|
|
40 | private Action<GenericSourceSet> configureSourceSet(TaskContainer tasks, TaskProvider<?> clean) { | |
|
|
41 | return sourceSet -> { | |
|
|
42 | var name = sourceSet.getName(); | |
|
|
43 | ||
|
|
44 | logger.info("{}: Processing source set '{}'", POLICY_NAME, name); | |
|
|
45 | ||
|
|
46 | var taskName = "clean" + Strings.capitalize(sourceSet.getName()); | |
|
|
26 | public static NamedDomainObjectContainer<GenericSourceSet> getSourcesExtension(Project target) { | |
|
|
27 | var extensions = target.getExtensions(); | |
|
|
47 | 28 | |
|
|
48 | logger.info("{}: Register task '{}' of type [{}]", POLICY_NAME, taskName, Copy.class.getTypeName()); | |
|
|
29 | @SuppressWarnings("unchecked") | |
|
|
30 | var extension = (NamedDomainObjectContainer<GenericSourceSet>)extensions.getByName(SOURCES_EXTENSION_NAME); | |
|
|
49 | 31 | |
|
|
50 | var task = tasks.register(taskName, Delete.class, self -> { | |
|
|
51 | self.delete(sourceSet.getOutputsDir()); | |
|
|
52 | }); | |
|
|
53 | ||
|
|
54 | clean.configure(self -> { | |
|
|
55 | self.dependsOn(task); | |
|
|
56 | }); | |
|
|
57 | }; | |
|
|
32 | if (extension == null) | |
|
|
33 | throw new GradleException("Sources extension isn't found"); | |
|
|
34 | return extension; | |
|
|
58 | 35 | } |
|
|
59 | 36 | } |
General Comments 0
You need to be logged in to leave comments.
Login now
