From c58d35288b1dd94a94320642439fd9a243f70a2b Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 23 Sep 2024 15:45:02 +0300 Subject: [PATCH] Added some codegen features --- .github/summary.yaml | 1 + build.gradle | 4 ++ .../kotatsu/parsers/ksp/ParserProcessor.kt | 24 +++++++---- .../parsers/util/IntentFilterGenerator.kt | 40 +++++++++++++++++++ 4 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 .github/summary.yaml create mode 100644 src/test/kotlin/org/koitharu/kotatsu/parsers/util/IntentFilterGenerator.kt diff --git a/.github/summary.yaml b/.github/summary.yaml new file mode 100644 index 00000000..85375952 --- /dev/null +++ b/.github/summary.yaml @@ -0,0 +1 @@ +total: 1110 \ No newline at end of file diff --git a/build.gradle b/build.gradle index 08269bc5..23329e30 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,10 @@ test { useJUnitPlatform() } +ksp { + arg("summaryOutputDir", "${projectDir}/.github") +} + compileKotlin { kotlinOptions { freeCompilerArgs += [ diff --git a/kotatsu-parsers-ksp/src/main/kotlin/org/koitharu/kotatsu/parsers/ksp/ParserProcessor.kt b/kotatsu-parsers-ksp/src/main/kotlin/org/koitharu/kotatsu/parsers/ksp/ParserProcessor.kt index f64f9c5d..4e05b6c0 100644 --- a/kotatsu-parsers-ksp/src/main/kotlin/org/koitharu/kotatsu/parsers/ksp/ParserProcessor.kt +++ b/kotatsu-parsers-ksp/src/main/kotlin/org/koitharu/kotatsu/parsers/ksp/ParserProcessor.kt @@ -7,8 +7,10 @@ import com.google.devtools.ksp.symbol.KSAnnotated import com.google.devtools.ksp.symbol.KSClassDeclaration import com.google.devtools.ksp.symbol.KSVisitorVoid import com.google.devtools.ksp.validate +import java.io.File import java.io.Writer import java.util.* +import kotlin.math.log class ParserProcessor( private val codeGenerator: CodeGenerator, @@ -19,9 +21,7 @@ class ParserProcessor( private val sourceNamePattern = Regex("[A-Z_][A-Z0-9_]{3,}") override fun process(resolver: Resolver): List { - val symbols = - resolver - .getSymbolsWithAnnotation("org.koitharu.kotatsu.parsers.MangaSourceParser") + val symbols = resolver.getSymbolsWithAnnotation("org.koitharu.kotatsu.parsers.MangaSourceParser") val ret = symbols.filterNot { it.validate() }.toList() if (!symbols.iterator().hasNext()) { return ret @@ -49,11 +49,12 @@ class ParserProcessor( logger.warn(e.toString(), null) null } - sourcesFile?.writer().use { sourcesWriter -> + val totalCount = sourcesFile?.writer().use { sourcesWriter -> factoryFile?.writer().use { factoryWriter -> writeContent(sourcesWriter, factoryWriter, symbols) } } + writeSummary(totalCount) return ret } @@ -61,9 +62,9 @@ class ParserProcessor( sourcesWriter: Writer?, factoryWriter: Writer?, symbols: Sequence, - ) { + ): Int { if (sourcesWriter == null && factoryWriter == null) { - return + return 0 } factoryWriter?.write( """ @@ -91,9 +92,10 @@ class ParserProcessor( ) val visitor = ParserVisitor(sourcesWriter, factoryWriter) - symbols + val totalCount = symbols .filter { it is KSClassDeclaration && it.validate() } - .forEach { it.accept(visitor, Unit) } + .onEach { it.accept(visitor, Unit) } + .count() factoryWriter?.write( """ @@ -112,6 +114,12 @@ class ParserProcessor( } """.trimIndent(), ) + return totalCount + } + + private fun writeSummary(totalCount: Int) { + val file = File(options["summaryOutputDir"] ?: return, "summary.yaml") + file.writeText("total: $totalCount") } private inner class ParserVisitor( diff --git a/src/test/kotlin/org/koitharu/kotatsu/parsers/util/IntentFilterGenerator.kt b/src/test/kotlin/org/koitharu/kotatsu/parsers/util/IntentFilterGenerator.kt new file mode 100644 index 00000000..6b997776 --- /dev/null +++ b/src/test/kotlin/org/koitharu/kotatsu/parsers/util/IntentFilterGenerator.kt @@ -0,0 +1,40 @@ +package org.koitharu.kotatsu.parsers.util + +import org.junit.jupiter.api.Test +import org.koitharu.kotatsu.parsers.MangaLoaderContextMock +import org.koitharu.kotatsu.parsers.model.MangaParserSource +import org.koitharu.kotatsu.parsers.newParser +import java.io.File + +class IntentFilterGenerator { + + @Test + fun generateIntentFilter() { + val output = File("out/test/resources/intent-filter.xml") + output.printWriter(Charsets.UTF_8).use { writer -> + writer.appendLine("") + writer.appendTab().appendLine("") + writer.appendLine() + writer.appendTab().appendLine("") + writer.appendTab().appendLine("") + writer.appendLine() + writer.appendTab().appendLine("") + writer.appendTab().appendLine("") + writer.appendLine() + for (source in MangaParserSource.entries) { + if (source == MangaParserSource.DUMMY) { + continue + } + val parser = source.newParser(MangaLoaderContextMock) + parser.configKeyDomain.presetValues.forEach { domain -> + writer.appendTab().append("") + } + } + writer.appendLine() + writer.appendLine("") + } + println(output.absolutePath) + } + + private fun Appendable.appendTab() = append('\t') +}