Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add project aliases config #34

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ kotlin {
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation("io.mockk:mockk:1.13.12")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ data class ConfigFile(

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

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

Expand Down Expand Up @@ -112,7 +111,8 @@ data class ConfigFile(
var side: ProjectSide? = null,
@SerialName("update_strategy") var updateStrategy: UpdateStrategy? = null,
@SerialName("redistributable") var redistributable: Boolean? = null,
var subpath: String? = null
var subpath: String? = null,
var aliases: MutableSet<String>? = null
)

// -- FILE I/O --
Expand Down
1 change: 0 additions & 1 deletion src/commonMain/kotlin/teksturepako/pakku/api/data/Json.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ val jsonEncodeDefaults = Json {
classDiscriminator = "_internal"
encodeDefaults = true
}

14 changes: 13 additions & 1 deletion src/commonMain/kotlin/teksturepako/pakku/api/projects/Project.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ data class Project(
@SerialName("redistributable") var redistributable: Boolean = true,

private var subpath: String? = null,
var aliases: MutableSet<String>? = null,

var files: MutableSet<ProjectFile>
)
Expand Down Expand Up @@ -80,6 +81,8 @@ data class Project(
updateStrategy = this.updateStrategy,
redistributable = this.redistributable && other.redistributable,

aliases = this.aliases?.plus(other.aliases ?: emptySet())?.toMutableSet() ?: other.aliases,

files = (this.files + other.files).toMutableSet(),
)
)
Expand All @@ -91,14 +94,22 @@ data class Project(
return this.id.values.any { it in other.id.values }
|| this.name.values.any { it in other.name.values }
|| this.slug.values.any { it in other.slug.values }
|| hasAliasOf(other)
}

/** Check if the current project has an alias of the specified project. */
infix fun hasAliasOf(other: Project): Boolean
{
return this.aliases?.any { it in other.id.values || it in other.name.values || it in other.slug.values } ?: false
}

/** Checks if the current project contains the specified string in its slugs, names or IDs. */
/** Checks if the current project contains the specified string in its slugs, names, IDs or aliases. */
operator fun contains(input: String): Boolean
{
return input in this.slug.values
|| input in this.name.values
|| input in this.id.values
|| this.aliases?.contains(input) == true
}

/** Checks if the project has any files. */
Expand Down Expand Up @@ -191,6 +202,7 @@ data class Project(
config.updateStrategy?.let { this.updateStrategy = it }
config.redistributable?.let { this.redistributable = it }
config.subpath?.let { this.subpath = it }
config.aliases?.let { this.aliases = it }
}
}

Expand Down
1 change: 0 additions & 1 deletion src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Add.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class Add : CliktCommand()
echo()
return@runBlocking
}

val platforms: List<Platform> = lockFile.getPlatforms().getOrElse {
terminal.danger(it.message)
echo()
Expand Down
1 change: 1 addition & 0 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Cfg.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.Context
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.core.terminal
import com.github.ajalt.clikt.parameters.options.associate
import com.github.ajalt.clikt.parameters.options.help
import com.github.ajalt.clikt.parameters.options.option
import kotlinx.coroutines.runBlocking
Expand Down
10 changes: 10 additions & 0 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/CfgPrj.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class CfgPrj : CliktCommand("prj")
private val subpathOpt: String? by option("-p", "--subpath", metavar = "path")
.help("Change the subpath of the project")

private val aliasOpt: String? by option("-a", "--alias", metavar = "alias")
.help("Add alias to the project")

override fun run(): Unit = runBlocking {
val lockFile = LockFile.readToResult().getOrElse {
terminal.danger(it.message)
Expand Down Expand Up @@ -106,6 +109,13 @@ class CfgPrj : CliktCommand("prj")
terminal.pSuccess("'projects.$arg.subpath' set to '$opt'.")
echo()
}

aliasOpt?.let { opt ->
if (aliases == null) aliases = mutableSetOf()
aliases!!.add(opt)
terminal.pSuccess("'projects.$arg.aliases' add '$opt'.")
echo()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package teksturepako.pakku.api.projects

import io.mockk.every
import io.mockk.mockk
import kotlin.test.Test
import kotlin.test.assertTrue

class ProjectTest
{
@Test
fun hasAliasOf_whenProjectHasAlias_returnsTrue()
{
val project1 = mockk<Project>() {
every { id } returns mutableMapOf("id1" to "id1")
every { name } returns mutableMapOf("name1" to "name1")
every { slug } returns mutableMapOf("slug1" to "slug1")
every { aliases } returns mutableSetOf("id3")
every { hasAliasOf(any()) } answers { callOriginal() }
}
val project2 = mockk<Project>() {
every { id } returns mutableMapOf("id2" to "id2")
every { name } returns mutableMapOf("name2" to "name2")
every { slug } returns mutableMapOf("slug2" to "slug2")
every { aliases } returns mutableSetOf("name1")
every { hasAliasOf(any()) } answers { callOriginal() }
}
val project3 = mockk<Project>() {
every { id } returns mutableMapOf("id3" to "id3")
every { name } returns mutableMapOf("name3" to "name3")
every { slug } returns mutableMapOf("slug3" to "slug3")
every { aliases } returns mutableSetOf("slug2")
every { hasAliasOf(any()) } answers { callOriginal() }
}

assertTrue(project1 hasAliasOf project3)
assertTrue(project2 hasAliasOf project1)
assertTrue(project3 hasAliasOf project2)
}
}