127 lines
No EOL
3.7 KiB
Kotlin
127 lines
No EOL
3.7 KiB
Kotlin
import com.squareup.kotlinpoet.*
|
|
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
|
|
import org.json.JSONArray
|
|
|
|
plugins {
|
|
alias(deps.plugins.android.library)
|
|
alias(deps.plugins.kotlin.android)
|
|
alias(deps.plugins.kotlin.kapt)
|
|
alias(deps.plugins.hilt.android)
|
|
alias(deps.plugins.ksp)
|
|
id("configuration")
|
|
}
|
|
|
|
buildscript {
|
|
dependencies {
|
|
classpath("com.squareup:kotlinpoet:1.15.0")
|
|
classpath("org.json:json:20231013")
|
|
}
|
|
}
|
|
|
|
android {
|
|
namespace = "com.tangem.core.configtoggle"
|
|
sourceSets["main"].java.srcDir("build/generated/source/toggles")
|
|
}
|
|
|
|
tasks.named("preBuild") {
|
|
dependsOn(generateFeatureToggles, generateExcludedBlockchainToggles)
|
|
}
|
|
|
|
tasks.withType<Test>().configureEach {
|
|
useJUnitPlatform()
|
|
}
|
|
|
|
dependencies {
|
|
/** DI */
|
|
implementation(deps.hilt.android)
|
|
kapt(deps.hilt.kapt)
|
|
|
|
/** Local storages */
|
|
implementation(deps.androidx.datastore)
|
|
|
|
/** Other libraries */
|
|
implementation(deps.moshi)
|
|
implementation(deps.moshi.kotlin)
|
|
implementation(deps.timber)
|
|
ksp(deps.moshi.kotlin.codegen)
|
|
|
|
/** Core modules */
|
|
implementation(projects.core.datasource)
|
|
implementation(projects.core.utils)
|
|
|
|
testImplementation(projects.test.core)
|
|
testRuntimeOnly(deps.test.junit5.engine)
|
|
}
|
|
|
|
val generateFeatureToggles by tasks.registering {
|
|
generateToggles(
|
|
inputFilePath = "src/main/assets/configs/feature_toggles_config.json",
|
|
generatedFileName = "FeatureToggles",
|
|
)
|
|
}
|
|
|
|
val generateExcludedBlockchainToggles by tasks.registering {
|
|
generateToggles(
|
|
inputFilePath = "src/main/assets/configs/excluded_blockchains_config.json",
|
|
generatedFileName = "ExcludedBlockchainToggles",
|
|
)
|
|
}
|
|
|
|
fun Task.generateToggles(inputFilePath: String, generatedFileName: String) {
|
|
val inputFile = file(inputFilePath)
|
|
val outputDir = file("build/generated/source/toggles")
|
|
|
|
inputs.file(inputFile)
|
|
outputs.dir(outputDir)
|
|
|
|
doLast {
|
|
val jsonText = inputFile.readText()
|
|
val jsonArray = JSONArray(jsonText)
|
|
|
|
val entries = (0 until jsonArray.length()).map { i ->
|
|
val obj = jsonArray.getJSONObject(i)
|
|
val name = obj.getString("name")
|
|
val version = obj.getString("version")
|
|
CodeBlock.of("%S to %S", name, version)
|
|
}
|
|
|
|
val mapInitializer = CodeBlock.builder()
|
|
.add("mapOf(\n")
|
|
.indent()
|
|
.apply {
|
|
entries.forEach { entry ->
|
|
add(entry)
|
|
add(",\n")
|
|
}
|
|
}
|
|
.unindent()
|
|
.add(")")
|
|
.build()
|
|
|
|
val objectBuilder = TypeSpec.objectBuilder(name = generatedFileName)
|
|
.addKdoc("Generated from $inputFilePath")
|
|
.addProperty(
|
|
PropertySpec.builder("values", MAP.parameterizedBy(STRING, STRING))
|
|
.initializer(mapInitializer)
|
|
.build()
|
|
)
|
|
|
|
val fileSpec = FileSpec.builder(packageName = "com.tangem.core.configtoggle", fileName = generatedFileName)
|
|
.addType(objectBuilder.build())
|
|
.build()
|
|
|
|
val outputPackageDir = File(outputDir, "")
|
|
outputPackageDir.mkdirs()
|
|
fileSpec.writeTo(outputPackageDir)
|
|
|
|
// Remove redundant public visibility modifiers
|
|
val generatedFile = File(outputPackageDir, "com/tangem/core/configtoggle/$generatedFileName.kt")
|
|
if (generatedFile.exists()) {
|
|
val content = generatedFile.readText()
|
|
val fixedContent = content
|
|
.replace("public object ", "object ")
|
|
.replace("public val ", "val ")
|
|
generatedFile.writeText(fixedContent)
|
|
}
|
|
}
|
|
} |