[gradle-plugin] Generate javadoc and source jars for publishing

This commit is contained in:
Ilya Matveev
2018-07-23 20:53:09 +07:00
committed by ilmat192
parent a3a26b60ce
commit a68c864e7a
6 changed files with 75 additions and 18 deletions
@@ -41,6 +41,9 @@ import org.jetbrains.kotlin.konan.target.KonanTarget
// TODO: implement ComponentWithObjectFiles when we build klibs as objects
interface KotlinNativeBinary: ComponentWithDependencies, BuildableComponent {
/** A component this binary belongs to. */
val component: KotlinNativeComponent
/** Returns the source files of this binary. */
val sources: FileCollection
@@ -62,6 +62,9 @@ interface KotlinNativeComponent: ComponentWithBinaries, ComponentWithDependencie
fun extraOpts(values: List<Any>)
fun pom(action: Action<MavenPom>)
val publishJavadoc: Boolean
val publishSources: Boolean
// endregion
}
@@ -58,7 +58,7 @@ import org.jetbrains.kotlin.konan.target.KonanTarget
abstract class AbstractKotlinNativeBinary(
private val name: String,
private val baseName: Provider<String>,
val component: KotlinNativeComponent,
override val component: AbstractKotlinNativeComponent,
val identity: KotlinNativeVariantIdentity,
val projectLayout: ProjectLayout,
override val kind: CompilerOutputKind,
@@ -88,5 +88,9 @@ abstract class AbstractKotlinNativeComponent @Inject constructor(
override fun pom(action: Action<MavenPom>) {
poms.add(action)
}
override val publishJavadoc: Boolean = true
override val publishSources: Boolean = true
// endregion
}
@@ -36,7 +36,7 @@ open class KotlinNativeTestExecutableImpl @Inject constructor(
name: String,
baseName: Provider<String>,
componentImplementation: Configuration,
testComponent: KotlinNativeTestComponent,
testComponent: KotlinNativeTestSuite,
val mainSources: KotlinNativeSourceSet,
identity: KotlinNativeVariantIdentity,
objects: ObjectFactory,
@@ -27,6 +27,7 @@ import org.gradle.api.provider.Provider
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.internal.reflect.Instantiator
import org.gradle.jvm.tasks.Jar
import org.gradle.language.cpp.CppBinary.DEBUGGABLE_ATTRIBUTE
import org.gradle.language.cpp.CppBinary.OPTIMIZED_ATTRIBUTE
import org.gradle.language.cpp.internal.DefaultUsageContext
@@ -183,21 +184,62 @@ class KotlinNativePlugin @Inject constructor(val attributesFactory: ImmutableAtt
}
}
private fun Project.setUpMavenPublish() = pluginManager.withPlugin("maven-publish") {
private val KotlinNativeSourceSetImpl.sourceSetSuffix: String
get() = name.let { if (it == MAIN_SOURCE_SET_NAME) "" else it }
private fun KotlinNativeSourceSetImpl.createSourcesJarTask(target: KonanTarget): Jar {
val sources = getAllSources(target)
val taskName = "sourcesJar${sourceSetSuffix}${target.name}"
val task = project.tasks.findByName(taskName)
return if (task != null) {
task as Jar
} else {
project.tasks.create(taskName, Jar::class.java) {
it.baseName = name
it.appendix = target.name
it.classifier = "sources"
it.from(sources)
}
}
}
private fun KotlinNativeSourceSetImpl.createEmptyJarTask(namePrefix: String, classifier: String): Jar =
project.tasks.maybeCreate("$namePrefix$sourceSetSuffix", Jar::class.java).apply {
baseName = name
this.classifier = classifier
}
private fun Project.setUpMavenPublish() = pluginManager.withPlugin("maven-publish") { _ ->
val publishingExtension = project.extensions.getByType(PublishingExtension::class.java)
val components = project.components.withType(KotlinNativeMainComponent::class.java)
publishingExtension.publications.forEach { publication ->
(publication as? MavenPublication)?.apply {
val component = components.find {
it.name == publication.name
|| publication.name.startsWith("${it.name}Release")
|| publication.name.startsWith("${it.name}Debug")
loop@for (publication in publishingExtension.publications) {
if (publication !is MavenPublication) continue
val publicationComponent = components.find { it.name == publication.name } ?: continue
val sourcesJar: Jar
val mainComponent: AbstractKotlinNativeComponent
when (publicationComponent) {
is KotlinNativeMainComponent -> {
mainComponent = publicationComponent
sourcesJar = mainComponent.sources.createEmptyJarTask("sourcesJar", "sources")
}
component?.let {
it.poms.forEach {
publication.pom(it)
}
is AbstractKotlinNativeBinary -> {
mainComponent = publicationComponent.component
sourcesJar = mainComponent.sources.createSourcesJarTask(publicationComponent.konanTarget)
}
else -> {
logger.info("Unknown component type: $publicationComponent, ${publicationComponent::class.java}")
continue@loop
}
}
val javadocJar = mainComponent.sources.createEmptyJarTask("javadocJar", "javadoc")
with(mainComponent) {
poms.forEach { publication.pom(it) }
if (publishSources) { publication.artifact(sourcesJar) }
if (publishJavadoc) { publication.artifact(javadocJar) }
}
}
}
@@ -221,8 +263,8 @@ class KotlinNativePlugin @Inject constructor(val attributesFactory: ImmutableAtt
// TODO: We should be able to create a component automatically once
// a source set is created by user. So we need a user DSL or some another way
// to determine which component class should be instantiated (main or test).
val mainSourceSet = sourceSets.create("main").apply {
kotlin.srcDir("src/main/kotlin")
val mainSourceSet = sourceSets.create(MAIN_SOURCE_SET_NAME).apply {
kotlin.srcDir("src/$MAIN_SOURCE_SET_NAME/kotlin")
component = objectFactory
.newInstance(KotlinNativeMainComponent::class.java, name, this)
.apply {
@@ -232,8 +274,8 @@ class KotlinNativePlugin @Inject constructor(val attributesFactory: ImmutableAtt
}
}
sourceSets.create("test").apply {
kotlin.srcDir("src/test/kotlin")
sourceSets.create(TEST_SOURCE_SET_NAME).apply {
kotlin.srcDir("src/$TEST_SOURCE_SET_NAME/kotlin")
component = objectFactory
.newInstance(KotlinNativeTestSuite::class.java, name, this, mainSourceSet.component)
.apply {
@@ -250,4 +292,9 @@ class KotlinNativePlugin @Inject constructor(val attributesFactory: ImmutableAtt
setUpMavenPublish()
}
}
companion object {
const val MAIN_SOURCE_SET_NAME = "main"
const val TEST_SOURCE_SET_NAME = "test"
}
}