Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include immutables sources for jd and source jar #8

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading