Skip to content

Commit

Permalink
feat: add project aliases config
Browse files Browse the repository at this point in the history
fix #33
fix #12
  • Loading branch information
SettingDust committed Oct 6, 2024
1 parent 5585603 commit fd23de2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
17 changes: 10 additions & 7 deletions src/commonMain/kotlin/teksturepako/pakku/api/data/ConfigFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

package teksturepako.pakku.api.data

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import teksturepako.pakku.api.overrides.filterOverrides
import teksturepako.pakku.api.projects.Project
import teksturepako.pakku.api.projects.ProjectSide
import teksturepako.pakku.api.projects.ProjectType
import teksturepako.pakku.api.projects.UpdateStrategy
Expand Down Expand Up @@ -34,16 +34,19 @@ data class ConfigFile(
private val overrides: MutableList<String> = mutableListOf(),

/** A mutable list of server overrides packed up with the modpack. */
@SerialName("server_overrides") private val serverOverrides: MutableList<String> = mutableListOf(),
private val serverOverrides: MutableList<String> = mutableListOf(),

/** A mutable list of client overrides packed up with the modpack. */
@SerialName("client_overrides") private val clientOverrides: MutableList<String> = mutableListOf(),
private val clientOverrides: MutableList<String> = mutableListOf(),

/** A map of project types to their respective paths. */
val paths: MutableMap<String, String> = mutableMapOf(),

/** A mutable map of _project slugs, names, IDs or filenames_ to _project configs_. */
val projects: MutableMap<String, ProjectConfig> = mutableMapOf()
val projects: MutableMap<String, ProjectConfig> = mutableMapOf(),

/** A mutable map of project aliases to their respective project slugs. */
val projectAliases: MutableMap<String, String> = mutableMapOf()
)
{
// -- PACK --
Expand Down Expand Up @@ -110,8 +113,8 @@ data class ConfigFile(
data class ProjectConfig(
var type: ProjectType? = null,
var side: ProjectSide? = null,
@SerialName("update_strategy") var updateStrategy: UpdateStrategy? = null,
@SerialName("redistributable") var redistributable: Boolean? = null,
var updateStrategy: UpdateStrategy? = null,
var redistributable: Boolean? = null,
var subpath: String? = null
)

Expand All @@ -136,5 +139,5 @@ data class ConfigFile(
fun readToResultFrom(path: String): Result<ConfigFile> = decodeToResult(path)
}

suspend fun write() = writeToFile(this, "$workingPath/$FILE_NAME", overrideText = true, format = json)
suspend fun write() = writeToFile(this, "$workingPath/$FILE_NAME", overrideText = true, format = jsonSnakeCase)
}
6 changes: 6 additions & 0 deletions src/commonMain/kotlin/teksturepako/pakku/api/data/Json.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package teksturepako.pakku.api.data

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonNamingStrategy

@OptIn(ExperimentalSerializationApi::class)
val json = Json {
Expand All @@ -18,3 +19,8 @@ val jsonEncodeDefaults = Json {
encodeDefaults = true
}

@OptIn(ExperimentalSerializationApi::class)
val jsonSnakeCase = Json(json) {
namingStrategy = JsonNamingStrategy.SnakeCase
}

5 changes: 3 additions & 2 deletions src/commonMain/kotlin/teksturepako/pakku/api/data/LockFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ data class LockFile(
return removed
}

fun getProject(input: String): Project? = this.projects
.find { project -> input in project || project.files.any { input in it.fileName } }
fun getProject(input: String): Project? = (ConfigFile.readOrNull()?.projectAliases?.get(input) ?: input).let { transformedInput ->
this.projects.find { project -> transformedInput in project || project.files.any { transformedInput in it.fileName } }
}

fun getProject(project: Project): Project? = this.projects.find { it isAlmostTheSameAs project }

Expand Down
5 changes: 3 additions & 2 deletions src/commonMain/kotlin/teksturepako/pakku/io/ReadFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.serialization.StringFormat
import kotlinx.serialization.serializer
import teksturepako.pakku.api.data.PakkuException
import teksturepako.pakku.api.data.json
import teksturepako.pakku.api.data.jsonSnakeCase
import kotlin.io.path.Path
import kotlin.io.path.readBytes
import kotlin.io.path.readText
Expand All @@ -19,13 +20,13 @@ fun readPathBytesOrNull(path: String): ByteArray?
}

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

inline fun <reified T> decodeToResult(
path: String, format: StringFormat = json
path: String, format: StringFormat = jsonSnakeCase
): 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: 2 additions & 1 deletion src/commonMain/kotlin/teksturepako/pakku/io/WriteToFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import kotlinx.serialization.StringFormat
import kotlinx.serialization.encodeToString
import teksturepako.pakku.api.actions.ActionError
import teksturepako.pakku.api.data.json
import teksturepako.pakku.api.data.jsonSnakeCase
import kotlin.io.path.*

suspend inline fun <reified T> writeToFile(
value: T,
path: String,
overrideText: Boolean = false,
format: StringFormat = json
format: StringFormat = jsonSnakeCase
): ActionError?
{
val file = Path(path)
Expand Down

0 comments on commit fd23de2

Please sign in to comment.