aboutsummaryrefslogtreecommitdiff
path: root/ant/src/dokka.kt
blob: 6538f0831188af97052acc7dcfb533b8b269f0f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.jetbrains.dokka.ant

import org.apache.tools.ant.Task
import org.apache.tools.ant.types.Path
import org.apache.tools.ant.types.Reference
import org.apache.tools.ant.BuildException
import org.apache.tools.ant.Project
import org.jetbrains.dokka.DokkaLogger
import org.jetbrains.dokka.DokkaGenerator
import org.jetbrains.dokka.SourceLinkDefinition
import java.io.File

class AntLogger(val task: Task): DokkaLogger {
    override fun info(message: String) = task.log(message, Project.MSG_INFO)
    override fun warn(message: String) = task.log(message, Project.MSG_WARN)
    override fun error(message: String) = task.log(message, Project.MSG_ERR)
}

class AntSourceLinkDefinition(var path: String? = null, var url: String? = null, var lineSuffix: String? = null)

class DokkaAntTask(): Task() {
    public var moduleName: String? = null
    public var outputDir: String? = null
    public var outputFormat: String = "html"

    public var skipDeprecated: Boolean = false

    public val compileClasspath: Path = Path(getProject())
    public val sourcePath: Path = Path(getProject())
    public val samplesPath: Path = Path(getProject())
    public val includesPath: Path = Path(getProject())

    public val antSourceLinks: MutableList<AntSourceLinkDefinition> = arrayListOf()

    public fun setClasspath(classpath: Path) {
        compileClasspath.append(classpath)
    }

    public fun setClasspathRef(ref: Reference) {
        compileClasspath.createPath().refid = ref
    }

    public fun setSrc(src: Path) {
        sourcePath.append(src)
    }

    public fun setSrcRef(ref: Reference) {
        sourcePath.createPath().refid = ref
    }

    public fun setSamples(samples: Path) {
        samplesPath.append(samples)
    }

    public fun setSamplesRef(ref: Reference) {
        samplesPath.createPath().refid = ref
    }

    public fun setInclude(include: Path) {
        includesPath.append(include)
    }

    public fun createSourceLink(): AntSourceLinkDefinition {
        val def = AntSourceLinkDefinition()
        antSourceLinks.add(def)
        return def
    }

    override fun execute() {
        if (sourcePath.list().size() == 0) {
            throw BuildException("At least one source path needs to be specified")
        }
        if (moduleName == null) {
            throw BuildException("Module name needs to be specified")
        }
        if (outputDir == null) {
            throw BuildException("Output directory needs to be specified")
        }
        val sourceLinks = antSourceLinks.map {
            val path = it.path
            if (path == null) {
                throw BuildException("Path attribute of a <sourceLink> element is required")
            }
            val url = it.url
            if (url == null) {
                throw BuildException("Path attribute of a <sourceLink> element is required")
            }
            SourceLinkDefinition(File(path).canonicalFile.absolutePath, url, it.lineSuffix)
        }

        val url = DokkaAntTask::class.java.getResource("/org/jetbrains/dokka/ant/DokkaAntTask.class")
        val jarRoot = url.path.substringBefore("!/").removePrefix("file:")

        val generator = DokkaGenerator(
                AntLogger(this),
                listOf(jarRoot) + compileClasspath.list().toList(),
                sourcePath.list().toList(),
                samplesPath.list().toList(),
                includesPath.list().toList(),
                moduleName!!,
                outputDir!!,
                outputFormat,
                sourceLinks,
                skipDeprecated
        )
        generator.generate()
    }
}