| @@ -0,0 +1,51 | |||||
|
|
1 | package org.implab.gradle.common.sources; | |||
|
|
2 | ||||
|
|
3 | import java.util.ArrayList; | |||
|
|
4 | import java.util.Collection; | |||
|
|
5 | import java.util.Collections; | |||
|
|
6 | import java.util.HashMap; | |||
|
|
7 | import java.util.List; | |||
|
|
8 | import java.util.Map; | |||
|
|
9 | ||||
|
|
10 | import javax.inject.Inject; | |||
|
|
11 | ||||
|
|
12 | import org.gradle.api.Action; | |||
|
|
13 | import org.gradle.api.model.ObjectFactory; | |||
|
|
14 | ||||
|
|
15 | public abstract class BuildVariantsExtension { | |||
|
|
16 | private final ObjectFactory objects; | |||
|
|
17 | private final Map<String, BuildVariant> variants = new HashMap<>(); | |||
|
|
18 | private final List<Action<? super BuildVariant>> listeners = new ArrayList<>(); | |||
|
|
19 | ||||
|
|
20 | @Inject | |||
|
|
21 | public BuildVariantsExtension(ObjectFactory objects) { | |||
|
|
22 | this.objects = objects; | |||
|
|
23 | } | |||
|
|
24 | ||||
|
|
25 | private BuildVariant newVariant(String name) { | |||
|
|
26 | return objects.newInstance(BuildVariant.class, name); | |||
|
|
27 | } | |||
|
|
28 | ||||
|
|
29 | public BuildVariant variant(String name, Action<? super BuildVariant> configure) { | |||
|
|
30 | BuildVariant v = variants.computeIfAbsent(name, this::newVariant); | |||
|
|
31 | configure.execute(v); | |||
|
|
32 | for (Action<? super BuildVariant> l : listeners) { | |||
|
|
33 | l.execute(v); | |||
|
|
34 | } | |||
|
|
35 | return v; | |||
|
|
36 | } | |||
|
|
37 | ||||
|
|
38 | public void all(Action<? super BuildVariant> action) { | |||
|
|
39 | variants.values().forEach(action::execute); | |||
|
|
40 | listeners.add(action); | |||
|
|
41 | } | |||
|
|
42 | ||||
|
|
43 | public Collection<BuildVariant> getAll() { | |||
|
|
44 | return Collections.unmodifiableCollection(variants.values()); | |||
|
|
45 | } | |||
|
|
46 | ||||
|
|
47 | public BuildVariant getByName(String name) { | |||
|
|
48 | return variants.get(name); | |||
|
|
49 | } | |||
|
|
50 | ||||
|
|
51 | } | |||
| @@ -8,8 +8,9 import org.gradle.api.Named; | |||||
| 8 | import org.gradle.api.NamedDomainObjectProvider; |
|
8 | import org.gradle.api.NamedDomainObjectProvider; | |
| 9 |
|
9 | |||
| 10 | import org.gradle.api.artifacts.ConfigurationPublications; |
|
10 | import org.gradle.api.artifacts.ConfigurationPublications; | |
|
|
11 | import org.gradle.api.plugins.ExtensionAware; | |||
| 11 |
|
12 | |||
| 12 | public abstract class BuildVariant implements Named { |
|
13 | public abstract class BuildVariant implements Named, ExtensionAware { | |
| 13 |
|
14 | |||
| 14 | private final Map<String, VariantSourceSet> variantSourceSets = new HashMap<>(); |
|
15 | private final Map<String, VariantSourceSet> variantSourceSets = new HashMap<>(); | |
| 15 |
|
16 | |||
| @@ -19,10 +20,6 public abstract class BuildVariant imple | |||||
| 19 | action.execute(sourceSetsSpec); |
|
20 | action.execute(sourceSetsSpec); | |
| 20 | } |
|
21 | } | |
| 21 |
|
22 | |||
| 22 | public void outgoing(Action<? super ConfigurationPublications> action) { |
|
|||
| 23 |
|
||||
| 24 | } |
|
|||
| 25 |
|
||||
| 26 | public class SourceSetsSpec { |
|
23 | public class SourceSetsSpec { | |
| 27 | void add(NamedDomainObjectProvider<GenericSourceSet> sourceSet) { |
|
24 | void add(NamedDomainObjectProvider<GenericSourceSet> sourceSet) { | |
| 28 | var variantSourceSet = new VariantSourceSet(sourceSet); |
|
25 | var variantSourceSet = new VariantSourceSet(sourceSet); | |
| @@ -1,25 +1,22 | |||||
| 1 | package org.implab.gradle.common.sources; |
|
1 | package org.implab.gradle.common.sources; | |
| 2 |
|
2 | |||
| 3 | import org.gradle.api.GradleException; |
|
3 | import org.gradle.api.GradleException; | |
| 4 | import org.gradle.api.NamedDomainObjectContainer; |
|
|||
| 5 | import org.gradle.api.Plugin; |
|
4 | import org.gradle.api.Plugin; | |
| 6 | import org.gradle.api.Project; |
|
5 | import org.gradle.api.Project; | |
| 7 |
|
6 | |||
| 8 | public abstract class VariantsPlugin implements Plugin<Project> { |
|
7 | public abstract class VariantsPlugin implements Plugin<Project> { | |
| 9 | public static final String VARIANTS_EXTENSION_NAME = "variants"; |
|
8 | public static final String VARIANTS_EXTENSION_NAME = "variants"; | |
| 10 | public static final String OUTGOING_VARIANTS_EXTENSION_NAME = "outgoingVariants"; |
|
|||
| 11 |
|
9 | |||
| 12 | @Override |
|
10 | @Override | |
| 13 | public void apply(Project target) { |
|
11 | public void apply(Project target) { | |
| 14 |
var variants = target.getObjects(). |
|
12 | var variants = target.getObjects().newInstance(BuildVariantsExtension.class); | |
| 15 | target.getExtensions().add(VARIANTS_EXTENSION_NAME, variants); |
|
13 | target.getExtensions().add(VARIANTS_EXTENSION_NAME, variants); | |
| 16 | } |
|
14 | } | |
| 17 |
|
15 | |||
| 18 |
public static |
|
16 | public static BuildVariantsExtension getVariantsExtension(Project target) { | |
| 19 | var extensions = target.getExtensions(); |
|
17 | var extensions = target.getExtensions(); | |
| 20 |
|
18 | |||
| 21 | @SuppressWarnings("unchecked") |
|
19 | var extension = extensions.getByType(BuildVariantsExtension.class); | |
| 22 | var extension = (NamedDomainObjectContainer<BuildVariant>)extensions.getByName(VARIANTS_EXTENSION_NAME); |
|
|||
| 23 |
|
20 | |||
| 24 | if (extension == null) |
|
21 | if (extension == null) | |
| 25 | throw new GradleException("Variants extension isn't found"); |
|
22 | throw new GradleException("Variants extension isn't found"); | |
General Comments 0
You need to be logged in to leave comments.
Login now
