Skip to content

Commit

Permalink
Fix error with importing
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-hrivnak committed Jul 23, 2024
1 parent 005c15e commit 4b891f1
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/teksturepako/pakku/Version.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

package teksturepako.pakku

const val VERSION = "0.12.1"
const val VERSION = "0.13.1"
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package teksturepako.pakku.api.actions.export

import com.github.michaelbull.result.getError
import korlibs.io.async.launch
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.serialization.StringFormat
import kotlinx.serialization.encodeToString
import teksturepako.pakku.api.actions.ActionError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import com.github.michaelbull.result.Result
import teksturepako.pakku.api.actions.ActionError
import teksturepako.pakku.api.actions.ActionError.CouldNotImport
import teksturepako.pakku.api.models.ModpackModel
import java.nio.file.Path
import kotlin.io.path.pathString

suspend fun importModpackModel(
path: String,
path: Path,
): Result<ModpackModel, ActionError>
{
return when
{
path.isCfModpack() -> importCurseForge(path)
path.isMrModpack() -> importModrinth(path)
else -> Err(CouldNotImport(path))
else -> Err(CouldNotImport(path.pathString))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,31 @@ import teksturepako.pakku.api.actions.ActionError.FileNotFound
import teksturepako.pakku.api.data.json
import teksturepako.pakku.api.models.ModpackModel
import teksturepako.pakku.api.models.cf.CfModpackModel
import teksturepako.pakku.io.readFileOrNull
import teksturepako.pakku.io.unzip
import java.io.File
import teksturepako.pakku.io.readPathTextOrNull
import teksturepako.pakku.io.readPathTextFromZip
import java.nio.file.Path
import kotlin.io.path.*

private fun String?.toCfModpackModel(): ModpackModel? =
this?.let { json.decodeFromString<CfModpackModel>(it) }

fun String.isCfModpack(): Boolean = this.endsWith(CfModpackModel.EXTENSION) || this == CfModpackModel.MANIFEST
fun Path.isCfModpack(): Boolean = this.extension == CfModpackModel.EXTENSION || this.name == CfModpackModel.MANIFEST

suspend fun importCurseForge(path: String): Result<ModpackModel, ActionError>
suspend fun importCurseForge(path: Path): Result<ModpackModel, ActionError>
{
val file = File(path)
if (!path.exists()) return Err(FileNotFound(path.pathString))

if (!file.exists()) return Err(FileNotFound(path))

return if (file.extension == CfModpackModel.EXTENSION)
return if (path.extension == CfModpackModel.EXTENSION)
{
val cfModpackModel = runCatching {
unzip(path)[CfModpackModel.MANIFEST].readString().toCfModpackModel()
}.getOrNull() ?: return Err(FileNotFound(path))
val cfModpackModel = readPathTextFromZip(path, CfModpackModel.MANIFEST).toCfModpackModel()
?: return Err(FileNotFound(path.pathString))

Ok(cfModpackModel)
}
else
{
val cfModpackModel = readFileOrNull(path).toCfModpackModel()
?: return Err(FileNotFound(path))
val cfModpackModel = readPathTextOrNull(path).toCfModpackModel()
?: return Err(FileNotFound(path.pathString))

Ok(cfModpackModel)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,35 @@ import teksturepako.pakku.api.actions.ActionError.FileNotFound
import teksturepako.pakku.api.data.json
import teksturepako.pakku.api.models.ModpackModel
import teksturepako.pakku.api.models.mr.MrModpackModel
import teksturepako.pakku.io.readFileOrNull
import teksturepako.pakku.io.unzip
import teksturepako.pakku.io.readPathTextOrNull
import teksturepako.pakku.io.readPathTextFromZip
import java.io.File
import java.nio.file.Path
import kotlin.io.path.exists
import kotlin.io.path.extension
import kotlin.io.path.name
import kotlin.io.path.pathString

private fun String?.toMrModpackModel(): ModpackModel? =
this?.let { json.decodeFromString<MrModpackModel>(it) }

fun String.isMrModpack(): Boolean = this.endsWith(MrModpackModel.EXTENSION) || this == MrModpackModel.MANIFEST
fun Path.isMrModpack(): Boolean = this.extension == MrModpackModel.EXTENSION || this.name == MrModpackModel.MANIFEST

suspend fun importModrinth(path: String): Result<ModpackModel, ActionError>
suspend fun importModrinth(path: Path): Result<ModpackModel, ActionError>
{
val file = File(path)
if (!path.exists()) return Err(FileNotFound(path.pathString))

if (!file.exists()) return Err(FileNotFound(path))

return if (file.extension == MrModpackModel.EXTENSION)
return if (path.extension == MrModpackModel.EXTENSION)
{
val cfModpackModel = runCatching {
unzip(path)[MrModpackModel.MANIFEST].readString().toMrModpackModel()
}.getOrNull() ?: return Err(FileNotFound(path))
val cfModpackModel = readPathTextFromZip(path, MrModpackModel.MANIFEST).toMrModpackModel()
?: return Err(FileNotFound(path.pathString))

Ok(cfModpackModel)
}
else
{
val cfModpackModel = readFileOrNull(path).toMrModpackModel()
?: return Err(FileNotFound(path))
val cfModpackModel = readPathTextOrNull(path).toMrModpackModel()
?: return Err(FileNotFound(path.pathString))

Ok(cfModpackModel)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import teksturepako.pakku.api.projects.ProjectSide
import teksturepako.pakku.api.projects.UpdateStrategy
import teksturepako.pakku.io.decodeOrNew
import teksturepako.pakku.io.decodeToResult
import teksturepako.pakku.io.readFileOrNull
import teksturepako.pakku.io.readPathTextOrNull
import teksturepako.pakku.io.writeToFile

@Serializable
Expand Down Expand Up @@ -94,7 +94,7 @@ data class ConfigFile(
{
const val FILE_NAME = "pakku.json"

suspend fun exists(): Boolean = readFileOrNull("$workingPath/$FILE_NAME") != null
suspend fun exists(): Boolean = readPathTextOrNull("$workingPath/$FILE_NAME") != null

suspend fun readOrNew(): ConfigFile = decodeOrNew(ConfigFile(), "$workingPath/$FILE_NAME")

Expand Down
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/teksturepako/pakku/api/data/LockFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import teksturepako.pakku.api.projects.inheritPropertiesFrom
import teksturepako.pakku.debug
import teksturepako.pakku.io.decodeOrNew
import teksturepako.pakku.io.decodeToResult
import teksturepako.pakku.io.readFileOrNull
import teksturepako.pakku.io.readPathTextOrNull
import teksturepako.pakku.io.writeToFile

/**
Expand Down Expand Up @@ -255,7 +255,7 @@ data class LockFile(
{
const val FILE_NAME = "pakku-lock.json"

suspend fun exists(): Boolean = readFileOrNull("$workingPath/$FILE_NAME") != null
suspend fun exists(): Boolean = readPathTextOrNull("$workingPath/$FILE_NAME") != null

/** Reads [LockFile] and parses it, or returns a new [LockFile]. */
suspend fun readOrNew(): LockFile = decodeOrNew<LockFile>(LockFile(), "$workingPath/$FILE_NAME")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package teksturepako.pakku.api.models.mr

import korlibs.io.async.async
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.Serializable
Expand Down
8 changes: 6 additions & 2 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Import.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package teksturepako.pakku.cli.cmd
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.terminal
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.optional
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.path
import com.github.michaelbull.result.getOrElse
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
Expand All @@ -20,15 +22,17 @@ import teksturepako.pakku.cli.ui.getFlavoredSlug
import teksturepako.pakku.cli.ui.pError
import teksturepako.pakku.cli.ui.pSuccess
import teksturepako.pakku.cli.ui.promptForProject
import java.nio.file.Path
import kotlin.io.path.pathString

class Import : CliktCommand("Import modpack")
{
private val pathArg: String by argument("path")
private val pathArg: Path by argument("path").path(mustExist = true)
private val depsFlag: Boolean by option("-D", "--deps", help = "Resolve dependencies").flag()

override fun run() = runBlocking {
val modpackModel = importModpackModel(pathArg).getOrElse {
terminal.pError(it, pathArg)
terminal.pError(it, pathArg.pathString)
echo()
return@runBlocking
}
Expand Down
28 changes: 11 additions & 17 deletions src/commonMain/kotlin/teksturepako/pakku/io/ReadFile.kt
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
package teksturepako.pakku.io

import korlibs.io.file.std.localCurrentDirVfs
import kotlinx.serialization.StringFormat
import kotlinx.serialization.serializer
import teksturepako.pakku.api.data.PakkuException
import teksturepako.pakku.api.data.json
import kotlin.io.path.Path
import kotlin.io.path.readBytes
import kotlin.io.path.readText

suspend fun readFileOrNull(path: String): String?
fun readPathTextOrNull(path: String): String?
{
val file = localCurrentDirVfs[path]

return if (file.exists()) {
runCatching { file.readString() }.getOrNull()
} else null
return runCatching { Path(path).readText() }.getOrNull()
}

suspend fun readFileBytesOrNull(path: String): ByteArray?
fun readPathBytesOrNull(path: String): ByteArray?
{
val file = localCurrentDirVfs[path]

return if (file.exists()) {
runCatching { file.readBytes() }.getOrNull()
} else null
return runCatching { Path(path).readBytes() }.getOrNull()
}

suspend inline fun <reified T> decodeOrNew(
inline fun <reified T> decodeOrNew(
value: T, path: String, format: StringFormat = json
): T = readFileOrNull(path)?.let {
): T = readPathTextOrNull(path)?.let {
runCatching { format.decodeFromString<T>(format.serializersModule.serializer(), it) }.getOrElse { value }
} ?: value

suspend inline fun <reified T> decodeToResult(
inline fun <reified T> decodeToResult(
path: String, format: StringFormat = json
): Result<T> = readFileOrNull(path)?.let {
): Result<T> = readPathTextOrNull(path)?.let {
runCatching { Result.success(format.decodeFromString<T>(format.serializersModule.serializer(), it)) }.getOrElse { exception ->
Result.failure(PakkuException("Error occurred while reading '$path': ${exception.message}"))
}
Expand Down
3 changes: 1 addition & 2 deletions src/commonMain/kotlin/teksturepako/pakku/io/ReadPath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ suspend fun <T> Path.tryToResult(action: (path: Path) -> T): Result<T, ActionErr
}
}

@Suppress("unused")
suspend fun readPathOrNull(path: Path): String? = coroutineScope {
suspend fun readPathTextOrNull(path: Path): String? = coroutineScope {
withContext(Dispatchers.IO) {
return@withContext if (path.exists()) runCatching { path.readText() }.get() else null
}
Expand Down
15 changes: 7 additions & 8 deletions src/commonMain/kotlin/teksturepako/pakku/io/WriteToFile.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package teksturepako.pakku.io

import korlibs.io.file.std.localCurrentDirVfs
import kotlinx.serialization.StringFormat
import kotlinx.serialization.encodeToString
import teksturepako.pakku.api.data.json
import kotlin.io.path.*

suspend inline fun <reified T> writeToFile(
inline fun <reified T> writeToFile(
value: T,
path: String,
overrideText: Boolean = false,
format: StringFormat = json
)
{
val file = localCurrentDirVfs[path]
) = runCatching {
val file = Path(path)

// Override file text
if (overrideText && file.exists()) file.delete()
if (overrideText && file.exists()) file.deleteIfExists()

// Write to file
file.parent.mkdirs()
file.writeString(format.encodeToString(value))
file.parent.createParentDirectories()
file.writeText(format.encodeToString(value))
}

0 comments on commit 4b891f1

Please sign in to comment.