Copy README/CHANGELOG files for multisrc overrides correctly (#12051)

This commit is contained in:
arkon 2022-06-02 19:59:40 -04:00 committed by GitHub
parent d8f3813906
commit 6b8ebe1340
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,11 +40,12 @@ interface ThemeSourceGenerator {
companion object { companion object {
private fun pkgNameSuffix(source: ThemeSourceData, separator: String): String { private fun pkgNameSuffix(source: ThemeSourceData, separator: String): String {
return if (source is ThemeSourceData.SingleLang) return if (source is ThemeSourceData.SingleLang) {
listOf(source.lang.substringBefore("-"), source.pkgName).joinToString(separator) listOf(source.lang.substringBefore("-"), source.pkgName).joinToString(separator)
else } else {
listOf("all", source.pkgName).joinToString(separator) listOf("all", source.pkgName).joinToString(separator)
} }
}
private fun themeSuffix(themePkg: String, separator: String): String { private fun themeSuffix(themePkg: String, separator: String): String {
return listOf("eu", "kanade", "tachiyomi", "multisrc", themePkg).joinToString(separator) return listOf("eu", "kanade", "tachiyomi", "multisrc", themePkg).joinToString(separator)
@ -59,6 +60,7 @@ interface ThemeSourceGenerator {
"SOURCEHOST" to source.baseUrl.toHttpUrlOrNull()?.host, "SOURCEHOST" to source.baseUrl.toHttpUrlOrNull()?.host,
"SOURCESCHEME" to source.baseUrl.toHttpUrlOrNull()?.scheme "SOURCESCHEME" to source.baseUrl.toHttpUrlOrNull()?.scheme
).filter { it.value != null } ).filter { it.value != null }
gradle.writeText( gradle.writeText(
""" """
// THIS FILE IS AUTO-GENERATED; DO NOT EDIT // THIS FILE IS AUTO-GENERATED; DO NOT EDIT
@ -92,11 +94,11 @@ ${placeholders.map { "${" ".repeat(28)}${it.key}: \"${it.value}\""}.joinToString
private fun writeAndroidManifest(androidManifestFile: File, manifestOverridesPath: String, defaultAndroidManifestPath: String) { private fun writeAndroidManifest(androidManifestFile: File, manifestOverridesPath: String, defaultAndroidManifestPath: String) {
val androidManifestOverride = File(manifestOverridesPath) val androidManifestOverride = File(manifestOverridesPath)
val defaultAndroidManifest = File(defaultAndroidManifestPath) val defaultAndroidManifest = File(defaultAndroidManifestPath)
if (androidManifestOverride.exists()) if (androidManifestOverride.exists()) {
androidManifestOverride.copyTo(androidManifestFile) androidManifestOverride.copyTo(androidManifestFile)
else if (defaultAndroidManifest.exists()) } else if (defaultAndroidManifest.exists()) {
defaultAndroidManifest.copyTo(androidManifestFile) defaultAndroidManifest.copyTo(androidManifestFile)
else } else {
androidManifestFile.writeText( androidManifestFile.writeText(
""" """
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
@ -105,11 +107,13 @@ ${placeholders.map { "${" ".repeat(28)}${it.key}: \"${it.value}\""}.joinToString
""".trimIndent() """.trimIndent()
) )
} }
}
private fun createGradleProject(source: ThemeSourceData, themePkg: String, themeClass: String, baseVersionCode: Int, userDir: String) { private fun createGradleProject(source: ThemeSourceData, themePkg: String, themeClass: String, baseVersionCode: Int, userDir: String) {
// userDir = tachiyomi-extensions project root path
val projectRootPath = "$userDir/generated-src/${pkgNameSuffix(source, "/")}" val projectRootPath = "$userDir/generated-src/${pkgNameSuffix(source, "/")}"
val projectSrcPath = "$projectRootPath/src/eu/kanade/tachiyomi/extension/${pkgNameSuffix(source, "/")}" val projectSrcPath = "$projectRootPath/src/eu/kanade/tachiyomi/extension/${pkgNameSuffix(source, "/")}"
val overridesPath = "$userDir/multisrc/overrides/$themePkg/${source.pkgName}" // userDir = tachiyomi-extensions project root path val overridesPath = "$userDir/multisrc/overrides/$themePkg/${source.pkgName}"
val defaultResPath = "$userDir/multisrc/overrides/$themePkg/default/res" val defaultResPath = "$userDir/multisrc/overrides/$themePkg/default/res"
val defaultAndroidManifestPath = "$userDir/multisrc/overrides/$themePkg/default/AndroidManifest.xml" val defaultAndroidManifestPath = "$userDir/multisrc/overrides/$themePkg/default/AndroidManifest.xml"
val defaultAdditionalGradlePath = "$userDir/multisrc/overrides/$themePkg/default/additional.gradle.kts" val defaultAdditionalGradlePath = "$userDir/multisrc/overrides/$themePkg/default/additional.gradle.kts"
@ -121,7 +125,7 @@ ${placeholders.map { "${" ".repeat(28)}${it.key}: \"${it.value}\""}.joinToString
val projectAndroidManifestFile = File("$projectRootPath/AndroidManifest.xml") val projectAndroidManifestFile = File("$projectRootPath/AndroidManifest.xml")
File(projectRootPath).let { projectRootFile -> File(projectRootPath).let { projectRootFile ->
println("Working on $source") println("Generating $source")
projectRootFile.mkdirs() projectRootFile.mkdirs()
// remove everything from past runs // remove everything from past runs
@ -131,27 +135,22 @@ ${placeholders.map { "${" ".repeat(28)}${it.key}: \"${it.value}\""}.joinToString
writeAndroidManifest(projectAndroidManifestFile, manifestOverridePath, defaultAndroidManifestPath) writeAndroidManifest(projectAndroidManifestFile, manifestOverridePath, defaultAndroidManifestPath)
writeSourceClasses(projectSrcPath, srcOverridePath, source, themePkg, themeClass) writeSourceClasses(projectSrcPath, srcOverridePath, source, themePkg, themeClass)
copyThemeReadmes(userDir, themePkg, projectRootPath)
copyThemeClasses(userDir, themePkg, projectRootPath) copyThemeClasses(userDir, themePkg, projectRootPath)
copyThemeReadmes(overridesPath, projectRootPath)
copyResFiles(resOverridePath, defaultResPath, source, projectRootPath) copyResFiles(resOverridePath, defaultResPath, source, projectRootPath)
} }
} }
private fun copyThemeReadmes(userDir: String, themePkg: String, projectRootPath: String) { private fun copyThemeReadmes(overridesPath: String, projectRootPath: String) {
val sourcePath = "$userDir/multisrc/src/main/java/${themeSuffix(themePkg, "/")}"
val sourceFile = File(sourcePath)
val destinationPath = "$projectRootPath" val destinationPath = "$projectRootPath"
File(destinationPath).mkdirs()
val destinationFile = File(destinationPath) File(overridesPath)
destinationFile.mkdirs() ?.list()
?.filter { it.endsWith("README.md") || it.endsWith("CHANGELOG.md") }
sourceFile.list()!! ?.forEach {
.filter { it.endsWith("README.md") || it.endsWith("CHANGELOG.md") }
.forEach {
Files.copy( Files.copy(
File("$sourcePath/$it").toPath(), File("$overridesPath/$it").toPath(),
File("$destinationPath/$it").toPath(), File("$destinationPath/$it").toPath(),
StandardCopyOption.REPLACE_EXISTING StandardCopyOption.REPLACE_EXISTING
) )
@ -160,15 +159,14 @@ ${placeholders.map { "${" ".repeat(28)}${it.key}: \"${it.value}\""}.joinToString
private fun copyThemeClasses(userDir: String, themePkg: String, projectRootPath: String) { private fun copyThemeClasses(userDir: String, themePkg: String, projectRootPath: String) {
val themeSrcPath = "$userDir/multisrc/src/main/java/${themeSuffix(themePkg, "/")}" val themeSrcPath = "$userDir/multisrc/src/main/java/${themeSuffix(themePkg, "/")}"
val themeSrcFile = File(themeSrcPath)
val themeDestPath = "$projectRootPath/src/${themeSuffix(themePkg, "/")}" val themeDestPath = "$projectRootPath/src/${themeSuffix(themePkg, "/")}"
File(themeDestPath).mkdirs()
val themeDestFile = File(themeDestPath) File(themeSrcPath)
themeDestFile.mkdirs() ?.list()
?.filter { it.endsWith(".kt") && !it.endsWith("Generator.kt") }
themeSrcFile.list()!! ?.forEach { Files.copy(File("$themeSrcPath/$it").toPath(), File("$themeDestPath/$it").toPath(), StandardCopyOption.REPLACE_EXISTING) }
.filter { it.endsWith(".kt") && !it.endsWith("Generator.kt") }
.forEach { Files.copy(File("$themeSrcPath/$it").toPath(), File("$themeDestPath/$it").toPath(), StandardCopyOption.REPLACE_EXISTING) }
} }
private fun copyResFiles(resOverridePath: String, defaultResPath: String, source: ThemeSourceData, projectRootPath: String): Any { private fun copyResFiles(resOverridePath: String, defaultResPath: String, source: ThemeSourceData, projectRootPath: String): Any {
@ -185,16 +183,17 @@ ${placeholders.map { "${" ".repeat(28)}${it.key}: \"${it.value}\""}.joinToString
private fun writeSourceClasses(projectSrcPath: String, srcOverridePath: String, source: ThemeSourceData, themePkg: String, themeClass: String) { private fun writeSourceClasses(projectSrcPath: String, srcOverridePath: String, source: ThemeSourceData, themePkg: String, themeClass: String) {
val projectSrcFile = File(projectSrcPath) val projectSrcFile = File(projectSrcPath)
projectSrcFile.mkdirs() projectSrcFile.mkdirs()
val srcOverrideFile = File(srcOverridePath) val srcOverrideFile = File(srcOverridePath)
if (srcOverrideFile.exists()) if (srcOverrideFile.exists()) {
srcOverrideFile.copyRecursively(projectSrcFile) srcOverrideFile.copyRecursively(projectSrcFile)
else } else {
writeSourceClass(projectSrcFile, source, themePkg, themeClass) writeSourceClass(projectSrcFile, source, themePkg, themeClass)
} }
}
private fun writeSourceClass(classPath: File, source: ThemeSourceData, themePkg: String, themeClass: String) { private fun writeSourceClass(classPath: File, source: ThemeSourceData, themePkg: String, themeClass: String) {
fun factoryClassText(): String { fun factoryClassText() = when (source) {
return when (source) {
is ThemeSourceData.SingleLang -> { is ThemeSourceData.SingleLang -> {
"""class ${source.className} : $themeClass("${source.sourceName}", "${source.baseUrl}", "${source.lang}")""" """class ${source.className} : $themeClass("${source.sourceName}", "${source.baseUrl}", "${source.lang}")"""
} }
@ -212,7 +211,6 @@ ${placeholders.map { "${" ".repeat(28)}${it.key}: \"${it.value}\""}.joinToString
""".trimIndent() """.trimIndent()
} }
} }
}
File("$classPath/${source.className}.kt").writeText( File("$classPath/${source.className}.kt").writeText(
"""/* ktlint-disable */ """/* ktlint-disable */
@ -228,9 +226,11 @@ ${placeholders.map { "${" ".repeat(28)}${it.key}: \"${it.value}\""}.joinToString
} }
private fun cleanDirectory(dir: File) { private fun cleanDirectory(dir: File) {
dir.listFiles()?.forEach { file -> dir.listFiles()?.forEach {
if (file.isDirectory) cleanDirectory(file) if (it.isDirectory) {
file.delete() cleanDirectory(it)
}
it.delete()
} }
} }
} }