Skip to content

Commit 59286c6

Browse files
authored
Move external documentation links to a task input in Dackka (#4033)
* Move external links in dackka to an input This will allow the package-list files to be cached, and monitored for updates. * Maybe fixed merge conflict * Fixed formatting * Removed duplicate configurations
1 parent f721dbc commit 59286c6

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

buildSrc/src/main/java/com/google/firebase/gradle/plugins/DackkaGenerationTask.kt

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import org.gradle.api.DefaultTask
66
import org.gradle.api.provider.ListProperty
77
import org.gradle.api.provider.Property
88
import org.gradle.api.provider.SetProperty
9-
import org.gradle.api.tasks.Input
9+
import org.gradle.api.tasks.CacheableTask
10+
import org.gradle.api.tasks.Classpath
1011
import org.gradle.api.tasks.InputFile
1112
import org.gradle.api.tasks.InputFiles
1213
import org.gradle.api.tasks.OutputDirectory
14+
import org.gradle.api.tasks.PathSensitive
15+
import org.gradle.api.tasks.PathSensitivity
1316
import org.gradle.api.tasks.TaskAction
1417
import org.gradle.process.ExecOperations
1518
import org.gradle.workers.WorkAction
@@ -29,26 +32,48 @@ import org.json.JSONObject
2932
* @property suppressedFiles a list of files to exclude from documentation
3033
* @property outputDirectory where to store the generated files
3134
*/
35+
@CacheableTask
3236
abstract class GenerateDocumentationTaskExtension : DefaultTask() {
33-
@get:InputFile
37+
@get:[InputFile Classpath]
3438
abstract val dackkaJarFile: Property<File>
3539

36-
@get:Input
40+
@get:[InputFiles Classpath]
3741
abstract val dependencies: ListProperty<File>
3842

3943
@get:InputFiles
44+
@get:PathSensitive(PathSensitivity.RELATIVE)
4045
abstract val kotlinSources: ListProperty<File>
4146

4247
@get:InputFiles
48+
@get:PathSensitive(PathSensitivity.RELATIVE)
4349
abstract val javaSources: ListProperty<File>
4450

4551
@get:InputFiles
52+
@get:PathSensitive(PathSensitivity.RELATIVE)
4653
abstract val suppressedFiles: ListProperty<File>
4754

55+
@get:InputFiles
56+
@get:PathSensitive(PathSensitivity.RELATIVE)
57+
abstract val packageListFiles: ListProperty<File>
58+
4859
@get:OutputDirectory
4960
abstract val outputDirectory: Property<File>
5061
}
5162

63+
/**
64+
* Wrapper data class for External package-lists in Dokka
65+
*
66+
* This class allows us to map package-lists in a type-safe way, versus inline straight to
67+
* a map. This extra step could be removed- but it could also catch bugs in the future.
68+
*
69+
* @property packageList the prepared package-list file to map against
70+
* @property externalLink the url to map with when generating the docs
71+
*/
72+
data class ExternalDocumentationLink(
73+
val packageList: File,
74+
val externalLink: String
75+
)
76+
5277
/**
5378
* Task to run Dackka on a project.
5479
*
@@ -68,14 +93,6 @@ abstract class GenerateDocumentationTask @Inject constructor(
6893
}
6994

7095
private fun constructArguments(): JSONObject {
71-
// TODO(b/243675474): Move these to a task input for caching purposes
72-
val linksMap = mapOf(
73-
"android" to "https://developer.android.com/reference/kotlin/",
74-
"google" to "https://developer.android.com/reference/",
75-
"firebase" to "https://firebase.google.com/docs/reference/kotlin/",
76-
"coroutines" to "https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/"
77-
)
78-
7996
val jsonMap = mapOf(
8097
"moduleName" to "",
8198
"outputDir" to outputDirectory.get().path,
@@ -90,9 +107,9 @@ abstract class GenerateDocumentationTask @Inject constructor(
90107
"documentedVisibilities" to listOf("PUBLIC", "PROTECTED"),
91108
"skipEmptyPackages" to "true",
92109
"suppressedFiles" to suppressedFiles.get().map { it.path },
93-
"externalDocumentationLinks" to linksMap.map { (name, url) -> mapOf(
94-
"url" to url,
95-
"packageListUrl" to "file://${project.rootDir.absolutePath}/kotlindoc/package-lists/$name/package-list"
110+
"externalDocumentationLinks" to createExternalLinks(packageListFiles).map { mapOf(
111+
"url" to it.externalLink,
112+
"packageListUrl" to it.packageList.toURI()
96113
) }
97114
)),
98115
"offlineMode" to "true",
@@ -102,6 +119,20 @@ abstract class GenerateDocumentationTask @Inject constructor(
102119
return JSONObject(jsonMap)
103120
}
104121

122+
private fun createExternalLinks(packageLists: ListProperty<File>): List<ExternalDocumentationLink> {
123+
val linksMap = mapOf(
124+
"android" to "https://developer.android.com/reference/kotlin/",
125+
"google" to "https://developer.android.com/reference/",
126+
"firebase" to "https://firebase.google.com/docs/reference/kotlin/",
127+
"coroutines" to "https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/"
128+
)
129+
130+
return packageLists.get().map {
131+
val externalLink = linksMap[it.parentFile.nameWithoutExtension] ?: throw RuntimeException("Unexpected package-list found: ${it.name}")
132+
ExternalDocumentationLink(it, externalLink)
133+
}
134+
}
135+
105136
private fun saveToJsonFile(jsonObject: JSONObject): File {
106137
val outputFile = File.createTempFile("dackkaArgs", ".json")
107138

buildSrc/src/main/java/com/google/firebase/gradle/plugins/DackkaPlugin.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,16 @@ abstract class DackkaPlugin : Plugin<Project> {
8888
}
8989

9090
docsTask.configure {
91+
// this will become useful with the agp upgrade, as they're separate in 7.x+
9192
val sourcesForKotlin = emptyList<File>()
93+
val packageLists = fetchPackageLists(project)
9294

9395
val excludedFiles = if (!isKotlin) projectSpecificSuppressedFiles(project) else emptyList()
9496
val fixedJavaSources = if (!isKotlin) listOf(project.docStubs) else sourcesForJava
9597

9698
javaSources.set(fixedJavaSources)
9799
suppressedFiles.set(excludedFiles)
100+
packageListFiles.set(packageLists)
98101

99102
kotlinSources.set(sourcesForKotlin)
100103
dependencies.set(classpath)
@@ -107,6 +110,11 @@ abstract class DackkaPlugin : Plugin<Project> {
107110
return docsTask
108111
}
109112

113+
private fun fetchPackageLists(project: Project) =
114+
project.rootProject.fileTree("kotlindoc/package-lists").matching {
115+
include("**/package-list")
116+
}.toList()
117+
110118
// TODO(b/243534168): Remove when fixed
111119
private fun projectSpecificSuppressedFiles(project: Project): List<File> =
112120
when (project.name) {
@@ -129,12 +137,14 @@ abstract class DackkaPlugin : Plugin<Project> {
129137
outputDirectory.set(dackkaOutputDirectory)
130138
}
131139

140+
// TODO(b/243833009): Make task cacheable
132141
private fun registerFiresiteTransformTask(project: Project, outputDirectory: Provider<File>) =
133142
project.tasks.register<FiresiteTransformTask>("firesiteTransform") {
134143
dackkaFiles.set(outputDirectory)
135144
}
136145

137146
// If we decide to publish java variants, we'll need to address the generated format as well
147+
// TODO(b/243833009): Make task cacheable
138148
private fun registerDeleteDackkaGeneratedJavaReferencesTask(project: Project, outputDirectory: Provider<File>) =
139149
project.tasks.register<Delete>("deleteDackkaGeneratedJavaReferences") {
140150
mustRunAfter("generateDackkaDocumentation")

0 commit comments

Comments
 (0)