Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-hrivnak committed Jan 4, 2025
2 parents a1ad3db + 330e9b6 commit 9cbc912
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 34 deletions.
16 changes: 9 additions & 7 deletions src/commonMain/kotlin/teksturepako/pakku/api/actions/Addition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import teksturepako.pakku.api.projects.Project
data class RequestHandlers(
val onError: suspend (error: ActionError) -> Unit,
val onSuccess: suspend (
project: Project, isRecommended: Boolean, isReplacing: Boolean, reqHandlers: RequestHandlers
project: Project, isRecommended: Boolean, replacing: Project?, reqHandlers: RequestHandlers
) -> Unit
)

suspend fun Project?.createAdditionRequest(
onError: suspend (error: ActionError) -> Unit,
onSuccess: suspend (
project: Project, isRecommended: Boolean, isReplacing: Boolean, reqHandlers: RequestHandlers
project: Project, isRecommended: Boolean, replacing: Project?, reqHandlers: RequestHandlers
) -> Unit,
lockFile: LockFile,
platforms: List<Platform>,
Expand All @@ -29,12 +29,14 @@ suspend fun Project?.createAdditionRequest(
var isRecommended = true

// Handle already added project
val isReplacing = if (lockFile.isProjectAdded(this))
val replacing = if (lockFile.isProjectAdded(this))
{
onError(AlreadyAdded(this))
if (lockFile.getProject(this)?.files == this.files) return else true
val existingProject = lockFile.getProject(this) ?: return onError(ProjNotFound())

onError(AlreadyAdded(existingProject))
if (existingProject.files == this.files) return else existingProject
}
else false
else null

// We do not have to check platform for GitHub only project
if (this.slug.keys.size > 1 || this.slug.keys.firstOrNull() != GitHub.serialName)
Expand Down Expand Up @@ -69,5 +71,5 @@ suspend fun Project?.createAdditionRequest(
isRecommended = false
}

onSuccess(this, isRecommended, isReplacing, RequestHandlers(onError, onSuccess))
onSuccess(this, isRecommended, replacing, RequestHandlers(onError, onSuccess))
}
19 changes: 14 additions & 5 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Add.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,30 @@ class Add : CliktCommand()
handleMissingProject(error, arg)
}
},
onSuccess = { project, isRecommended, isReplacing, reqHandlers ->
onSuccess = { project, isRecommended, replacing, reqHandlers ->
val projMsg = project.getFullMsg()
val promptMessage = if (!isReplacing) "add" to "added" else "replace" to "replaced"
val promptMessage = if (replacing == null)
{
"Do you want to add $projMsg?" to "$projMsg added"
}
else
{
val replacingMsg = replacing.getFullMsg()
"Do you want to replace $replacingMsg with $projMsg?" to
"$replacingMsg replaced with $projMsg"
}

if (terminal.ynPrompt("Do you want to ${promptMessage.first} $projMsg?", isRecommended))
if (terminal.ynPrompt(promptMessage.first, isRecommended))
{
if (!isReplacing) lockFile.add(project) else lockFile.update(project)
if (replacing == null) lockFile.add(project) else lockFile.update(project)
lockFile.linkProjectToDependents(project)

if (!noDepsFlag)
{
project.resolveDependencies(terminal, reqHandlers, lockFile, projectProvider, platforms)
}

terminal.pSuccess("$projMsg ${promptMessage.second}")
terminal.pSuccess(promptMessage.second)
}
},
lockFile, platforms, strict
Expand Down
19 changes: 14 additions & 5 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/AddPrj.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,30 @@ class AddPrj : CliktCommand("prj")
handleMissingProject(error)
}
},
onSuccess = { project, isRecommended, isReplacing, reqHandlers ->
onSuccess = { project, isRecommended, replacing, reqHandlers ->
val projMsg = project.getFullMsg()
val promptMessage = if (!isReplacing) "add" to "added" else "replace" to "replaced"
val promptMessage = if (replacing == null)
{
"Do you want to add $projMsg?" to "$projMsg added"
}
else
{
val replacingMsg = replacing.getFullMsg()
"Do you want to replace $replacingMsg with $projMsg?" to
"$replacingMsg replaced with $projMsg"
}

if (terminal.ynPrompt("Do you want to ${promptMessage.first} $projMsg?", isRecommended))
if (terminal.ynPrompt(promptMessage.first, isRecommended))
{
if (!isReplacing) lockFile.add(project) else lockFile.update(project)
if (replacing == null) lockFile.add(project) else lockFile.update(project)
lockFile.linkProjectToDependents(project)

if (!noDepsFlag)
{
project.resolveDependencies(terminal, reqHandlers, lockFile, projectProvider, platforms)
}

terminal.pSuccess("$projMsg ${promptMessage.second}")
terminal.pSuccess(promptMessage.second)
}
}, lockFile, platforms, strict
)
Expand Down
16 changes: 13 additions & 3 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Import.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,20 @@ class Import : CliktCommand()
onError = { error ->
if (error !is AlreadyAdded) terminal.pError(error)
},
onSuccess = { project, _, isReplacing, reqHandlers ->
val promptMessage = if (!isReplacing) "add" to "added" else "replace" to "replaced"
onSuccess = { project, _, replacing, reqHandlers ->
val projMsg = project.getFullMsg()
val promptMessage = if (replacing == null)
{
"Do you want to add $projMsg?" to "$projMsg added"
}
else
{
val replacingMsg = replacing.getFullMsg()
"Do you want to replace $replacingMsg with $projMsg?" to
"$replacingMsg replaced with $projMsg"
}

if (!isReplacing) lockFile.add(project) else lockFile.update(project)
if (replacing == null) lockFile.add(project) else lockFile.update(project)
lockFile.linkProjectToDependents(project)

if (depsFlag)
Expand Down
28 changes: 14 additions & 14 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,25 @@ class Sync : CliktCommand()
onError = { error ->
terminal.pError(error)
},
onSuccess = { project, isRecommended, isReplacing, _ ->
onSuccess = { project, isRecommended, replacing, _ ->
val projMsg = project.getFullMsg()
val promptMessage = if (!isReplacing) "add" to "added" else "replace" to "replaced"
val promptMessage = if (replacing == null)
{
"Do you want to add $projMsg?" to "$projMsg added"
}
else
{
val replacingMsg = replacing.getFullMsg()
"Do you want to replace $replacingMsg with $projMsg?" to
"$replacingMsg replaced with $projMsg"
}

if (terminal.ynPrompt("Do you want to ${promptMessage.first} $projMsg?", isRecommended))
if (terminal.ynPrompt(promptMessage.first, isRecommended))
{
if (!isReplacing) lockFile.add(project) else lockFile.update(project)
if (replacing == null) lockFile.add(project) else lockFile.update(project)
lockFile.linkProjectToDependents(project)

terminal.pSuccess("$projMsg ${promptMessage.second}")

project.getSubpath()?.onSuccess { subpath ->
configFile?.setProjectConfig(projectIn, lockFile) { slug ->
this.subpath = subpath
terminal.pSuccess("'projects.$slug.subpath' set to '$subpath'")
}
}?.onFailure { error ->
terminal.pError(error)
}
terminal.pSuccess(promptMessage.second)
}
},
lockFile, platforms
Expand Down

0 comments on commit 9cbc912

Please sign in to comment.