Skip to content

Commit

Permalink
Refactor ExportingScope
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-hrivnak committed Feb 2, 2025
1 parent bd6d209 commit 7771af7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,31 @@ class ExportProfileBuilder(
private val requiresPlatform: Platform? = null,
private val builder: (ExportProfileBuilder.() -> Unit),
private var rules: Sequence<ExportRule> = sequenceOf(),
) : ExportingScope
) : ExportRuleScope
{
override lateinit var lockFile: LockFile
override lateinit var configFile: ConfigFile

fun build(exportingScope: ExportingScope): ExportProfile
fun build(exportingScope: ExportRuleScope): ExportProfile
{
lockFile = exportingScope.lockFile
configFile = exportingScope.configFile
this.lockFile = exportingScope.lockFile
this.configFile = exportingScope.configFile

this.rules = emptySequence()
this.apply(builder)
// Don't mutate this class
val builderCopy = ExportProfileBuilder(name, fileExtension, requiresPlatform, builder, rules)

debug { println("Building [$name profile]") }
debug { println("[$name profile] has ${rules.toList().size} rule(s)") }
debug { println("Building [${builderCopy.name} profile]") }
debug { println("[${builderCopy.name} profile] has ${builderCopy.rules.toList().size} rule(s)") }

return ExportProfile(name, fileExtension, rules.toList(), requiresPlatform)
return ExportProfile(
builderCopy.name, builderCopy.fileExtension, builderCopy.rules.toList(), builderCopy.requiresPlatform
)
}

// -- AFTER BUILD --

/** Adds an export rule to the profile. */
fun rule(exportRule: (ExportingScope) -> ExportRule): ExportRule
fun rule(exportRule: (ExportRuleScope) -> ExportRule): ExportRule
{
val rule = exportRule(this)

Expand All @@ -82,7 +84,7 @@ class ExportProfileBuilder(
}

/** Adds an optional export rule to the profile. */
fun optionalRule(exportRule: (ExportingScope) -> ExportRule?): ExportRule?
fun optionalRule(exportRule: (ExportRuleScope) -> ExportRule?): ExportRule?
{
val rule = exportRule(this)

Expand All @@ -95,7 +97,7 @@ class ExportProfileBuilder(
}

/** Provides a fallback mechanism when an optional rule is null. */
infix fun ExportRule?.orElse(exportRule: (ExportingScope) -> ExportRule?): ExportRule?
infix fun ExportRule?.orElse(exportRule: (ExportRuleScope) -> ExportRule?): ExportRule?
{
if (this == null)
{
Expand All @@ -112,7 +114,7 @@ class ExportProfileBuilder(
}
}

interface ExportingScope
interface ExportRuleScope
{
/** The lock file associated with the exporting scope. */
val lockFile: LockFile
Expand All @@ -121,7 +123,7 @@ interface ExportingScope
val configFile: ConfigFile
}

fun exportingScope(lockFile: LockFile, configFile: ConfigFile): ExportingScope = object : ExportingScope
fun exportingScope(lockFile: LockFile, configFile: ConfigFile): ExportRuleScope = object : ExportRuleScope
{
override val lockFile: LockFile = lockFile
override val configFile: ConfigFile = configFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package teksturepako.pakku.api.actions.export.rules
import teksturepako.pakku.api.actions.errors.ActionError
import teksturepako.pakku.api.actions.errors.ErrorSeverity
import teksturepako.pakku.api.actions.export.ExportRule
import teksturepako.pakku.api.actions.export.ExportingScope
import teksturepako.pakku.api.actions.export.ExportRuleScope
import teksturepako.pakku.api.actions.export.Packaging
import teksturepako.pakku.api.actions.export.RuleContext.*
import teksturepako.pakku.api.actions.export.ruleResult
Expand All @@ -23,7 +23,7 @@ data object RequiresMcVersion : ActionError()
override val severity = ErrorSeverity.FATAL
}

fun ExportingScope.cfModpackRule(): ExportRule
fun ExportRuleScope.cfModpackRule(): ExportRule
{
val modpackModel = lockFile.getFirstMcVersion()?.let { mcVersion ->
createCfModpackModel(mcVersion, lockFile, configFile)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package teksturepako.pakku.api.actions.export.rules

import teksturepako.pakku.api.actions.export.ExportRule
import teksturepako.pakku.api.actions.export.ExportingScope
import teksturepako.pakku.api.actions.export.ExportRuleScope
import teksturepako.pakku.api.actions.export.Packaging
import teksturepako.pakku.api.actions.export.RuleContext.*
import teksturepako.pakku.api.actions.export.ruleResult
Expand All @@ -17,7 +17,7 @@ import teksturepako.pakku.api.projects.ProjectSide
import teksturepako.pakku.io.createHash
import teksturepako.pakku.io.readPathBytesOrNull

fun ExportingScope.mrModpackRule(): ExportRule
fun ExportRuleScope.mrModpackRule(): ExportRule
{
val modpackModel = lockFile.getFirstMcVersion()?.run {
createMrModpackModel(this, lockFile, configFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import net.thauvin.erik.urlencoder.UrlEncoderUtil
import teksturepako.pakku.api.actions.errors.ActionError
import teksturepako.pakku.api.actions.errors.NoFiles
import teksturepako.pakku.api.actions.export.ExportRule
import teksturepako.pakku.api.actions.export.ExportingScope
import teksturepako.pakku.api.actions.export.ExportRuleScope
import teksturepako.pakku.api.actions.export.Packaging
import teksturepako.pakku.api.actions.export.RuleContext.Finished
import teksturepako.pakku.api.actions.export.RuleContext.MissingProject
Expand All @@ -33,7 +33,7 @@ data class FileDirectorModel(
)
}

fun ExportingScope.fileDirectorRule(
fun ExportRuleScope.fileDirectorRule(
excludedProviders: Set<Provider> = setOf(),
fileDirectorModel: FileDirectorModel = FileDirectorModel()
): ExportRule?
Expand Down

0 comments on commit 7771af7

Please sign in to comment.