From 08fbbc554b1267d6f7803887ad0d1aa0a796498a Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Wed, 14 Dec 2016 20:23:24 +0300 Subject: Hotfix 0.9.11, now dokka-android should work properly --- runners/android-gradle-plugin/build.gradle | 12 ++- .../android-gradle-plugin/src/main/kotlin/main.kt | 86 --------------------- .../src/main/kotlin/mainAndroid.kt | 88 ++++++++++++++++++++++ .../org.jetbrains.dokka-android.properties | 3 +- runners/gradle-plugin/src/main/kotlin/main.kt | 26 ++++--- 5 files changed, 118 insertions(+), 97 deletions(-) delete mode 100644 runners/android-gradle-plugin/src/main/kotlin/main.kt create mode 100644 runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt diff --git a/runners/android-gradle-plugin/build.gradle b/runners/android-gradle-plugin/build.gradle index 55bce3f4..afb0fb72 100644 --- a/runners/android-gradle-plugin/build.gradle +++ b/runners/android-gradle-plugin/build.gradle @@ -26,13 +26,23 @@ dependencies { provided gradleApi() provided localGroovy() - shadow 'com.android.tools.build:gradle:2.2.0' + provided 'com.android.tools.build:gradle:2.2.0' } task sourceJar(type: Jar) { from sourceSets.main.allSource } +processResources { + eachFile { + if (it.name == "org.jetbrains.dokka-android.properties") { + it.filter { line -> + line.replace("", dokka_version) + } + } + } +} + shadowJar { baseName = 'dokka-android-gradle-plugin' classifier = '' diff --git a/runners/android-gradle-plugin/src/main/kotlin/main.kt b/runners/android-gradle-plugin/src/main/kotlin/main.kt deleted file mode 100644 index 11040b2a..00000000 --- a/runners/android-gradle-plugin/src/main/kotlin/main.kt +++ /dev/null @@ -1,86 +0,0 @@ -package org.jetbrains.dokka.gradle - -import com.android.build.gradle.AppExtension -import com.android.build.gradle.BasePlugin -import com.android.build.gradle.LibraryExtension -import com.android.build.gradle.api.BaseVariant -import com.android.build.gradle.internal.VariantManager -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.api.tasks.SourceSet -import java.io.File -import kotlin.reflect.jvm.isAccessible -import kotlin.reflect.memberProperties - -open class DokkaAndroidPlugin : Plugin { - val allVariantsClassPath = mutableSetOf() - - override fun apply(project: Project) { - project.tasks.create("dokka", DokkaAndroidTask::class.java).apply { - moduleName = project.name - outputDirectory = File(project.buildDir, "dokka").absolutePath - } - - if (project.hasAndroidPlugin()) { - project.afterEvaluate { - collectClasspath(project) - } - } - else { - project.plugins.whenPluginAdded { - if (project.hasAndroidPlugin()) { - collectClasspath(project) - } - } - } - } - - private fun collectClasspath(project: Project) { - val plugin = (project.plugins.findPlugin("android") - ?: project.plugins.findPlugin("android-library") - ?: project.plugins.findPlugin("com.android.test")) as BasePlugin - - val variantManagerProperty = plugin::class.memberProperties.find { it.name == "variantManager" }!! - variantManagerProperty.isAccessible = true - val variantManager = variantManagerProperty.get(plugin) as VariantManager - variantManager.variantDataList.flatMapTo(allVariantsClassPath) { it.variantConfiguration.compileClasspath } - } -} - -open class DokkaAndroidTask : DokkaTask() { - override val sdkProvider: SdkProvider? = AndroidSdkProvider(project) -} - -private fun Project.hasAndroidPlugin() = plugins.hasPlugin("com.android.library") || plugins.hasPlugin("com.android.application") - -private fun Project.findDokkaAndroidPlugin() = plugins.findPlugin(DokkaAndroidPlugin::class.java) - -private fun Project.collectAllVariants(): Collection { - extensions.findByType(LibraryExtension::class.java)?.let { - return it.libraryVariants - } - extensions.findByType(AppExtension::class.java)?.let { - return it.applicationVariants - } - return emptyList() -} - -private class AndroidSdkProvider(private val project: Project) : SdkProvider { - private val ext by lazy { - project.extensions.findByType(LibraryExtension::class.java) ?: project.extensions.findByType(AppExtension::class.java) - } - - override val name: String = "android" - - override val isValid: Boolean - get() = project.hasAndroidPlugin() - - override val classpath: List - get() = ext.bootClasspath + (project.findDokkaAndroidPlugin()?.allVariantsClassPath ?: emptyList()) - - override val sourceDirs: Set? - get() { - val sourceSet = ext?.sourceSets?.findByName(SourceSet.MAIN_SOURCE_SET_NAME) - return sourceSet?.java?.srcDirs - } -} diff --git a/runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt b/runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt new file mode 100644 index 00000000..5ca3066a --- /dev/null +++ b/runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt @@ -0,0 +1,88 @@ +package org.jetbrains.dokka.gradle + +import com.android.build.gradle.AppExtension +import com.android.build.gradle.BasePlugin +import com.android.build.gradle.LibraryExtension +import com.android.build.gradle.api.BaseVariant +import com.android.build.gradle.internal.VariantManager +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.tasks.SourceSet +import java.io.File +import kotlin.reflect.jvm.isAccessible +import kotlin.reflect.memberProperties + +open class DokkaAndroidPlugin : Plugin { + val allVariantsClassPath = mutableSetOf() + + override fun apply(project: Project) { + + DokkaVersion.loadFrom(javaClass.getResourceAsStream("/META-INF/gradle-plugins/org.jetbrains.dokka-android.properties")) + project.tasks.create("dokka", DokkaAndroidTask::class.java).apply { + moduleName = project.name + outputDirectory = File(project.buildDir, "dokka").absolutePath + } + + if (project.hasAndroidPlugin()) { + project.afterEvaluate { + collectClasspath(project) + } + } + else { + project.plugins.whenPluginAdded { + if (project.hasAndroidPlugin()) { + collectClasspath(project) + } + } + } + } + + private fun collectClasspath(project: Project) { + val plugin = (project.plugins.findPlugin("android") + ?: project.plugins.findPlugin("android-library") + ?: project.plugins.findPlugin("com.android.test")) as BasePlugin + + val variantManagerProperty = plugin::class.memberProperties.find { it.name == "variantManager" }!! + variantManagerProperty.isAccessible = true + val variantManager = variantManagerProperty.get(plugin) as VariantManager + variantManager.variantDataList.flatMapTo(allVariantsClassPath) { it.variantConfiguration.compileClasspath } + } +} + +open class DokkaAndroidTask : DokkaTask() { + override val sdkProvider: SdkProvider? = AndroidSdkProvider(project) +} + +private fun Project.hasAndroidPlugin() = plugins.hasPlugin("com.android.library") || plugins.hasPlugin("com.android.application") + +private fun Project.findDokkaAndroidPlugin() = plugins.findPlugin(DokkaAndroidPlugin::class.java) + +private fun Project.collectAllVariants(): Collection { + extensions.findByType(LibraryExtension::class.java)?.let { + return it.libraryVariants + } + extensions.findByType(AppExtension::class.java)?.let { + return it.applicationVariants + } + return emptyList() +} + +private class AndroidSdkProvider(private val project: Project) : SdkProvider { + private val ext by lazy { + project.extensions.findByType(LibraryExtension::class.java) ?: project.extensions.findByType(AppExtension::class.java) + } + + override val name: String = "android" + + override val isValid: Boolean + get() = project.hasAndroidPlugin() + + override val classpath: List + get() = ext.bootClasspath + (project.findDokkaAndroidPlugin()?.allVariantsClassPath ?: emptyList()) + + override val sourceDirs: Set? + get() { + val sourceSet = ext?.sourceSets?.findByName(SourceSet.MAIN_SOURCE_SET_NAME) + return sourceSet?.java?.srcDirs + } +} diff --git a/runners/android-gradle-plugin/src/main/resources/META-INF/gradle-plugins/org.jetbrains.dokka-android.properties b/runners/android-gradle-plugin/src/main/resources/META-INF/gradle-plugins/org.jetbrains.dokka-android.properties index 03b28d93..b204da7b 100644 --- a/runners/android-gradle-plugin/src/main/resources/META-INF/gradle-plugins/org.jetbrains.dokka-android.properties +++ b/runners/android-gradle-plugin/src/main/resources/META-INF/gradle-plugins/org.jetbrains.dokka-android.properties @@ -1 +1,2 @@ -implementation-class=org.jetbrains.dokka.gradle.DokkaAndroidPlugin \ No newline at end of file +implementation-class=org.jetbrains.dokka.gradle.DokkaAndroidPlugin +dokka-version= \ No newline at end of file diff --git a/runners/gradle-plugin/src/main/kotlin/main.kt b/runners/gradle-plugin/src/main/kotlin/main.kt index f2800335..dbb6efce 100644 --- a/runners/gradle-plugin/src/main/kotlin/main.kt +++ b/runners/gradle-plugin/src/main/kotlin/main.kt @@ -11,7 +11,9 @@ import org.gradle.api.tasks.* import org.jetbrains.dokka.DokkaBootstrap import org.jetbrains.dokka.automagicTypedProxy import org.jetbrains.dokka.gradle.ClassloaderContainer.fatJarClassLoader +import org.jetbrains.dokka.gradle.DokkaVersion.version import java.io.File +import java.io.InputStream import java.io.Serializable import java.net.URLClassLoader import java.util.* @@ -19,13 +21,8 @@ import java.util.function.BiConsumer open class DokkaPlugin : Plugin { - val properties = Properties() - - init { - properties.load(javaClass.getResourceAsStream("/META-INF/gradle-plugins/org.jetbrains.dokka.properties")) - } override fun apply(project: Project) { - version = properties.getProperty("dokka-version") + DokkaVersion.loadFrom(javaClass.getResourceAsStream("/META-INF/gradle-plugins/org.jetbrains.dokka.properties")) project.tasks.create("dokka", DokkaTask::class.java).apply { moduleName = project.name outputDirectory = File(project.buildDir, "dokka").absolutePath @@ -33,7 +30,16 @@ open class DokkaPlugin : Plugin { } } -var version: String? = null +object DokkaVersion { + var version: String? = null + + fun loadFrom(stream: InputStream) { + version = Properties().apply { + load(stream) + }.getProperty("dokka-version") + } +} + object ClassloaderContainer { @JvmField @@ -89,8 +95,10 @@ open class DokkaTask : DefaultTask() { val fatjar = if (dokkaFatJar is File) dokkaFatJar as File else { - val dependency = project.buildscript.dependencies.create(dokkaFatJar) - val configuration = project.buildscript.configurations.detachedConfiguration(dependency) + //Searching for buildscript where dependency to dokka plugin was defined + val myBuildScript = project.allprojects.map { it.buildscript }.find { it.classLoader == javaClass.classLoader }!! + val dependency = myBuildScript.dependencies.create(dokkaFatJar) + val configuration = myBuildScript.configurations.detachedConfiguration(dependency) configuration.description = "Dokka main jar" configuration.resolve().first() } -- cgit