[gradle-plugin] Generate javadoc and source jars for publishing
This commit is contained in:
+3
@@ -41,6 +41,9 @@ import org.jetbrains.kotlin.konan.target.KonanTarget
|
|||||||
// TODO: implement ComponentWithObjectFiles when we build klibs as objects
|
// TODO: implement ComponentWithObjectFiles when we build klibs as objects
|
||||||
interface KotlinNativeBinary: ComponentWithDependencies, BuildableComponent {
|
interface KotlinNativeBinary: ComponentWithDependencies, BuildableComponent {
|
||||||
|
|
||||||
|
/** A component this binary belongs to. */
|
||||||
|
val component: KotlinNativeComponent
|
||||||
|
|
||||||
/** Returns the source files of this binary. */
|
/** Returns the source files of this binary. */
|
||||||
val sources: FileCollection
|
val sources: FileCollection
|
||||||
|
|
||||||
|
|||||||
+3
@@ -62,6 +62,9 @@ interface KotlinNativeComponent: ComponentWithBinaries, ComponentWithDependencie
|
|||||||
fun extraOpts(values: List<Any>)
|
fun extraOpts(values: List<Any>)
|
||||||
|
|
||||||
fun pom(action: Action<MavenPom>)
|
fun pom(action: Action<MavenPom>)
|
||||||
|
|
||||||
|
val publishJavadoc: Boolean
|
||||||
|
val publishSources: Boolean
|
||||||
// endregion
|
// endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -58,7 +58,7 @@ import org.jetbrains.kotlin.konan.target.KonanTarget
|
|||||||
abstract class AbstractKotlinNativeBinary(
|
abstract class AbstractKotlinNativeBinary(
|
||||||
private val name: String,
|
private val name: String,
|
||||||
private val baseName: Provider<String>,
|
private val baseName: Provider<String>,
|
||||||
val component: KotlinNativeComponent,
|
override val component: AbstractKotlinNativeComponent,
|
||||||
val identity: KotlinNativeVariantIdentity,
|
val identity: KotlinNativeVariantIdentity,
|
||||||
val projectLayout: ProjectLayout,
|
val projectLayout: ProjectLayout,
|
||||||
override val kind: CompilerOutputKind,
|
override val kind: CompilerOutputKind,
|
||||||
|
|||||||
+4
@@ -88,5 +88,9 @@ abstract class AbstractKotlinNativeComponent @Inject constructor(
|
|||||||
override fun pom(action: Action<MavenPom>) {
|
override fun pom(action: Action<MavenPom>) {
|
||||||
poms.add(action)
|
poms.add(action)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override val publishJavadoc: Boolean = true
|
||||||
|
override val publishSources: Boolean = true
|
||||||
|
|
||||||
// endregion
|
// endregion
|
||||||
}
|
}
|
||||||
+1
-1
@@ -36,7 +36,7 @@ open class KotlinNativeTestExecutableImpl @Inject constructor(
|
|||||||
name: String,
|
name: String,
|
||||||
baseName: Provider<String>,
|
baseName: Provider<String>,
|
||||||
componentImplementation: Configuration,
|
componentImplementation: Configuration,
|
||||||
testComponent: KotlinNativeTestComponent,
|
testComponent: KotlinNativeTestSuite,
|
||||||
val mainSources: KotlinNativeSourceSet,
|
val mainSources: KotlinNativeSourceSet,
|
||||||
identity: KotlinNativeVariantIdentity,
|
identity: KotlinNativeVariantIdentity,
|
||||||
objects: ObjectFactory,
|
objects: ObjectFactory,
|
||||||
|
|||||||
+63
-16
@@ -27,6 +27,7 @@ import org.gradle.api.provider.Provider
|
|||||||
import org.gradle.api.publish.PublishingExtension
|
import org.gradle.api.publish.PublishingExtension
|
||||||
import org.gradle.api.publish.maven.MavenPublication
|
import org.gradle.api.publish.maven.MavenPublication
|
||||||
import org.gradle.internal.reflect.Instantiator
|
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.DEBUGGABLE_ATTRIBUTE
|
||||||
import org.gradle.language.cpp.CppBinary.OPTIMIZED_ATTRIBUTE
|
import org.gradle.language.cpp.CppBinary.OPTIMIZED_ATTRIBUTE
|
||||||
import org.gradle.language.cpp.internal.DefaultUsageContext
|
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 publishingExtension = project.extensions.getByType(PublishingExtension::class.java)
|
||||||
val components = project.components.withType(KotlinNativeMainComponent::class.java)
|
loop@for (publication in publishingExtension.publications) {
|
||||||
publishingExtension.publications.forEach { publication ->
|
|
||||||
(publication as? MavenPublication)?.apply {
|
if (publication !is MavenPublication) continue
|
||||||
val component = components.find {
|
val publicationComponent = components.find { it.name == publication.name } ?: continue
|
||||||
it.name == publication.name
|
|
||||||
|| publication.name.startsWith("${it.name}Release")
|
val sourcesJar: Jar
|
||||||
|| publication.name.startsWith("${it.name}Debug")
|
val mainComponent: AbstractKotlinNativeComponent
|
||||||
|
|
||||||
|
when (publicationComponent) {
|
||||||
|
is KotlinNativeMainComponent -> {
|
||||||
|
mainComponent = publicationComponent
|
||||||
|
sourcesJar = mainComponent.sources.createEmptyJarTask("sourcesJar", "sources")
|
||||||
}
|
}
|
||||||
component?.let {
|
is AbstractKotlinNativeBinary -> {
|
||||||
it.poms.forEach {
|
mainComponent = publicationComponent.component
|
||||||
publication.pom(it)
|
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
|
// 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
|
// 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).
|
// to determine which component class should be instantiated (main or test).
|
||||||
val mainSourceSet = sourceSets.create("main").apply {
|
val mainSourceSet = sourceSets.create(MAIN_SOURCE_SET_NAME).apply {
|
||||||
kotlin.srcDir("src/main/kotlin")
|
kotlin.srcDir("src/$MAIN_SOURCE_SET_NAME/kotlin")
|
||||||
component = objectFactory
|
component = objectFactory
|
||||||
.newInstance(KotlinNativeMainComponent::class.java, name, this)
|
.newInstance(KotlinNativeMainComponent::class.java, name, this)
|
||||||
.apply {
|
.apply {
|
||||||
@@ -232,8 +274,8 @@ class KotlinNativePlugin @Inject constructor(val attributesFactory: ImmutableAtt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sourceSets.create("test").apply {
|
sourceSets.create(TEST_SOURCE_SET_NAME).apply {
|
||||||
kotlin.srcDir("src/test/kotlin")
|
kotlin.srcDir("src/$TEST_SOURCE_SET_NAME/kotlin")
|
||||||
component = objectFactory
|
component = objectFactory
|
||||||
.newInstance(KotlinNativeTestSuite::class.java, name, this, mainSourceSet.component)
|
.newInstance(KotlinNativeTestSuite::class.java, name, this, mainSourceSet.component)
|
||||||
.apply {
|
.apply {
|
||||||
@@ -250,4 +292,9 @@ class KotlinNativePlugin @Inject constructor(val attributesFactory: ImmutableAtt
|
|||||||
setUpMavenPublish()
|
setUpMavenPublish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val MAIN_SOURCE_SET_NAME = "main"
|
||||||
|
const val TEST_SOURCE_SET_NAME = "test"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user