<lambda>null1 package com.android.ndkports
2
3 import org.gradle.api.InvalidUserDataException
4 import org.gradle.api.Plugin
5 import org.gradle.api.Project
6 import org.gradle.api.artifacts.Configuration
7 import org.gradle.api.attributes.Attribute
8 import org.gradle.api.component.SoftwareComponentFactory
9 import org.gradle.api.file.DirectoryProperty
10 import org.gradle.api.file.RegularFileProperty
11 import org.gradle.api.model.ObjectFactory
12 import org.gradle.api.plugins.BasePlugin
13 import org.gradle.api.provider.Property
14 import org.gradle.api.provider.Provider
15 import org.gradle.api.tasks.bundling.Zip
16 import javax.inject.Inject
17
18 abstract class NdkPortsExtension {
19 abstract val source: RegularFileProperty
20
21 abstract val ndkPath: DirectoryProperty
22
23 abstract val minSdkVersion: Property<Int>
24 }
25
26 class NdkPortsPluginImpl(
27 private val project: Project,
28 private val softwareComponentFactory: SoftwareComponentFactory,
29 objects: ObjectFactory,
30 ) {
31 private val topBuildDir = project.buildDir.resolve("port")
32
33 private val extension =
34 project.extensions.create("ndkPorts", NdkPortsExtension::class.java)
35
36 private var portTaskAdded: Boolean = false
37 private val portTask = objects.property(PortTask::class.java)
38 private lateinit var prefabTask: Provider<PrefabTask>
39 private lateinit var extractTask: Provider<SourceExtractTask>
40 private lateinit var packageTask: Provider<PackageBuilderTask>
41 private lateinit var aarTask: Provider<Zip>
42
43 private lateinit var implementation: Configuration
44 private lateinit var exportedAars: Configuration
45 private lateinit var consumedAars: Configuration
46
47 private val artifactType = Attribute.of("artifactType", String::class.java)
48
createConfigurationsnull49 private fun createConfigurations() {
50 implementation = project.configurations.create("implementation") {
51 it.isCanBeResolved = false
52 it.isCanBeConsumed = false
53 }
54
55 exportedAars = project.configurations.create("exportedAars") {
56 it.isCanBeResolved = false
57 it.isCanBeConsumed = true
58 it.extendsFrom(implementation)
59 it.attributes { attributes ->
60 with(attributes) {
61 attribute(artifactType, "aar")
62 }
63 }
64 }
65
66 consumedAars = project.configurations.create("consumedAars") {
67 it.isCanBeResolved = true
68 it.isCanBeConsumed = false
69 it.extendsFrom(implementation)
70 it.attributes { attributes ->
71 with(attributes) {
72 attribute(artifactType, "aar")
73 }
74 }
75 }
76 }
77
createTasksnull78 private fun createTasks() {
79 prefabTask = project.tasks.register("prefab", PrefabTask::class.java) {
80 with(it) {
81 aars = consumedAars.incoming.artifacts.artifactFiles
82 outputDirectory.set(topBuildDir.resolve("dependencies"))
83 ndkPath.set(extension.ndkPath)
84 minSdkVersion.set(extension.minSdkVersion)
85 }
86 }
87
88 extractTask = project.tasks.register(
89 "extractSrc", SourceExtractTask::class.java
90 ) {
91 with(it) {
92 source.set(extension.source)
93 outDir.set(topBuildDir.resolve("src"))
94 }
95 }
96
97 packageTask = project.tasks.register(
98 "prefabPackage", PackageBuilderTask::class.java
99 ) {
100 if (!portTask.isPresent) {
101 throw InvalidUserDataException(
102 "The ndkports plugin was applied but no port task was " +
103 "registered. A task deriving from NdkPortsTask " +
104 "must be registered."
105 )
106 }
107 with(it) {
108 sourceDirectory.set(extractTask.get().outDir)
109 outDir.set(topBuildDir)
110 ndkPath.set(extension.ndkPath)
111 installDirectory.set(portTask.get().installDir)
112 minSdkVersion.set(extension.minSdkVersion)
113 }
114 }
115
116 aarTask = project.tasks.register("packageAar", Zip::class.java) {
117 it.from(packageTask.get().intermediatesDirectory)
118 it.archiveExtension.set("aar")
119 it.dependsOn(packageTask)
120 }
121
122 project.artifacts.add(exportedAars.name, aarTask)
123
124 val portTasks = project.tasks.withType(PortTask::class.java)
125 portTasks.whenTaskAdded { portTask ->
126 if (portTaskAdded) {
127 throw InvalidUserDataException(
128 "Cannot define multiple port tasks for a single module"
129 )
130 }
131 portTaskAdded = true
132 this.portTask.set(portTask)
133
134 with (portTask) {
135 sourceDirectory.set(extractTask.get().outDir)
136 ndkPath.set(extension.ndkPath)
137 buildDir.set(topBuildDir)
138 minSdkVersion.set(extension.minSdkVersion)
139 prefabGenerated.set(prefabTask.get().generatedDirectory)
140 }
141 }
142
143 val testTasks =
144 project.tasks.withType(AndroidExecutableTestTask::class.java)
145 testTasks.whenTaskAdded { testTask ->
146 with (testTask) {
147 dependsOn(aarTask)
148 minSdkVersion.set(extension.minSdkVersion)
149 ndkPath.set(extension.ndkPath)
150 }
151 project.tasks.getByName("check").dependsOn(testTask)
152 }
153 }
154
createComponentsnull155 private fun createComponents() {
156 val adhocComponent = softwareComponentFactory.adhoc("prefab")
157 project.components.add(adhocComponent)
158 adhocComponent.addVariantsFromConfiguration(exportedAars) {
159 it.mapToMavenScope("runtime")
160 }
161 }
162
applynull163 fun apply() {
164 project.pluginManager.apply(BasePlugin::class.java)
165 createConfigurations()
166 createTasks()
167 createComponents()
168 }
169 }
170
171 @Suppress("UnstableApiUsage", "Unused")
172 class NdkPortsPlugin @Inject constructor(
173 private val objects: ObjectFactory,
174 private val softwareComponentFactory: SoftwareComponentFactory,
175 ) : Plugin<Project> {
applynull176 override fun apply(project: Project) {
177 NdkPortsPluginImpl(project, softwareComponentFactory, objects).apply()
178 }
179 }