Skip to content

Commit

Permalink
Improve importing
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-hrivnak committed Jun 5, 2024
1 parent a36282a commit 361cf59
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 40 deletions.
12 changes: 12 additions & 0 deletions src/commonMain/kotlin/teksturepako/pakku/Debug.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import kotlin.contracts.contract
*/
var debugMode = false

/**
* Debug scope function; Use anywhere to add code called only in debug mode.
*/
@OptIn(ExperimentalContracts::class)
inline fun debug(block: () -> Unit)
{
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
if (debugMode) block()
}

/**
* Debug scope function; Use anywhere to add code called only in debug mode.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/teksturepako/pakku/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fun main(args: Array<String>)
// Check Modrinth's rate limit
Modrinth.checkRateLimit()

println("Program arguments: ${args.joinToString()}")
debug { println("Program arguments: ${args.joinToString()}") }

// Close http client & exit program
client.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import teksturepako.pakku.api.data.ConfigFile
import teksturepako.pakku.api.data.LockFile
import teksturepako.pakku.api.data.json
import teksturepako.pakku.api.data.jsonEncodeDefaults
import teksturepako.pakku.api.models.ModpackModel.CfModpackModel
import teksturepako.pakku.api.models.ModpackModel.CfModpackModel.*
import teksturepako.pakku.api.models.ModpackModel.MrModpackModel
import teksturepako.pakku.api.models.ModpackModel.MrModpackModel.File
import teksturepako.pakku.api.models.cf.CfModpackModel
import teksturepako.pakku.api.models.cf.CfModpackModel.*
import teksturepako.pakku.api.models.mr.MrModpackModel
import teksturepako.pakku.api.models.mr.MrModpackModel.File
import teksturepako.pakku.api.overrides.OverrideType
import teksturepako.pakku.api.overrides.Overrides
import teksturepako.pakku.api.overrides.Overrides.ProjectOverride
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@ package teksturepako.pakku.api.actions.import

import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Result
import com.github.michaelbull.result.map
import teksturepako.pakku.api.actions.ActionError
import teksturepako.pakku.api.actions.ActionError.CouldNotImport
import teksturepako.pakku.api.data.LockFile
import teksturepako.pakku.api.platforms.Platform
import teksturepako.pakku.api.projects.Project
import teksturepako.pakku.api.models.ModpackModel

suspend fun import(
suspend fun importModpackModel(
path: String,
lockFile: LockFile,
platforms: List<Platform>
): Result<Set<Project>, ActionError>
): Result<ModpackModel, ActionError>
{
val modpack = when
return when
{
path.isCfModpack() -> importCurseForge(path)
path.isMrModpack() -> importModrinth(path)
else -> Err(CouldNotImport(path))
}

return modpack.map { it.toSetOfProjects(lockFile, platforms) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.michaelbull.result.Result
import teksturepako.pakku.api.actions.ActionError
import teksturepako.pakku.api.actions.ActionError.FileNotFound
import teksturepako.pakku.api.data.json
import teksturepako.pakku.api.models.cf.CfModpackModel
import teksturepako.pakku.api.models.ModpackModel
import teksturepako.pakku.io.readFileOrNull
import teksturepako.pakku.io.unzip
Expand All @@ -15,7 +16,7 @@ private const val CF_EXTENSION = "zip"
private const val CF_MANIFEST = "manifest.json"

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

fun String.isCfModpack(): Boolean = this.endsWith(CF_EXTENSION) || this == CF_MANIFEST

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import teksturepako.pakku.api.actions.ActionError
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 java.io.File
Expand All @@ -15,7 +16,7 @@ private const val MR_EXTENSION = "mrpack"
private const val MR_MANIFEST = "modrinth.index.json"

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

fun String.isMrModpack(): Boolean = this.endsWith(MR_EXTENSION) || this == MR_MANIFEST

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonObject
import net.thauvin.erik.urlencoder.UrlEncoderUtil.decode
import teksturepako.pakku.api.data.json
import teksturepako.pakku.api.models.*
import teksturepako.pakku.api.models.cf.*
import teksturepako.pakku.api.projects.Project
import teksturepako.pakku.api.projects.ProjectFile
import teksturepako.pakku.api.projects.ProjectType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import kotlinx.serialization.json.JsonObject
import net.thauvin.erik.urlencoder.UrlEncoderUtil
import net.thauvin.erik.urlencoder.UrlEncoderUtil.encode
import teksturepako.pakku.api.data.json
import teksturepako.pakku.api.models.GetVersionsFromHashesRequest
import teksturepako.pakku.api.models.MrProjectModel
import teksturepako.pakku.api.models.MrVersionModel
import teksturepako.pakku.api.models.mr.GetVersionsFromHashesRequest
import teksturepako.pakku.api.models.mr.MrProjectModel
import teksturepako.pakku.api.models.mr.MrVersionModel
import teksturepako.pakku.api.projects.*
import teksturepako.pakku.debugIfEmpty
import teksturepako.pakku.io.exitPakku
Expand Down
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Diff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class Diff : CliktCommand("Diff projects in modpack")
{
private val oldPathArg by argument("path")
private val newPathArg by argument("path")
private val markdownDiffOpt by option("--markdown-diff", metavar = "<path>")
private val markdownOpt by option("--markdown", metavar = "<path>")
private val markdownDiffOpt by option("--markdown-diff", metavar = "<path>", help = "Export a `.md` file formatted as a diff code block")
private val markdownOpt by option("--markdown", metavar = "<path>", help = "Export a `.md` file formatted as regular markdown")

override fun run(): Unit = runBlocking {
val oldLockFile = LockFile.readToResultFrom(oldPathArg).getOrElse {
Expand Down
27 changes: 16 additions & 11 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Import.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import com.github.michaelbull.result.getOrElse
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import teksturepako.pakku.api.actions.ActionError
import teksturepako.pakku.api.actions.ActionError.AlreadyAdded
import teksturepako.pakku.api.actions.createAdditionRequest
import teksturepako.pakku.api.actions.import.import
import teksturepako.pakku.api.actions.import.importModpackModel
import teksturepako.pakku.api.data.LockFile
import teksturepako.pakku.api.platforms.Modrinth
import teksturepako.pakku.api.platforms.Platform
import teksturepako.pakku.cli.resolveDependencies
import teksturepako.pakku.cli.ui.getFlavoredSlug
import teksturepako.pakku.cli.ui.prefixed
import teksturepako.pakku.cli.ui.processErrorMsg
import teksturepako.pakku.cli.ui.promptForProject
Expand All @@ -23,7 +24,13 @@ class Import : CliktCommand("Import modpack")
private val pathArg: String by argument("path")

override fun run() = runBlocking {
val lockFile = LockFile.readOrNew()
val modpackModel = importModpackModel(pathArg).getOrElse {
terminal.println(processErrorMsg(it, pathArg))
echo()
return@runBlocking
}

val lockFile = LockFile.readToResult().getOrNull() ?: modpackModel.toLockFile()

// Configuration
val platforms: List<Platform> = lockFile.getPlatforms().getOrElse {
Expand All @@ -39,23 +46,21 @@ class Import : CliktCommand("Import modpack")
}
// --

val importedProjects = import(pathArg, lockFile, platforms).getOrElse {
terminal.danger(it.message)
echo()
return@runBlocking
}
val importedProjects = modpackModel.toSetOfProjects(lockFile, platforms)

importedProjects.map { projectIn ->
launch {
projectIn.createAdditionRequest(
onError = { error ->
if (error !is ActionError.AlreadyAdded) terminal.println(processErrorMsg(error))
if (error !is AlreadyAdded) terminal.println(processErrorMsg(error))
},
onRetry = { platform, _ ->
promptForProject(platform, terminal, lockFile)
},
onRetry = { platform, _ -> promptForProject(platform, terminal, lockFile) },
onSuccess = { project, _, reqHandlers ->
lockFile.add(project)
project.resolveDependencies(terminal, reqHandlers, lockFile, projectProvider, platforms)
terminal.success(prefixed("${project.slug} added"))
terminal.success(prefixed("${project.getFlavoredSlug()} added"))
Modrinth.checkRateLimit()
},
lockFile, platforms
Expand Down
8 changes: 5 additions & 3 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Pakku.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ class Pakku : CliktCommand()
append("## Usage\n")
append("\n")
append("<snippet id=\"snippet-cmd\">\n")
append(" <var name=\"cmd\">$cmdName</var>\n")
append(" <var name=\"help\"></var>\n")
append(" <include from=\"_template_cmd.md\" element-id=\"template-cmd\"/>\n")
append("\n")
append("<var name=\"cmd\">$cmdName</var>\n")
append("<var name=\"help\"></var>\n")
append("<include from=\"_template_cmd.md\" element-id=\"template-cmd\"/>\n")
append("\n")
append("</snippet>\n")
append("\n")
append("## Options\n")
Expand Down
7 changes: 6 additions & 1 deletion src/commonMain/kotlin/teksturepako/pakku/cli/ui/ErrorMsg.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ fun processErrorMsg(error: ActionError, arg: String = ""): String
{
val msg = when (error)
{
// -- ADDITION --
// -- PROJECT --

is ProjNotFound -> "Project '$arg' not found."

// -- ADDITION --

is AlreadyAdded -> "Could not add ${error.project.getFlavoredSlug()}. It is already added."
is NotFoundOnPlatform -> "${error.project.getFlavoredSlug()} was not found on ${error.platform.name}."

// -- REMOVAL --

is ProjRequiredBy -> "${error.project.getFlavoredSlug()} is required by " +
"${error.dependants.map { it.getFlavoredSlug() }}"

Expand Down

0 comments on commit 361cf59

Please sign in to comment.