Skip to content

Commit

Permalink
Refactor some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-hrivnak committed Oct 10, 2024
1 parent 50d30b0 commit b29f66b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/commonMain/kotlin/teksturepako/pakku/Debug.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ inline fun <T> T.debug(block: (T) -> Unit): T
}

@OptIn(ExperimentalContracts::class)
inline fun <T> Collection<T>.debugIf(
predicate: (Collection<T>) -> Boolean, block: (Collection<T>) -> Unit
): Collection<T>
inline fun <T> T.debugIf(
predicate: (T) -> Boolean, block: (T) -> Unit
): T
{
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import teksturepako.pakku.api.data.Dirs.cacheDir
import teksturepako.pakku.api.data.LockFile
import teksturepako.pakku.api.data.workingPath
import teksturepako.pakku.api.overrides.OverrideType
import teksturepako.pakku.api.overrides.filterOverrides
import teksturepako.pakku.api.overrides.readProjectOverrides
import teksturepako.pakku.api.platforms.Platform
import teksturepako.pakku.debug
Expand All @@ -22,6 +21,7 @@ import java.nio.file.Path
import kotlin.io.path.*
import kotlin.time.Duration
import kotlin.time.measureTimedValue
import com.github.michaelbull.result.fold as resultFold


suspend fun export(
Expand Down Expand Up @@ -74,7 +74,8 @@ suspend fun ExportProfile.export(

val inputDirectory = Path(cacheDir.pathString, this.name)

val results: List<RuleResult> = this.rules.filterNotNull().produceRuleResults(lockFile, configFile, this.name)
val results: List<RuleResult> = this.rules.filterNotNull()
.produceRuleResults({ onError(this, it) }, lockFile, configFile, this.name)

// Run export rules
val cachedPaths: List<Path> = results.resolveResults { onError(this, it) }.awaitAll().filterNotNull() +
Expand Down Expand Up @@ -265,18 +266,43 @@ suspend fun List<RuleResult>.finishResults(
* [RuleContext.MissingProject] and [RuleContext.Finished] are applied last.
*/
suspend fun List<ExportRule>.produceRuleResults(
onError: suspend (error: ActionError) -> Unit,
lockFile: LockFile, configFile: ConfigFile, workingSubDir: String
): List<RuleResult>
{
val results = this.fold(listOf<Pair<ExportRule, RuleContext>>()) { acc, rule ->
acc + lockFile.getAllProjects().map {
rule to RuleContext.ExportingProject(it, lockFile, configFile, workingSubDir)
} + configFile.getAllOverrides().map {
rule to RuleContext.ExportingOverride(it, OverrideType.OVERRIDE, lockFile, configFile, workingSubDir)
} + configFile.getAllServerOverrides().map {
rule to RuleContext.ExportingOverride(it, OverrideType.SERVER_OVERRIDE, lockFile, configFile, workingSubDir)
} + configFile.getAllClientOverrides().map {
rule to RuleContext.ExportingOverride(it, OverrideType.CLIENT_OVERRIDE, lockFile, configFile, workingSubDir)
} + configFile.getAllOverrides().mapNotNull { result ->
result.resultFold(
success = {
rule to RuleContext.ExportingOverride(it, OverrideType.OVERRIDE, lockFile, configFile, workingSubDir)
},
failure = {
onError(it)
null
}
)
} + configFile.getAllServerOverrides().mapNotNull { result ->
result.resultFold(
success = {
rule to RuleContext.ExportingOverride(it, OverrideType.SERVER_OVERRIDE, lockFile, configFile, workingSubDir)
},
failure = {
onError(it)
null
}
)
} + configFile.getAllClientOverrides().mapNotNull { result ->
result.resultFold(
success = {
rule to RuleContext.ExportingOverride(it, OverrideType.CLIENT_OVERRIDE, lockFile, configFile, workingSubDir)
},
failure = {
onError(it)
null
}
)
} + readProjectOverrides(configFile).map {
rule to RuleContext.ExportingProjectOverride(it, lockFile, configFile, workingSubDir)
}
Expand Down
26 changes: 19 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,13 +2,15 @@

package teksturepako.pakku.api.data

import com.github.michaelbull.result.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import teksturepako.pakku.api.overrides.filterOverrides
import teksturepako.pakku.api.actions.ActionError
import teksturepako.pakku.api.projects.ProjectSide
import teksturepako.pakku.api.projects.ProjectType
import teksturepako.pakku.api.projects.UpdateStrategy
import teksturepako.pakku.io.*
import java.nio.file.Path
import kotlin.io.path.Path

/**
Expand Down Expand Up @@ -98,9 +100,17 @@ data class ConfigFile(
this.overrides.clear()
}

fun getAllOverrides(): List<String> = filterOverrides(this.overrides.expandWithGlob(Path(workingPath)))
fun getAllServerOverrides(): List<String> = filterOverrides(this.serverOverrides.expandWithGlob(Path(workingPath)))
fun getAllClientOverrides(): List<String> = filterOverrides(this.clientOverrides.expandWithGlob(Path(workingPath)))
fun getAllOverrides(): List<Result<String, ActionError>> = this.overrides
.expandWithGlob(Path(workingPath))
.map { filterPath(it) }

fun getAllServerOverrides(): List<Result<String, ActionError>> = this.serverOverrides
.expandWithGlob(Path(workingPath))
.map { filterPath(it) }

fun getAllClientOverrides(): List<Result<String, ActionError>> = this.clientOverrides
.expandWithGlob(Path(workingPath))
.map { filterPath(it) }

// -- PROJECTS --

Expand Down Expand Up @@ -128,11 +138,13 @@ data class ConfigFile(

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

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

suspend fun write() = writeToFile(this, "$workingPath/$FILE_NAME", overrideText = true, format = json)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import teksturepako.pakku.api.data.Dirs.PAKKU_DIR
import teksturepako.pakku.api.data.workingPath
import teksturepako.pakku.api.projects.ProjectType
import teksturepako.pakku.debug
import teksturepako.pakku.debugIf
import teksturepako.pakku.io.tryOrNull
import java.io.File
import kotlin.io.path.Path
Expand Down Expand Up @@ -38,4 +39,6 @@ suspend fun readProjectOverrides(configFile: ConfigFile?): Set<ProjectOverride>
.awaitAll()
.filterNotNull()
.toSet()
.debug { println("ProjectOverrides = ${it.map { projectOverride -> projectOverride.path }}") }
.debugIf({ it.isNotEmpty() }) {
println("readProjectOverrides = ${it.map { projectOverride -> projectOverride.path }}")
}
3 changes: 2 additions & 1 deletion src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Export.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.terminal
import com.github.ajalt.mordant.terminal.danger
import com.github.michaelbull.result.getOrElse
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.runBlocking
import teksturepako.pakku.api.actions.ActionError.AlreadyExists
Expand Down Expand Up @@ -35,7 +36,7 @@ class Export : CliktCommand()
}

val configFile = ConfigFile.readToResult().getOrElse {
terminal.danger(it.message)
terminal.pError(it)
echo()
return@runBlocking
}
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Fetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Fetch : CliktCommand()
val configFile = if (ConfigFile.exists())
{
ConfigFile.readToResult().getOrElse {
terminal.danger(it.message)
terminal.pError(it)
echo()
return@runBlocking
}
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Status.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Status: CliktCommand()
}

val configFile = ConfigFile.readToResult().getOrElse {
terminal.danger(it.message)
terminal.pError(it)
echo()
null
}
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/teksturepako/pakku/io/Glob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ fun List<String>.expandWithGlob(inputPath: Path) = fold(listOf<String>()) { acc,
{
acc + inputPath.listDirectoryEntriesRecursive(glob).map { it.toString() }
}
}.debugIf({ it.isNotEmpty() }) { println("expandWithGlob = $it") }.toList()
}.debugIf({ it.isNotEmpty() }) { println("expandWithGlob = $it") }

0 comments on commit b29f66b

Please sign in to comment.