| @@ -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 | } | |
| @@ -1,32 +1,29 | |||
|
|
1 | 1 | package org.implab.gradle.common.sources; |
|
|
2 | 2 | |
|
|
3 | 3 | import java.util.HashMap; |
|
|
4 | 4 | import java.util.Map; |
|
|
5 | 5 | |
|
|
6 | 6 | import org.gradle.api.Action; |
|
|
7 | 7 | import org.gradle.api.Named; |
|
|
8 | 8 | import org.gradle.api.NamedDomainObjectProvider; |
|
|
9 | 9 | |
|
|
10 | 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 | 15 | private final Map<String, VariantSourceSet> variantSourceSets = new HashMap<>(); |
|
|
15 | 16 | |
|
|
16 | 17 | private final SourceSetsSpec sourceSetsSpec = new SourceSetsSpec(); |
|
|
17 | 18 | |
|
|
18 | 19 | public void sourceSets(Action<? super SourceSetsSpec> action) { |
|
|
19 | 20 | action.execute(sourceSetsSpec); |
|
|
20 | 21 | } |
|
|
21 | 22 | |
|
|
22 | public void outgoing(Action<? super ConfigurationPublications> action) { | |
|
|
23 | ||
|
|
24 | } | |
|
|
25 | ||
|
|
26 | 23 | public class SourceSetsSpec { |
|
|
27 | 24 | void add(NamedDomainObjectProvider<GenericSourceSet> sourceSet) { |
|
|
28 | 25 | var variantSourceSet = new VariantSourceSet(sourceSet); |
|
|
29 | 26 | variantSourceSets.put(sourceSet.getName(), variantSourceSet); |
|
|
30 | 27 | } |
|
|
31 | 28 | } |
|
|
32 | 29 | } |
| @@ -1,29 +1,26 | |||
|
|
1 | 1 | package org.implab.gradle.common.sources; |
|
|
2 | 2 | |
|
|
3 | 3 | import org.gradle.api.GradleException; |
|
|
4 | import org.gradle.api.NamedDomainObjectContainer; | |
|
|
5 | 4 | import org.gradle.api.Plugin; |
|
|
6 | 5 | import org.gradle.api.Project; |
|
|
7 | 6 | |
|
|
8 | 7 | public abstract class VariantsPlugin implements Plugin<Project> { |
|
|
9 | 8 | public static final String VARIANTS_EXTENSION_NAME = "variants"; |
|
|
10 | public static final String OUTGOING_VARIANTS_EXTENSION_NAME = "outgoingVariants"; | |
|
|
11 | 9 | |
|
|
12 | 10 | @Override |
|
|
13 | 11 | public void apply(Project target) { |
|
|
14 |
var variants = target.getObjects(). |
|
|
|
12 | var variants = target.getObjects().newInstance(BuildVariantsExtension.class); | |
|
|
15 | 13 | target.getExtensions().add(VARIANTS_EXTENSION_NAME, variants); |
|
|
16 | 14 | } |
|
|
17 | 15 | |
|
|
18 |
public static |
|
|
|
16 | public static BuildVariantsExtension getVariantsExtension(Project target) { | |
|
|
19 | 17 | var extensions = target.getExtensions(); |
|
|
20 | 18 | |
|
|
21 | @SuppressWarnings("unchecked") | |
|
|
22 | var extension = (NamedDomainObjectContainer<BuildVariant>)extensions.getByName(VARIANTS_EXTENSION_NAME); | |
|
|
19 | var extension = extensions.getByType(BuildVariantsExtension.class); | |
|
|
23 | 20 | |
|
|
24 | 21 | if (extension == null) |
|
|
25 | 22 | throw new GradleException("Variants extension isn't found"); |
|
|
26 | 23 | return extension; |
|
|
27 | 24 | } |
|
|
28 | 25 | |
|
|
29 | 26 | } |
General Comments 0
You need to be logged in to leave comments.
Login now
