diff options
6 files changed, 192 insertions, 12 deletions
diff --git a/dokka-android-gradle-plugin/build.gradle b/dokka-android-gradle-plugin/build.gradle new file mode 100644 index 00000000..4e4a204f --- /dev/null +++ b/dokka-android-gradle-plugin/build.gradle @@ -0,0 +1,86 @@ +group 'org.jetbrains.dokka' +version dokka_version + +buildscript { + repositories { + mavenCentral() + jcenter() + maven { + url "https://dl.bintray.com/kotlin/kotlin-eap" + } + } + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4' + } +} + +apply plugin: 'java' +apply plugin: 'kotlin' +apply plugin: 'maven-publish' +apply plugin: 'com.jfrog.bintray' + +sourceCompatibility = 1.6 + +repositories { + mavenCentral() + jcenter() + maven { + url "https://dl.bintray.com/kotlin/kotlin-eap" + } +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' + compile project(':core') + compile project(':dokka-gradle-plugin') + + compile gradleApi() + compile localGroovy() + + compile 'com.android.tools.build:gradle:2.0.0-beta7' +} + +sourceSets { + main.java.srcDirs += 'src/main/kotlin' +} + +task sourceJar(type: Jar) { + from sourceSets.main.allSource +} + +task wrapper(type: Wrapper) { + gradleVersion = '2.5' + distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip" +} + +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + + artifact sourceJar { + classifier "sources" + } + } + } +} + +bintray { + user = System.getenv('BINTRAY_USER') + key = System.getenv('BINTRAY_KEY') + + pkg { + repo = dokka_eap.toBoolean() ? 'kotlin-eap' : 'dokka' + name = 'dokka' + userOrg = 'kotlin' + desc = 'Dokka, the Kotlin documentation tool' + vcsUrl = 'https://github.com/kotlin/dokka.git' + licenses = ['Apache-2.0'] + version { + name = dokka_version + } + } + + publications = ['mavenJava'] +} diff --git a/dokka-android-gradle-plugin/settings.gradle b/dokka-android-gradle-plugin/settings.gradle new file mode 100644 index 00000000..08876dc5 --- /dev/null +++ b/dokka-android-gradle-plugin/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'dokka-android-gradle-plugin' + diff --git a/dokka-android-gradle-plugin/src/main/kotlin/main.kt b/dokka-android-gradle-plugin/src/main/kotlin/main.kt new file mode 100644 index 00000000..9a92e2a2 --- /dev/null +++ b/dokka-android-gradle-plugin/src/main/kotlin/main.kt @@ -0,0 +1,38 @@ +package org.jetbrains.dokka.gradle + +import com.android.build.gradle.LibraryExtension +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.tasks.SourceSet +import java.io.File + +open class DokkaAndroidPlugin : Plugin<Project> { + override fun apply(project: Project) { + project.tasks.create("dokka", DokkaAndroidTask::class.java).apply { + moduleName = project.name + outputDirectory = File(project.buildDir, "dokka").absolutePath + } + } +} + +open class DokkaAndroidTask : DokkaTask() { + override val sdkProvider: SdkProvider? = AndroidSdkProvider(project) +} + +private class AndroidSdkProvider(private val project: Project) : SdkProvider { + private val ext by lazy { project.extensions.getByType(LibraryExtension::class.java) } + + override val name: String = "android" + + override val isValid: Boolean + get() = project.plugins.hasPlugin("com.android.library") + + override val classpath: List<File> + get() = ext.bootClasspath + + override val sourceDirs: Set<File>? + get() { + val sourceSet = ext.sourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME) + return sourceSet?.java?.srcDirs + } +} diff --git a/dokka-android-gradle-plugin/src/main/resources/META-INF/gradle-plugins/org.jetbrains.dokka-android.properties b/dokka-android-gradle-plugin/src/main/resources/META-INF/gradle-plugins/org.jetbrains.dokka-android.properties new file mode 100644 index 00000000..03b28d93 --- /dev/null +++ b/dokka-android-gradle-plugin/src/main/resources/META-INF/gradle-plugins/org.jetbrains.dokka-android.properties @@ -0,0 +1 @@ +implementation-class=org.jetbrains.dokka.gradle.DokkaAndroidPlugin
\ No newline at end of file diff --git a/dokka-gradle-plugin/src/main/kotlin/main.kt b/dokka-gradle-plugin/src/main/kotlin/main.kt index 476f7656..e3275501 100644 --- a/dokka-gradle-plugin/src/main/kotlin/main.kt +++ b/dokka-gradle-plugin/src/main/kotlin/main.kt @@ -44,6 +44,10 @@ open class DokkaTask : DefaultTask() { var samples: List<Any?> = arrayListOf() @Input var jdkVersion: Int = 6 + @Input + var sourceDirs: Iterable<File> = emptyList() + + protected open val sdkProvider: SdkProvider? = null fun linkMapping(closure: Closure<Any?>) { val mapping = LinkMapping() @@ -63,13 +67,15 @@ open class DokkaTask : DefaultTask() { @TaskAction fun generate() { val project = project + val sdkProvider = sdkProvider val sourceDirectories = getSourceDirectories() val allConfigurations = project.configurations val classpath = - processConfigurations - .map { allConfigurations?.getByName(it.toString()) ?: throw IllegalArgumentException("No configuration $it found") } - .flatMap { it } + if (sdkProvider != null && sdkProvider.isValid) sdkProvider.classpath else emptyList<File>() + + processConfigurations + .map { allConfigurations?.getByName(it.toString()) ?: throw IllegalArgumentException("No configuration $it found") } + .flatMap { it } if (sourceDirectories.isEmpty()) { logger.warn("No source directories found: skipping dokka generation") @@ -89,20 +95,33 @@ open class DokkaTask : DefaultTask() { ).generate() } - fun getSourceDirectories(): List<File> { - val javaPluginConvention = project.convention.getPlugin(JavaPluginConvention::class.java) - val sourceSets = javaPluginConvention.sourceSets?.findByName(SourceSet.MAIN_SOURCE_SET_NAME) - return sourceSets?.allSource?.srcDirs?.filter { it.exists() } ?: emptyList() + fun getSourceDirectories(): Collection<File> { + val provider = sdkProvider + val sourceDirs = if (sourceDirs.any()) { + logger.info("Dokka: Taking source directories provided by the user") + sourceDirs.toSet() + } else if (provider != null && provider.isValid) { + logger.info("Dokka: Taking source directories from ${provider.name} sdk provider") + provider.sourceDirs + } else { + logger.info("Dokka: Taking source directories from default java plugin") + val javaPluginConvention = project.convention.getPlugin(JavaPluginConvention::class.java) + val sourceSets = javaPluginConvention.sourceSets?.findByName(SourceSet.MAIN_SOURCE_SET_NAME) + sourceSets?.allSource?.srcDirs + } + + return sourceDirs?.filter { it.exists() } ?: emptyList() } + @Input @InputFiles @SkipWhenEmpty - fun getInputFiles() : FileCollection = project.files(getSourceDirectories().map { project.fileTree(it) }) + - project.files(includes) + - project.files(samples) + fun getInputFiles(): FileCollection = project.files(getSourceDirectories().map { project.fileTree(it) }) + + project.files(includes) + + project.files(samples) @OutputDirectory - fun getOutputDirectoryAsFile() : File = project.file(outputDirectory) + fun getOutputDirectoryAsFile(): File = project.file(outputDirectory) } @@ -111,3 +130,37 @@ open class LinkMapping { var url: String = "" var suffix: String? = null } + +/** + * A provider for SDKs that can be used if a project uses classes that live outside the JDK or uses a + * different method to determine the source directories. + * + * For example an Android library project configures its sources through the Android extension instead + * of the basic java convention. Also it has its custom classes located in the SDK installation directory. + */ +interface SdkProvider { + /** + * The name of this provider. Only used for logging purposes. + */ + val name: String + + /** + * Checks whether this provider has everything it needs to provide the source directories. + */ + val isValid: Boolean + + /** + * Provides additional classpath files where Dokka should search for external classes. + * The file list is injected **after** JDK Jars and **before** project dependencies. + * + * This is only called if [isValid] returns `true`. + */ + val classpath: List<File> + + /** + * Provides a list of directories where Dokka should search for source files. + * + * This is only called if [isValid] returns `true`. + */ + val sourceDirs: Set<File>? +} diff --git a/settings.gradle b/settings.gradle index f02d412e..5f59e822 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include 'core', 'dokka-gradle-plugin' +include 'core', 'dokka-gradle-plugin', 'dokka-android-gradle-plugin' |