Commit e554fd85 authored by Setako's avatar Setako

[Add] component list export

parent 21e08de3
......@@ -41,7 +41,7 @@ internal class ReactantCommandRegister(
command(::ReactantMainCommand) {
command(::ReactantComponentCommand) {
command({ ReactantComponentListSubCommand(providerManager, containerManager) })
command({ ReactantComponentListSubCommand(providerManager, containerManager, jsonParserService, fileIOUploadService) })
}
command(::ReactantEchoCommand)
......
......@@ -4,9 +4,13 @@ import dev.reactant.reactant.core.commands.ReactantPermissions
import dev.reactant.reactant.core.component.container.ContainerManager
import dev.reactant.reactant.core.dependency.ProviderManager
import dev.reactant.reactant.core.dependency.injection.producer.ComponentProvider
import dev.reactant.reactant.core.dependency.injection.producer.Provider
import dev.reactant.reactant.extra.command.ReactantCommand
import dev.reactant.reactant.extra.file.FileIOUploadService
import dev.reactant.reactant.extra.parser.GsonJsonParserService
import dev.reactant.reactant.utils.PatternMatchingUtils
import dev.reactant.reactant.utils.formatting.MultiColumns
import okhttp3.MediaType
import picocli.CommandLine
import java.util.regex.Pattern
import kotlin.reflect.jvm.jvmErasure
......@@ -20,7 +24,9 @@ import kotlin.reflect.jvm.jvmName
)
internal class ReactantComponentListSubCommand(
val providerManager: ProviderManager,
val containerManager: ContainerManager
val containerManager: ContainerManager,
val jsonParserService: GsonJsonParserService,
val fileIOUploadService: FileIOUploadService
) : ReactantCommand() {
@CommandLine.Option(names = ["-r", "--is-running"], paramLabel = "IS_RUNNING",
......@@ -43,6 +49,10 @@ internal class ReactantComponentListSubCommand(
description = ["Filtering Component class canonical name, wildcard is available"])
var classNameWildcards: ArrayList<String> = arrayListOf();
@CommandLine.Option(names = ["-e", "--export"],
description = ["Export the file as json"])
var export: Boolean = false
private val listTable = MultiColumns.create {
column { align = MultiColumns.Alignment.Right }
column { maxLength = 50; overflowCutFromRight = false; }
......@@ -66,9 +76,19 @@ internal class ReactantComponentListSubCommand(
.filter { matchClassNameWildcards(it) }
.filter { matchContainerIdentifierWildcards(it.container.identifier) }
.toList()
.forEach { addToListTable(it) }
.map { ComponentRow(it, showShortName, providerManager.blacklistedProviders) }
.let {
if (export) {
stdout.out("Exporting components...")
jsonParserService.encode(it).flatMap {
fileIOUploadService.upload("component-list.json", it, MediaType.parse("application/json")!!)
}.subscribe { resp -> stdout.out("Exported and uploaded to ${resp.link}") }
} else {
it.forEach { addToListTable(it) }
listTable.generate().forEach(stdout::out)
}
}
}
private fun matchClassNameWildcards(componentProvider: ComponentProvider<*>) =
classNameWildcards.isEmpty() || classNameWildcards.any { wildcard ->
......@@ -80,16 +100,26 @@ internal class ReactantComponentListSubCommand(
PatternMatchingUtils.matchWildcard(wildcard, identifier)
}
private fun addToListTable(componentProvider: ComponentProvider<*>) {
listTable.rows.add(listOf<String>(
private fun addToListTable(componentRow: ComponentRow) {
listTable.rows.add(listOf(
componentRow.hashCode, componentRow.jvmName, componentRow.container, componentRow.state
))
}
}
private data class ComponentRow(
val hashCode: String,
val jvmName: String,
val container: String,
val state: String
) {
constructor(componentProvider: ComponentProvider<*>, showShortName: Boolean, blacklist: HashSet<Provider>) : this(
componentProvider.hashCode().toString(36),
componentProvider.productType.jvmErasure.let { if (showShortName) it.simpleName!! else it.jvmName },
componentProvider.container.identifier,
if (componentProvider.isInitialized()) "Running"
else if (providerManager.blacklistedProviders.contains(componentProvider)) "Blacklisted"
else if (blacklist.contains(componentProvider)) "Blacklisted"
else if (componentProvider.catchedThrowable != null) "Error"
else if (!componentProvider.fulfilled) "Not Fulfilled" else ""
))
}
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment