Skip to content

Commit

Permalink
Improve status cmd & document some code
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-hrivnak committed Aug 11, 2024
1 parent 844f0b1 commit 1c705fe
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 38 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.14.0"
const val VERSION = "0.14.3"
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ open class ActionError(
val isWarning: Boolean = false,
)
{
/** A message dedicated for the CLI. Should not be used outside of terminal. */
/** A message dedicated for the CLI. It should not be used outside of terminal. */
open fun message(arg: String = ""): String = rawMessage

// -- FILE --
Expand Down
12 changes: 6 additions & 6 deletions src/commonMain/kotlin/teksturepako/pakku/api/data/ConfigFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,20 @@ data class ConfigFile(
{
const val FILE_NAME = "pakku.json"

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

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

suspend fun readOrNull() = decodeToResult<ConfigFile>("$workingPath/$FILE_NAME").getOrNull()
fun readOrNull() = decodeToResult<ConfigFile>("$workingPath/$FILE_NAME").getOrNull()

/**
* Reads [LockFile] and parses it, or returns an exception.
* Use [Result.fold] to map it's [success][Result.success] or [failure][Result.failure] values.
*/
suspend fun readToResult(): Result<ConfigFile> = decodeToResult("$workingPath/$FILE_NAME")
fun readToResult(): Result<ConfigFile> = decodeToResult("$workingPath/$FILE_NAME")

suspend fun readToResultFrom(path: String): Result<ConfigFile> = decodeToResult(path)
fun readToResultFrom(path: String): Result<ConfigFile> = decodeToResult(path)
}

suspend fun write() = writeToFile(this, "$workingPath/$FILE_NAME", overrideText = true, format = jsonEncodeDefaults)
fun write() = writeToFile(this, "$workingPath/$FILE_NAME", overrideText = true, format = jsonEncodeDefaults)
}
29 changes: 19 additions & 10 deletions src/commonMain/kotlin/teksturepako/pakku/api/data/LockFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,29 @@ import teksturepako.pakku.io.readPathTextOrNull
import teksturepako.pakku.io.writeToFile

/**
* A LockFile is used to define all properties of a Pakku modpack.
* A lock file (`pakku-lock.json`) is an automatically generated file used by Pakku
* to define all properties of a modpack needed for its development.
*
* @property target The targeted platform of the modpack.
* @property mcVersions The Minecraft versions supported by the mod pack.
* @property loaders The mod loaders used by the mod pack.
* @property projects A list of associated projects with the mod pack.
* @property lockFileVersion The version of the LockFile.
* This file is not intended to be modified manually.
*/
@Serializable
data class LockFile(
/** Targeted platform of the modpack. */
private var target: String? = null,

/** Minecraft versions supported by the modpack. */
@SerialName("mc_versions") private var mcVersions: MutableList<String> = mutableListOf(),

/**
* Mutable map of _loader names_ to _loader versions_ supported by the modpack.
* _loader names_ will always be formated to lowercase.
*/
private val loaders: MutableMap<String, String> = mutableMapOf(),

/** List of projects included in the modpack. */
private var projects: MutableList<Project> = mutableListOf(),

/** The version of the LockFile. */
@SerialName("lockfile_version") @Required private var lockFileVersion: Int? = null,
)
{
Expand Down Expand Up @@ -71,8 +80,8 @@ data class LockFile(
this.loaders.putAll(loaders)
}

fun getLoaders() = this.loaders.keys.toList()
fun getLoadersWithVersions() = this.loaders.toList()
fun getLoaders() = this.loaders.keys.toList().map { it.lowercase() }
fun getLoadersWithVersions() = this.loaders.toList().map { it.first.lowercase() }

// -- TARGET --

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

suspend fun exists(): Boolean = readPathTextOrNull("$workingPath/$FILE_NAME") != null
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 All @@ -272,5 +281,5 @@ data class LockFile(
.onSuccess { it.inheritConfig(ConfigFile.readOrNull()) }
}

suspend fun write() = writeToFile(this, "$workingPath/$FILE_NAME", overrideText = true)
fun write() = writeToFile(this, "$workingPath/$FILE_NAME", overrideText = true)
}

This file was deleted.

37 changes: 31 additions & 6 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Status.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import kotlinx.coroutines.runBlocking
import teksturepako.pakku.api.data.ConfigFile
import teksturepako.pakku.api.data.LockFile
import teksturepako.pakku.api.platforms.Multiplatform
import teksturepako.pakku.api.platforms.Platform
import teksturepako.pakku.api.projects.containsProject
import teksturepako.pakku.cli.ui.*


class Status: CliktCommand("Get status of your modpack")
{
private val verboseFlag by option("-v", "--verbose", help = "Give the output in the verbose-format").flag()

override fun run() = runBlocking {
val lockFile = LockFile.readToResult().getOrElse {
terminal.danger(it.message)
Expand All @@ -30,14 +29,40 @@ class Status: CliktCommand("Get status of your modpack")
null
}

val platforms: List<Platform> = lockFile.getPlatforms().getOrElse {
terminal.danger(it.message)
echo()
return@runBlocking
}

if (configFile != null)
{
terminal.pInfo("Managing '${strong(configFile.getName())}' modpack, " +
"version '${strong(configFile.getVersion())}', " +
"by '${strong(configFile.getAuthor())}'"
)
val msg = buildString {
append("Managing '${strong(configFile.getName())}' modpack")

if (configFile.getVersion().isNotBlank())
{
append("; version '${strong(configFile.getVersion())}'")
}

if (configFile.getAuthor().isNotBlank())
{
append("; by '${strong(configFile.getAuthor())}'")
}
}

terminal.pInfo(msg)
}

terminal.pInfo(buildString {
val mcVer = lockFile.getMcVersions()
val loaders = lockFile.getLoadersWithVersions()

append("On Minecraft " + (if (mcVer.size > 1) "versions" else "version") + " ${strong(mcVer)}; ")
append((if (loaders.size > 1) "loaders" else "loader") + " ${strong(loaders)}; ")
append("targeting " + (if (platforms.size > 1) "platforms" else "platform") + " ${strong(platforms)}")
})

val currentProjects = lockFile.getAllProjects()
val updatedProjects =
Multiplatform.updateMultipleProjectsWithFiles(
Expand Down
21 changes: 9 additions & 12 deletions src/commonMain/kotlin/teksturepako/pakku/cli/ui/CliConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ data class CliConfig(
@SerialName("ansi_level") val ansiLevel: String? = null
)
{
fun toTerminal(): Terminal
{
return Terminal(
theme = when (theme?.lowercase())
{
"default" -> CliThemes.Default
"ascii" -> CliThemes.Ascii
fun toTerminal(): Terminal = Terminal(
theme = when (theme?.lowercase())
{
"default" -> CliThemes.Default
"ascii" -> CliThemes.Ascii

else -> CliThemes.Ascii
},
ansiLevel = this.ansiLevel?.let { AnsiLevel.valueOf(it.uppercase()) }
)
}
else -> CliThemes.Ascii
},
ansiLevel = this.ansiLevel?.let { AnsiLevel.valueOf(it.uppercase()) }
)

companion object
{
Expand Down
5 changes: 5 additions & 0 deletions src/commonMain/kotlin/teksturepako/pakku/cli/ui/CliThemes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.ajalt.mordant.rendering.Theme

object CliThemes
{
/** Default theme for Pakku that extends the Mordant default theme. */
val Default: Theme = Theme(Theme.Default) {
strings["pakku.prefix"] = "❯❯❯"
strings["pakku.warning_sign"] = ""
Expand All @@ -13,6 +14,10 @@ object CliThemes
strings["pakku.update_strategy.none"] = "✖^"
}

/**
* ASCII theme for Pakku that replaces UTF-8 characters
* from the Pakku default theme with respective ASCII characters.
*/
val Ascii: Theme = Theme(this.Default) {
strings["pakku.prefix"] = ">>>"
strings["pakku.warning_sign"] = "!"
Expand Down
1 change: 1 addition & 0 deletions src/commonMain/kotlin/teksturepako/pakku/cli/ui/Msg.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fun dim(string: String): String = TextStyle(color = TextColors.gray)(string)
fun String.addDim(string: String): String = this + TextStyle(color = TextColors.gray)(string)

fun strong(string: String): String = TextStyle(bold = true, underline = true)(string)
fun strong(text: Any): String = TextStyle(bold = true, underline = true)(text.toString())
fun String.addStrong(string: String): String = this + TextStyle(bold = true, underline = true)(string)

fun String.createHyperlink(hyperlink: String): String = TextStyle(hyperlink = hyperlink)(this)
Expand Down

0 comments on commit 1c705fe

Please sign in to comment.