Skip to content

Commit

Permalink
add pinning mods functionality - closes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
maradotwebp committed Oct 23, 2021
1 parent 42d7ee8 commit d255b5f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/cmd/add.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ proc addDependencies(manifest: var Manifest, file: ManifestFile, strategy: strin
metadata = initManifestMetadata(
name = mcMod.read().get().name,
explicit = false,
pinned = false,
dependencies = mcModFile.dependencies
)
)
Expand Down Expand Up @@ -144,6 +145,7 @@ proc paxAdd*(input: string, noDepends: bool, strategy: string): void =
metadata = initManifestMetadata(
name = mcMod.name,
explicit = true,
pinned = false,
dependencies = mcModFile.dependencies
)
)
Expand Down
43 changes: 43 additions & 0 deletions src/cmd/pin.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import asyncdispatch, asyncfutures, options, os
import common
import ../api/cfcore, ../api/cfclient
import ../modpack/install, ../modpack/manifest
import ../term/log, ../term/prompt
import ../util/flow

proc paxPin*(name: string): void =
## pin/unpin an installed mod
requirePaxProject()

echoDebug "Loading data from manifest.."
var manifest = readManifestFromDisk()

echoDebug "Loading mods.."
let mcMods = waitFor(fetchAddonsByQuery(name))

echoDebug "Searching for mod.."
let mcModOption = manifest.promptAddonChoice(mcMods, selectInstalled = true)
if mcModOption.isNone:
echoError "No installed mods found for your search."
quit(1)
let mcMod = mcModOption.get()

echo ""
echoRoot "SELECTED MOD".dim
echoAddon(mcMod)
echo ""

let manifestFile = manifest.getFile(mcMod.projectId)

if manifestFile.metadata.pinned:
returnIfNot promptYN("Unpin this mod?", default = true)
echoInfo "Unpinning ", mcMod.name.fgCyan, ".."
else:
returnIfNot promptYN("Pin this mod to the current version?", default = true)
echoInfo "Removing ", mcMod.name.fgCyan, ".."

manifestFile.metadata.pinned = not manifestFile.metadata.pinned
updateAddon(manifest, manifestFile)

echoDebug "Writing to manifest..."
manifest.writeToDisk()
5 changes: 5 additions & 0 deletions src/cmd/update.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ proc paxUpdate*(name: string, strategy: string): void =

returnIfNot promptYN("Are you sure you want to update this mod?", default = true)

let manifestFile = manifest.getFile(mcMod.projectId)
if manifestFile.metadata.pinned:
echoError "Cannot update mod - ", mcMod.name.fgCyan, " is pinned."
return

echoDebug "Retrieving mod versions.."
let mcModFiles = waitFor(fetchAddonFiles(mcMod.projectId))

Expand Down
4 changes: 4 additions & 0 deletions src/cmd/upgrade.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ proc paxUpgrade*(strategy: string): void =
for pairs in modData:
let (mcMod, mcModFiles) = pairs
let mcModFile = mcModFiles.selectAddonFile(manifest, strategy)
let manifestFile = manifest.getFile(mcMod.get().projectId)
if manifestFile.metadata.pinned:
echoWarn mcMod.get().name.fgCyan, " is pinned. Skipping.."
continue
if mcModFile.isNone:
echoWarn mcMod.get().name.fgCyan, " does not have a compatible version. Skipping.."
continue
Expand Down
13 changes: 12 additions & 1 deletion src/modpack/manifest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type
## Metadata for a given project in a manifest.json
name*: string
explicit*: bool
pinned*: bool
dependencies*: seq[int]

ManifestFile* = ref object
Expand Down Expand Up @@ -50,11 +51,12 @@ const
manifestFile* = packFolder / "manifest.json"
outputFolder* = projectFolder / ".out/"

proc initManifestMetadata*(name: string, explicit: bool, dependencies: seq[int]): ManifestMetadata =
proc initManifestMetadata*(name: string, explicit: bool, pinned: bool, dependencies: seq[int]): ManifestMetadata =
## create a new manifest metadata object.
result = ManifestMetadata()
result.name = name
result.explicit = explicit
result.pinned = pinned
result.dependencies = dependencies

proc initManifestFile*(projectId: int, fileId: int, metadata: ManifestMetadata): ManifestFile =
Expand All @@ -81,10 +83,12 @@ proc toManifestFile(json: JsonNode): Future[ManifestFile] {.async.} =
)
result.metadata.name = addon.read().get().name
result.metadata.explicit = true
result.metadata.pinned = false
result.metadata.dependencies = addonFile.read().get().dependencies
else:
result.metadata.name = json["__meta"]["name"].getStr()
result.metadata.explicit = json["__meta"]{"explicit"}.getBool(true)
result.metadata.pinned = json["__meta"]{"pinned"}.getBool(false)
result.metadata.dependencies = json["__meta"]{"dependencies"}.getElems(@[]).map((x) => x.getInt())

converter toJson(file: ManifestFile): JsonNode {.used.} =
Expand All @@ -99,6 +103,8 @@ converter toJson(file: ManifestFile): JsonNode {.used.} =
}
if not file.metadata.explicit:
result["__meta"]["explicit"] = newJBool(false)
if file.metadata.pinned:
result["__meta"]["pinned"] = newJBool(true)
if file.metadata.dependencies.len > 0:
let dependencyJsonArray = newJArray()
for d in file.metadata.dependencies:
Expand Down Expand Up @@ -162,6 +168,11 @@ proc removeAddon*(manifest: var Manifest, projectId: int): ManifestFile =
manifest.files.delete(i, i)
return file

proc updateAddon*(manifest: var Manifest, file: ManifestFile): void =
## update a mod with the given `file`
discard removeAddon(manifest, file.projectId)
installAddon(manifest, file)

proc updateAddon*(manifest: var Manifest, projectId: int, fileId: int): void =
## update a mod with the given `projectId` to the given `fileId`
var modToUpdate = removeAddon(manifest, projectId)
Expand Down
18 changes: 17 additions & 1 deletion src/pax.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import therapist
import cmd/add, cmd/expo, cmd/impo, cmd/init, cmd/list, cmd/remove, cmd/update, cmd/upgrade, cmd/version
import cmd/add, cmd/expo, cmd/impo, cmd/init, cmd/list, cmd/pin, cmd/remove, cmd/update, cmd/upgrade, cmd/version
import term/color, term/prompt

let commonArgs = (
Expand Down Expand Up @@ -68,6 +68,16 @@ let removeCmd = (
help: newHelpArg()
)

let pinCmd = (
name: newStringArg(@["<name>"],
multi = true,
help = "name of the mod to pin"
),
yes: commonArgs.yes,
noColor: commonArgs.noColor,
help: newHelpArg()
)

let updateCmd = (
name: newStringArg(@["<name>"],
multi = true,
Expand Down Expand Up @@ -142,6 +152,10 @@ let spec = (
removeCmd,
help = "remove a mod from the modpack"
),
pin: newCommandArg(@["pin"],
pinCmd,
help = "pin/unpin a mod to its current version"
),
update: newCommandArg(@["update"],
updateCmd,
help = "update a specific mod"
Expand Down Expand Up @@ -186,6 +200,8 @@ elif spec.add.seen:
strategy = addCmd.strategy.value)
elif spec.remove.seen:
paxRemove(name = removeCmd.name.value, strategy = removeCmd.strategy.value)
elif spec.pin.seen:
paxPin(name = pinCmd.name.value)
elif spec.update.seen:
paxUpdate(name = updateCmd.name.value, strategy = updateCmd.strategy.value)
elif spec.upgrade.seen:
Expand Down

0 comments on commit d255b5f

Please sign in to comment.