Skip to content

Instantly share code, notes, and snippets.

@realdadfish
Last active May 29, 2020 21:53
Show Gist options
  • Save realdadfish/0541d0768cb731d756c91e1f222f55ff to your computer and use it in GitHub Desktop.
Save realdadfish/0541d0768cb731d756c91e1f222f55ff to your computer and use it in GitHub Desktop.
Kotlin Gradle Plugin that shows how the generation and consumption of artifacts between different Gradle projects works
open class MyCodemarkerPlugin : Plugin<Project> {
override fun apply(project: Project) {
val androidExtension = project.extensions.findByType(BaseExtension::class.java)
if (androidExtension != null) {
project.createCodemarkerTask {
sources.from(project.files(androidExtension.sourceSets.map {
it.java.srcDirs + it.assets.srcDirs + it.res.srcDirs + it.manifest.srcFile
}))
}
return
}
val javaPluginConvention = project.convention.findPlugin(JavaPluginConvention::class.java)
if (javaPluginConvention != null) {
project.createCodemarkerTask {
sources.from(project.files(javaPluginConvention.sourceSets.map {
it.java.srcDirs + it.resources.srcDirs
}))
}
return
}
if (project == project.rootProject) {
val aggregatedFile = project.file("${project.buildDir}/reports/aggregated_codemarker.json")
project.createAggregateTask(aggregatedFile)
val publishingExtension = project.extensions.findByType(MyPublishingExtension::class.java)
publishingExtension?.let {
publishingExtension.file(aggregatedFile) {
classifier = Codemarker.MAVEN_CLASSIFIER
extension = Codemarker.MAVEN_EXTENSION
}
}
}
}
private fun Project.createCodemarkerTask(configuration: CountCodemarkerTask.() -> Unit) {
val task = tasks.register<CountCodemarkerTask>("countCodemarker") {
markers.set(MARKERS)
resultFile.set(project.file("${project.buildDir}/codemarker.json"))
configuration(this)
}
configurations.create(CODEMARKER_GENERATION) {
isCanBeResolved = false
isCanBeConsumed = true
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage::class.java, CODEMARKER_USAGE))
}
}
artifacts.add(CODEMARKER_GENERATION, task.map { it.resultFile })
}
private fun Project.createAggregateTask(aggregatedFile: File) {
tasks.register<AggregateCodemarkerTask>("aggregateCodemarker") {
codemarkers.from(configurations.getByName(CODEMARKER_CONSUMPTION))
aggregatedCodemarker.set(aggregatedFile)
}
configurations.create(CODEMARKER_CONSUMPTION) {
isCanBeResolved = true
isCanBeConsumed = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage::class.java, CODEMARKER_USAGE))
}
}
subprojects.forEach { subproject ->
if (subproject.configurations.findByName(CODEMARKER_GENERATION) != null) {
dependencies.add(CODEMARKER_CONSUMPTION, subproject)
}
}
}
companion object {
private val MARKERS = listOf("TODO", "FIXME")
private const val CODEMARKER_GENERATION = "codemarkerGeneration"
private const val CODEMARKER_CONSUMPTION = "codemarkerConsumption"
private const val CODEMARKER_USAGE = "codemarker-json"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment