1 /*
2  * Copyright 2023 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 
17 package androidx.build.dackka
18 
19 import com.google.gson.Gson
20 import com.google.gson.GsonBuilder
21 import com.google.gson.JsonElement
22 import com.google.gson.JsonPrimitive
23 import com.google.gson.JsonSerializationContext
24 import com.google.gson.JsonSerializer
25 import java.io.File
26 import java.lang.reflect.Type
27 import org.gradle.api.file.FileCollection
28 import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
29 import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
30 
31 internal object DokkaUtils {
32     /** Creates a GSON instance that can be used to serialize Dokka CLI json models. */
createGsonnull33     fun createGson(): Gson =
34         GsonBuilder()
35             .setPrettyPrinting()
36             .registerTypeAdapter(File::class.java, CanonicalFileSerializer())
37             .registerTypeAdapter(FileCollection::class.java, FileCollectionSerializer())
38             .create()
39 
40     /** Serializer for Gradle's [FileCollection] */
41     private class FileCollectionSerializer : JsonSerializer<FileCollection> {
42         override fun serialize(
43             src: FileCollection,
44             typeOfSrc: Type,
45             context: JsonSerializationContext
46         ): JsonElement {
47             return context.serialize(src.files)
48         }
49     }
50 
51     /**
52      * Serializer for [File] instances in the Dokka CLI model.
53      *
54      * Dokka doesn't work well with relative paths hence we use a canonical paths while setting up
55      * its parameters.
56      */
57     private class CanonicalFileSerializer : JsonSerializer<File> {
serializenull58         override fun serialize(
59             src: File,
60             typeOfSrc: Type,
61             context: JsonSerializationContext
62         ): JsonElement {
63             return JsonPrimitive(src.canonicalPath)
64         }
65     }
66 }
67 
68 enum class DokkaAnalysisPlatform(val jsonName: String) {
69     JVM("jvm"),
70     ANDROID("jvm"), // intentionally same as JVM as dokka only support jvm
71     JS("js"),
72     NATIVE("native"),
73     COMMON("common");
74 
androidOrJvmnull75     fun androidOrJvm() = this == JVM || this == ANDROID
76 }
77 
78 fun KotlinTarget.docsPlatform() =
79     when (platformType) {
80         KotlinPlatformType.common -> DokkaAnalysisPlatform.COMMON
81         KotlinPlatformType.jvm -> DokkaAnalysisPlatform.JVM
82         KotlinPlatformType.js -> DokkaAnalysisPlatform.JS
83         KotlinPlatformType.wasm -> DokkaAnalysisPlatform.JS
84         KotlinPlatformType.androidJvm -> DokkaAnalysisPlatform.ANDROID
85         KotlinPlatformType.native -> DokkaAnalysisPlatform.NATIVE
86     }
87