1 /*
<lambda>null2 * Copyright (C) 2024 The Dagger Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package dagger.gradle.build
18
19 import org.gradle.api.NamedDomainObjectContainer
20 import org.gradle.api.Project
21 import org.gradle.api.file.SourceDirectorySet
22 import org.gradle.api.plugins.JavaPluginExtension
23 import org.gradle.api.tasks.TaskProvider
24 import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
25 import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
26 import java.nio.file.Path
27 import kotlin.io.path.Path
28 import kotlin.io.path.isDirectory
29
30 private typealias JavaSourceSet = org.gradle.api.tasks.SourceSet
31
32 @DslMarker
33 annotation class DaggerGradleDsl
34
35 @DaggerGradleDsl
36 class DaggerSourceSet(
37 private val project: Project,
38 private val kotlinSourceSets: NamedDomainObjectContainer<KotlinSourceSet>,
39 private val javaSourceSets: NamedDomainObjectContainer<JavaSourceSet>,
40 ) {
41 private val resourceCopyTask: TaskProvider<ResourceCopyTask> =
42 project.tasks.register("copyResources", ResourceCopyTask::class.java) {
43 outputDirectory.set(project.layout.buildDirectory.dir("generated/resources"))
44 }
45
46 init {
47 listOf(resourceCopyTask.map { it.outputDirectory }).let {
48 kotlinSourceSets.named("main").configure { resources.setSrcDirs(it) }
49 javaSourceSets.named("main").configure { resources.setSrcDirs(it) }
50 }
51 }
52
53 /**
54 * The main source set whose based path is `<root>/java`
55 */
56 val main: SourceSet = object : SourceSet {
57 override fun setPackages(packages: List<String>) {
58 val packagePaths = packages.map { Path(it) }
59 kotlinSourceSets.named("main").configure {
60 kotlin.includePackages("${project.rootDir}/java", packagePaths)
61 }
62 javaSourceSets.named("main").configure {
63 java.includePackages("${project.rootDir}/java", packagePaths)
64 }
65 }
66
67 override fun setResources(resources: Map<String, String>) {
68 resourceCopyTask.configure {
69 val baseDir = project.rootProject.layout.projectDirectory.dir("java")
70 resources.forEach { (resourceFilePath, jarDirectoryPath) ->
71 val resource = baseDir.file(resourceFilePath)
72 resourceSpecs.put(resource.asFile.path, jarDirectoryPath)
73 inputFiles.add(resource)
74 }
75 }
76 }
77 }
78
79 /**
80 * The main source set whose based path is `<root>/javatests`
81 */
82 val test: SourceSet = object : SourceSet {
83 override fun setPackages(packages: List<String>) {
84 val packagePaths = packages.map { Path(it) }
85 kotlinSourceSets.named("test").configure {
86 kotlin.includePackages("${project.rootDir}/javatests", packagePaths)
87 }
88 javaSourceSets.named("test").configure {
89 java.includePackages("${project.rootDir}/javatests", packagePaths)
90 }
91 }
92
93 override fun setResources(resources: Map<String, String>) {
94 throw UnsupportedOperationException(
95 "Resources are only configurable for the 'main' source set."
96 )
97 }
98 }
99
100 interface SourceSet {
101 /**
102 * Sets the list of source packages that are part of the project's source set.
103 *
104 * Only sources directly in those packages are included and not in its subpackages.
105 *
106 * Example usage:
107 * ```
108 * daggerSources {
109 * main.setPackages(
110 * listOf(
111 * "dagger",
112 * "dagger/assisted",
113 * "dagger/internal",
114 * "dagger/multibindings",
115 * )
116 * )
117 * }
118 * ```
119 * @see daggerSources
120 */
121 fun setPackages(packages: List<String>)
122
123 /**
124 * Sets the resource file paths and their corresponding artifact location.
125 *
126 * Example usage:
127 * ```
128 * daggerSources {
129 * main.setResources(
130 * mapOf("dagger/r8.pro" to "META-INF/com.android.tools/r8/")
131 * )
132 * }
133 * ```
134 * @see daggerSources
135 */
136 fun setResources(resources: Map<String, String>)
137 }
138 }
139
140 /**
141 * Configure project's source set based on Dagger's project structure.
142 *
143 * Specifically it will include sources in the packages specified by
144 * [DaggerSourceSet.SourceSet.setPackages] and resources as specified by
145 * [DaggerSourceSet.SourceSet.setResources].
146 */
Projectnull147 fun Project.daggerSources(block: DaggerSourceSet.() -> Unit) {
148 val kotlinExtension = extensions.findByType(KotlinProjectExtension::class.java)
149 ?: error("The daggerSources() configuration must be applied to a Kotlin (JVM) project.")
150 val javaExtension = extensions.findByType(JavaPluginExtension::class.java)
151 ?: error("The daggerSources() configuration must be applied to a Kotlin (JVM) project.")
152 val daggerSources = DaggerSourceSet(this, kotlinExtension.sourceSets, javaExtension.sourceSets)
153 block.invoke(daggerSources)
154 }
155
156 /**
157 * Includes sources from the given [packages] into this source set.
158 *
159 * Only sources within the package directory are included and not its sub-packages.
160 */
SourceDirectorySetnull161 private fun SourceDirectorySet.includePackages(
162 basePath: String,
163 packages: Iterable<Path>,
164 ) {
165 val packagesDirectories = packages.flatMap { it.expandParts() }.toSet()
166 setSrcDirs(listOf(basePath)).include {
167 val path = Path(it.path)
168 if (Path(basePath).resolve(path).isDirectory()) {
169 path in packagesDirectories
170 } else {
171 path.parent in packages
172 }
173 }
174 }
175
176 /**
177 * Expands a [Path] to includes it parents.
178 *
179 * i.e. for `"foo/bar"` it will expand to `setOf("foo", foo/bar")`
180 */
Pathnull181 private fun Path.expandParts(): Set<Path> {
182 return buildSet {
183 var path: Path? = this@expandParts
184 while (path != null) {
185 add(path)
186 path = path.parent
187 }
188 }
189 }