From dbb414e316b86e187907015c4b6354c263057aa8 Mon Sep 17 00:00:00 2001 From: SanmerDev Date: Fri, 24 May 2024 21:06:03 +0800 Subject: [PATCH] Fix `BaseModuleManagerImpl` --- .../compat/impl/BaseModuleManagerImpl.kt | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/BaseModuleManagerImpl.kt b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/BaseModuleManagerImpl.kt index 46906b0e..1c581fc9 100644 --- a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/BaseModuleManagerImpl.kt +++ b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/BaseModuleManagerImpl.kt @@ -38,21 +38,12 @@ internal abstract class BaseModuleManagerImpl( override fun getModules() = modulesDir.listFiles() .orEmpty() .mapNotNull { dir -> - readProps(dir) - ?.toModule( - state = readState(dir), - lastUpdated = readLastUpdated(dir) - ) + readProps(dir)?.toModule(dir) } override fun getModuleById(id: String): LocalModule? { val dir = modulesDir.resolve(id) - - return readProps(dir) - ?.toModule( - state = readState(dir), - lastUpdated = readLastUpdated(dir) - ) + return readProps(dir)?.toModule(dir) } override fun getModuleInfo(zipPath: String): LocalModule? { @@ -113,13 +104,22 @@ internal abstract class BaseModuleManagerImpl( } private fun Map.toModule( + dir: File + ) = toModule( + path = dir.name, + state = readState(dir), + lastUpdated = readLastUpdated(dir) + ) + + private fun Map.toModule( + path: String = "unknown", state: State = State.ENABLE, lastUpdated: Long = 0L ) = LocalModule( - id = getOrDefault("id", "unknown"), - name = getOrDefault("name", "unknown"), + id = getOrDefault("id", path), + name = getOrDefault("name", path), version = getOrDefault("version", ""), - versionCode = getOrDefault("versionCode", "-1").toInt(), + versionCode = getOrDefault("versionCode", "-1").toIntOr(-1), author = getOrDefault("author", ""), description = getOrDefault("description", ""), updateJson = getOrDefault("updateJson", ""), @@ -127,6 +127,11 @@ internal abstract class BaseModuleManagerImpl( lastUpdated = lastUpdated ) + private fun String.toIntOr(defaultValue: Int) = + runCatching { + toInt() + }.getOrDefault(defaultValue) + private fun String.exec() = ShellUtils.fastCmd(shell, this) internal fun install(cmd: String, path: String, callback: IInstallCallback) {