|
| 1 | +package io.codemodder.docs |
| 2 | + |
| 3 | +import com.github.javaparser.JavaParser |
| 4 | +import com.google.gson.Gson |
| 5 | +import org.gradle.api.DefaultTask |
| 6 | +import org.gradle.api.file.* |
| 7 | +import org.gradle.api.tasks.Input |
| 8 | +import org.gradle.api.tasks.InputDirectory |
| 9 | +import org.gradle.api.tasks.InputFile |
| 10 | +import org.gradle.api.tasks.InputFiles |
| 11 | +import org.gradle.api.tasks.Internal |
| 12 | +import org.gradle.api.tasks.TaskAction |
| 13 | +import org.gradle.api.tasks.options.Option |
| 14 | +import org.gradle.internal.enterprise.test.FileProperty |
| 15 | +import java.util.stream.Collectors |
| 16 | +import javax.inject.Inject |
| 17 | + |
| 18 | +/** |
| 19 | + * A task to generate documentation for the codemodder plugin |
| 20 | + * |
| 21 | + * This task requires the user to specify command line flag `codemodDocsDir` to specify the directory to write the docs |
| 22 | + */ |
| 23 | +abstract class GenerateDocsTask : DefaultTask() { |
| 24 | + |
| 25 | + @get:Internal |
| 26 | + abstract val codemodDocsDir: DirectoryProperty |
| 27 | + |
| 28 | + @get:InputFiles |
| 29 | + abstract val javaMainSources: ConfigurableFileCollection |
| 30 | + |
| 31 | + @get:InputFile |
| 32 | + abstract val defaultCodemodSource: RegularFileProperty |
| 33 | + |
| 34 | + @Option(option = "codemodDocsDir", description = "The dir to write the docs to") |
| 35 | + fun setCodemodderDocsDir(value: String) { |
| 36 | + codemodDocsDir.set(project.file(value)) |
| 37 | + } |
| 38 | + |
| 39 | + @get:Inject |
| 40 | + abstract val projectLayout: ProjectLayout |
| 41 | + |
| 42 | + @TaskAction |
| 43 | + fun generateDocs() { |
| 44 | + val codemodDocsDir = this.codemodDocsDir.get().asFile |
| 45 | + if (!codemodDocsDir.exists()) { |
| 46 | + throw IllegalArgumentException("codemodDocsDir does not exist") |
| 47 | + } |
| 48 | + |
| 49 | + if (codemodDocsDir.list().isEmpty()) { |
| 50 | + println("Docs directory was empty") |
| 51 | + } |
| 52 | + |
| 53 | + val defaultCodemodSource = projectLayout.projectDirectory.file("src/main/java/io/codemodder/codemods/DefaultCodemods.java").asFile.readText() |
| 54 | + |
| 55 | + |
| 56 | + for (javaMainSource in javaMainSources) { |
| 57 | + val codemodFiles = javaMainSource.walkTopDown().filter { it.name.endsWith("Codemod.java") } |
| 58 | + for (codemodFile in codemodFiles) { |
| 59 | + val codemodName = codemodFile.nameWithoutExtension |
| 60 | + |
| 61 | + // check if it's in the DefaultCodemods list -- we don't do docs for the others |
| 62 | + if (!defaultCodemodSource.contains(codemodName)) { |
| 63 | + println("Skipping $codemodName") |
| 64 | + continue |
| 65 | + } else { |
| 66 | + println("Processing $codemodName") |
| 67 | + } |
| 68 | + val parsedJavaFile = JavaParser().parse(codemodFile).result |
| 69 | + val codemodClass = parsedJavaFile.get().types[0] |
| 70 | + |
| 71 | + // get the codemod annotation which has some metadata |
| 72 | + val codemodAnnotation = codemodClass.annotations |
| 73 | + .stream() |
| 74 | + .filter { it.nameAsString == "Codemod" } |
| 75 | + .findFirst() |
| 76 | + .get() |
| 77 | + .asNormalAnnotationExpr() |
| 78 | + |
| 79 | + // get the name and other metadata |
| 80 | + val annotationParameters = codemodAnnotation.pairs |
| 81 | + val id = annotationParameters.stream() |
| 82 | + .filter { it.nameAsString == "id" } |
| 83 | + .findFirst() |
| 84 | + .get() |
| 85 | + .value |
| 86 | + .asStringLiteralExpr() |
| 87 | + .value |
| 88 | + |
| 89 | + val fileName = id.replace(":", "_").replace("/", "_") + ".md" |
| 90 | + |
| 91 | + val importance = annotationParameters.stream() |
| 92 | + .filter { it.nameAsString == "importance" } |
| 93 | + .findFirst() |
| 94 | + .get() |
| 95 | + .value |
| 96 | + .asFieldAccessExpr() |
| 97 | + .nameAsString |
| 98 | + |
| 99 | + val mergeGuidance = annotationParameters.stream() |
| 100 | + .filter { it.nameAsString == "reviewGuidance" } |
| 101 | + .findFirst() |
| 102 | + .get() |
| 103 | + .value |
| 104 | + .asFieldAccessExpr() |
| 105 | + .toString() |
| 106 | + |
| 107 | + // the other metadata is in the resources |
| 108 | + val resourceDir = projectLayout.projectDirectory.dir("src/main/resources/io/codemodder/codemods/$codemodName") |
| 109 | + val description = resourceDir.file("description.md").asFile.readText() |
| 110 | + val reportJson = resourceDir.file("report.json").asFile.readText() |
| 111 | + val report = Gson().fromJson(reportJson, Map::class.java) |
| 112 | + |
| 113 | + val summary = report["summary"] as String |
| 114 | + |
| 115 | + // add the scanning tool to the summary |
| 116 | + var needsScanningTool = "No" |
| 117 | + if (id.startsWith("sonar")) { |
| 118 | + needsScanningTool = "Yes (Sonar)" |
| 119 | + } else if (id.startsWith("codeql")) { |
| 120 | + needsScanningTool = "Yes (CodeQL)" |
| 121 | + } else if (id.startsWith("semgrep")) { |
| 122 | + needsScanningTool = "Yes (Semgrep)" |
| 123 | + } |
| 124 | + |
| 125 | + // get the merge advice |
| 126 | + var mergeGuidanceStr = "Merge After Review" |
| 127 | + if (mergeGuidance == "ReviewGuidance.MERGE_WITHOUT_REVIEW") { |
| 128 | + mergeGuidanceStr = "Merge Without Review" |
| 129 | + } else if (mergeGuidance == "ReviewGuidance.MERGE_AFTER_CURSORY_REVIEW") { |
| 130 | + mergeGuidanceStr = "Merge After Cursory Review" |
| 131 | + } |
| 132 | + |
| 133 | + val mergeGuidanceJustification = report["reviewGuidanceIJustification"] |
| 134 | + |
| 135 | + val faqs = report["faqs"] |
| 136 | + |
| 137 | + val references = report["references"] as List<String> |
| 138 | + val referencesStr = references.stream().map { " * [$it]($it)" }.collect(Collectors.joining("\n")) |
| 139 | + |
| 140 | + val codemodDocsFile = projectLayout.projectDirectory.file("$codemodDocsDir/$fileName") |
| 141 | + |
| 142 | + // escape the quotes in summary |
| 143 | + val escapedSummary = summary.replace("\"", "\\\"") |
| 144 | + var doc = """ |
| 145 | + --- |
| 146 | + title: "$escapedSummary" |
| 147 | + sidebar_position: 1 |
| 148 | + --- |
| 149 | + |
| 150 | + ## $id |
| 151 | +
|
| 152 | + | Importance | Review Guidance | Requires Scanning Tool | |
| 153 | + |-------------|----------------------|------------------------| |
| 154 | + | $importance | $mergeGuidanceStr | $needsScanningTool | |
| 155 | + |
| 156 | + """.trimIndent() |
| 157 | + |
| 158 | + doc += "\n$description\n" |
| 159 | + |
| 160 | + val hasFaq = mergeGuidanceJustification != null || faqs != null |
| 161 | + |
| 162 | + if (hasFaq) { |
| 163 | + doc += "## F.A.Q.\n\n" |
| 164 | + } |
| 165 | + |
| 166 | + if (mergeGuidanceJustification != null) { |
| 167 | + doc += "### Why is this codemod marked as $mergeGuidanceStr?\n\n" |
| 168 | + doc += "$mergeGuidanceJustification\n\n" |
| 169 | + } |
| 170 | + |
| 171 | + if (faqs != null) { |
| 172 | + faqs as List<Map<String, String>> |
| 173 | + for (faq in faqs) { |
| 174 | + doc += """ |
| 175 | + ### ${faq["question"]} |
| 176 | + |
| 177 | + ${faq["answer"]} |
| 178 | + |
| 179 | + """.trimIndent() |
| 180 | + doc += "\n" |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + doc += "\n## References\n" |
| 185 | + doc += referencesStr |
| 186 | + doc += "\n" // file editors like a trailing newline |
| 187 | + codemodDocsFile.asFile.writeText(doc) |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | +} |
0 commit comments