Skip to content

Commit

Permalink
rework manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
maradotwebp committed Apr 17, 2022
1 parent 7aefee1 commit 175a061
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 119 deletions.
38 changes: 18 additions & 20 deletions src/modpack/manifest.nim
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
## Defines methods for interacting with a curseforge manifest file.
##
## In the CF modpack format, modpack information & mods/resourcepacks are tracked within
## a specific file known as the `manifest.json`. Each addon is tracked with a given projectid (identifies the addon)
## and a file id (identifies the version of the addon.)
## In the CF modpack format, modpack information & mods/resourcepacks are tracked within a specific file
## known as the `manifest.json`. Each addon is tracked with a given projectid (identifies the addon)
## and a file id (identifies the version of the addon).

import algorithm, asyncdispatch, json, options, os, sequtils, strformat, sugar
import std/[algorithm, asyncdispatch, json, options, os, sequtils, sugar]
import loader
import ../api/cfclient, ../api/cfcore
import ../api/[cfclient, cfcore]
import ../modpack/version
import ../term/color, ../term/log
export color
export log
import ../term/[color, log]
export color, log

type
ManifestMetadata* = ref object
Expand Down Expand Up @@ -48,7 +47,6 @@ const
tempPackFolder* = projectFolder / "temppack/"
overridesFolder* = packFolder / "overrides/"
paxFile* = overridesFolder / ".pax"
manifestFile* = packFolder / "manifest.json"
outputFolder* = projectFolder / ".out/"

proc initManifestMetadata*(name: string, explicit: bool, pinned: bool, dependencies: seq[int]): ManifestMetadata =
Expand Down Expand Up @@ -160,7 +158,7 @@ proc removeAddon*(manifest: var Manifest, projectId: int): ManifestFile =
## remove a mod from the project with the given `projectId`, returns removed mod.
for i, file in manifest.files:
if file.projectId == projectId:
manifest.files.delete(i, i)
manifest.files.delete(i..i)
return file

proc updateAddon*(manifest: var Manifest, file: ManifestFile): void =
Expand All @@ -174,20 +172,20 @@ proc updateAddon*(manifest: var Manifest, projectId: int, fileId: int): void =
modToUpdate.fileId = fileId
installAddon(manifest, modToUpdate)

template isPaxProject*: bool =
template isPaxProject*(path = packFolder): bool =
## returns true if the current folder is a pax project folder
fileExists(manifestFile)
fileExists(path / "manifest.json")

template requirePaxProject*: void =
template requirePaxProject*(path = packFolder): void =
## will error if the current folder isn't a pax project
if not isPaxProject():
if not isPaxProject(path):
echoError "The current folder isn't a pax project."
echoClr indentPrefix, "To initialize a pax project, enter ".fgRed, "pax init"
return

template rejectPaxProject*: void =
template rejectPaxProject*(path = packFolder): void =
## will error if the current folder is a pax project
if isPaxProject:
if isPaxProject(path):
echoError "The current folder is already a pax project."
echoClr indentPrefix, "If you are sure you want to overwrite existing files, use the ", "--force".fgRed, " option"
return
Expand All @@ -198,10 +196,10 @@ template rejectInstalledAddon*(manifest: Manifest, projectId: int): void =
echoError "This mod is already installed."
return

proc readManifestFromDisk*(path = manifestFile): Manifest =
proc readManifestFromDisk*(path = packFolder): Manifest =
## get a Manifest from disk (with `path` as the path)
return readFile(path).parseJson.toManifest
return readFile(path / "manifest.json").parseJson.toManifest

proc writeToDisk*(manifest: Manifest, path = manifestFile): void =
proc writeToDisk*(manifest: Manifest, path = packFolder): void =
## write `manifest` to disk (with `path` as the path)
writeFile(path, manifest.toJson.pretty)
writeFile(path / "manifest.json", manifest.toJson.pretty)
56 changes: 56 additions & 0 deletions tests/modpack/tmanifest.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import modpack/[loader, manifest, version]

block: # manifest loader
let m = Manifest()
m.name = "testmodpack"
m.author = "testauthor"
m.version = "1.0.0"
m.mcVersion = "1.16.3".Version

m.mcModloaderId = "fabric-0.11.0"
doAssert m.loader == Loader.Fabric

m.mcModloaderId = "forge-34.1.0"
doAssert m.loader == Loader.Forge

let file = initManifestFile(
projectId = 111,
fileId = 200,
initManifestMetadata(
name = "test",
explicit = true,
pinned = false,
dependencies = @[]
)
)

block: # manifest mods
var m = Manifest()
m.name = "testmodpack"
m.author = "testauthor"
m.version = "1.0.0"
m.mcVersion = "1.16.3".Version

doAssert m.files.len == 0

block: # install
m.installAddon(file)
doAssert m.files.len == 1
doAssert m.files[0].projectId == 111
doAssert m.files[0].fileId == 200

block: # update with project & file id
m.updateAddon(111, 300)
doAssert m.files.len == 1
doAssert m.files[0].projectId == 111
doAssert m.files[0].fileId == 300

block: # update with file
m.updateAddon(file)
doAssert m.files.len == 1
doAssert m.files[0].projectId == 111
doAssert m.files[0].fileId == 300

block: # remove
discard m.removeAddon(111)
doAssert m.files.len == 0
53 changes: 0 additions & 53 deletions tests/modpack/tmanifest/tmanifest.nim

This file was deleted.

20 changes: 0 additions & 20 deletions tests/modpack/tmanifest/tmanifest_disk.nim

This file was deleted.

14 changes: 14 additions & 0 deletions tests/modpack/tmanifest_disk.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import std/[json, tempfiles, os]
import modpack/[manifest, version]

block: # manifest write / read from disk
let m = Manifest()
m.name = "testmodpack"
m.author = "testauthor"
m.version = "1.0.0"
m.mcVersion = "1.16.3".Version

let tmpdir = createTempDir("", "")
m.writeToDisk(path = tmpdir)
let readM = readManifestFromDisk(path = tmpdir)
doAssert m.toJson == readM.toJson
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
discard """
joinable: false
batchable: false
"""
import std/[os, tempfiles]
import modpack/[loader, manifest, version]

import os
import modpack/loader, modpack/manifest, modpack/version

block: # rejectInstalledMod
block: # rejectInstalledAddon
let m = Manifest()
m.name = "testmodpack"
m.author = "testauthor"
Expand All @@ -22,36 +17,31 @@ block: # rejectInstalledMod
doAssert f(1234) == -1
doAssert f(9999) == 1

block: # isPaxProject
let tmpdir = createTempDir("", "")

doAssert isPaxProject(tmpdir) == false
writeFile(tmpdir / "manifest.json", "")
doAssert isPaxProject(tmpdir) == true

block: # requirePaxProject
let tmpdir = createTempDir("", "")
let f = proc(): int =
result = -1
requirePaxProject()
requirePaxProject(tmpdir)
result = 1

removeDir(packFolder)

doAssert f() == -1

createDir(packFolder)
createDir(overridesFolder)
writeFile(manifestFile, "Hello pax test")
defer: removeDir(packFolder)

writeFile(tmpdir / "manifest.json", "")
doAssert f() == 1

block: # rejectPaxProject
let tmpdir = createTempDir("", "")
let f = proc(): int =
result = -1
rejectPaxProject()
rejectPaxProject(tmpdir)
result = 1

removeDir(packFolder)

doAssert f() == 1

createDir(packFolder)
createDir(overridesFolder)
writeFile(manifestFile, "Hello pax test")
defer: removeDir(packFolder)

writeFile(tmpdir / "manifest.json", "")
doAssert f() == -1

0 comments on commit 175a061

Please sign in to comment.