1/*
2 * Copyright 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.build.docs
18
19import androidx.build.Version
20import androidx.build.doclava.DoclavaTask
21import java.io.File
22
23open class GenerateDocsTask : DoclavaTask() {
24
25    private data class Since(val path: String, val apiLevel: String)
26    private data class Artifact(val path: String, val artifact: String)
27
28    private val sinces = mutableListOf<Since>()
29    private val artifacts = mutableListOf<Artifact>()
30
31    fun addArtifactsAndSince() {
32        doFirst {
33            coreJavadocOptions {
34                if (sinces.isNotEmpty()) {
35                    addMultilineMultiValueOption("since").value = sinces.map { (path, apiLevel) ->
36                        listOf(path, apiLevel)
37                    }
38                }
39
40                if (artifacts.isNotEmpty()) {
41                    addMultilineMultiValueOption("artifact").value = artifacts.map { artifact ->
42                        listOf(artifact.path, artifact.artifact)
43                    }
44                }
45            }
46        }
47    }
48
49    fun addSinceFilesFrom(dir: File) {
50        File(dir, "api").listFiles().forEach { file ->
51            Version.parseOrNull(file)?.let { version ->
52                sinces.add(Since(file.absolutePath, version.toString()))
53            }
54        }
55    }
56
57    fun addArtifact(path: String, artifact: String) = artifacts.add(Artifact(path, artifact))
58}