Skip to content

Commit

Permalink
include immutables sources for jd and source jar
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jan 28, 2024
1 parent 5c24b96 commit a91664a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/org/incendo/cloudbuildlogic/BasePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BasePlugin : Plugin<Project> {
target.plugins.apply("net.kyori.indra")
target.plugins.apply("net.kyori.indra.checkstyle")
target.plugins.apply(BaselineImmutables::class)
target.plugins.apply(IncludeImmutablesSources::class)

target.extensions.configure(IndraExtension::class) {
javaVersions {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.incendo.cloudbuildlogic

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.bundling.Zip
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.javadoc.Javadoc
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.named

/**
* Plugin that includes generated sources in source jars and javadoc for source sets with the
* immutables value annotation processor. Must be applied *after* javadoc/sourcesJar is enabled.
*/
class IncludeImmutablesSources : Plugin<Project> {
override fun apply(project: Project) {
project.plugins.withId("java") {
project.extensions.getByType(SourceSetContainer::class).configureEach {
val sourceSet = this
val compile = project.tasks.named(compileJavaTaskName, JavaCompile::class)

project.tasks.maybeConfigure<Zip>(sourcesJarTaskName) {
from(generatedSourceOrEmpty(compile, project, sourceSet))
}
project.tasks.maybeConfigure<Javadoc>(javadocTaskName) {
source(generatedSourceOrEmpty(compile, project, sourceSet))
}
}
}
}
}

private fun generatedSourceOrEmpty(
compile: TaskProvider<JavaCompile>,
project: Project,
sourceSet: SourceSet
): Provider<Any> = compile.flatMap {
if (hasImmutablesProcessor(project, sourceSet)) {
it.options.generatedSourceOutputDirectory
} else {
project.provider { emptyList<Any>() }
}
}

/**
* Based on checks in [com.palantir.baseline.plugins.BaselineImmutables]
*/
private fun hasImmutablesProcessor(project: Project, sourceSet: SourceSet): Boolean {
return project
.configurations
.getByName(sourceSet.annotationProcessorConfigurationName)
.incoming
.resolutionResult
.allComponents
.any(::isImmutablesValue)
}

private fun isImmutablesValue(component: ResolvedComponentResult): Boolean {
val id = component.id as? ModuleComponentIdentifier ?: return false

return id.group == "org.immutables" && id.module == "value"
}
8 changes: 8 additions & 0 deletions src/main/kotlin/org/incendo/cloudbuildlogic/ext.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.incendo.cloudbuildlogic

import org.gradle.api.Action
import org.gradle.api.PolymorphicDomainObjectContainer
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.publish.maven.MavenPomDeveloperSpec
Expand All @@ -10,6 +12,12 @@ val ProviderFactory.ciBuild: Provider<Boolean>
.map { it.toBoolean() }
.orElse(false)

inline fun <reified S> PolymorphicDomainObjectContainer<in S>.maybeConfigure(name: String, op: Action<S>) {
if (name in names) {
named(name, S::class.java, op)
}
}

fun MavenPomDeveloperSpec.city() {
developer {
id.set("Citymonstret")
Expand Down

0 comments on commit a91664a

Please sign in to comment.