Index fixes and performance improvements (fixes #223, #224)

- Fixed creation of duplicate index entries when importing from CurseForge (fixes #224)
- Automatically remove duplicates in index
- Fixed `packwiz serve` with a custom `--pack-root` argument (fixes #223)
- Fixed `packwiz serve` with a custom index.toml location
- Cleaned up internal serving code, added comments and better errors
- Refactored path handling code
- Improved refreshing/exporting performance
- Factored out duplicated exporting logic
- Replaced GetAllMods calls with cleaner LoadAllMods calls and made the former private
- Improved variable names in update command
- Improved handling of aliassed files
- Changed CheckUpdate to take references to metadata
- Removed the ability to use an absolute path to the index file (that probably didn't work anyway)
- Behaviour change: order of entries in exported files may be random
This commit is contained in:
comp500
2023-05-29 23:15:55 +01:00
parent d25817273b
commit 4fb1f1b59d
14 changed files with 401 additions and 352 deletions

View File

@@ -9,7 +9,6 @@ import (
"golang.org/x/exp/slices"
"net/url"
"os"
"path/filepath"
"strconv"
"github.com/packwiz/packwiz/core"
@@ -55,9 +54,6 @@ var exportCmd = &cobra.Command{
return
}
// TODO: should index just expose indexPath itself, through a function?
indexPath := filepath.Join(filepath.Dir(viper.GetString("pack-file")), filepath.FromSlash(pack.Index.File))
fmt.Println("Reading external files...")
mods, err := index.LoadAllMods()
if err != nil {
@@ -113,15 +109,13 @@ var exportCmd = &cobra.Command{
fmt.Printf("Warning for %s (%s): %v\n", dl.Mod.Name, dl.Mod.FileName, warning)
}
pathForward, err := filepath.Rel(filepath.Dir(indexPath), dl.Mod.GetDestFilePath())
path, err := index.RelIndexPath(dl.Mod.GetDestFilePath())
if err != nil {
fmt.Printf("Error resolving external file: %s\n", err.Error())
// TODO: exit(1)?
continue
}
path := filepath.ToSlash(pathForward)
hashes := make(map[string]string)
hashes["sha1"] = dl.Hashes["sha1"]
hashes["sha512"] = dl.Hashes["sha512"]
@@ -170,11 +164,11 @@ var exportCmd = &cobra.Command{
fmt.Printf("%s (%s) added to manifest\n", dl.Mod.Name, dl.Mod.FileName)
} else {
if dl.Mod.Side == core.ClientSide {
_ = cmdshared.AddToZip(dl, exp, "client-overrides", indexPath)
_ = cmdshared.AddToZip(dl, exp, "client-overrides", &index)
} else if dl.Mod.Side == core.ServerSide {
_ = cmdshared.AddToZip(dl, exp, "server-overrides", indexPath)
_ = cmdshared.AddToZip(dl, exp, "server-overrides", &index)
} else {
_ = cmdshared.AddToZip(dl, exp, "overrides", indexPath)
_ = cmdshared.AddToZip(dl, exp, "overrides", &index)
}
}
}
@@ -233,29 +227,7 @@ var exportCmd = &cobra.Command{
os.Exit(1)
}
for _, v := range index.Files {
if !v.MetaFile {
// Save all non-metadata files into the zip
path, err := filepath.Rel(filepath.Dir(indexPath), index.GetFilePath(v))
if err != nil {
fmt.Printf("Error resolving file: %s\n", err.Error())
// TODO: exit(1)?
continue
}
file, err := exp.Create(filepath.ToSlash(filepath.Join("overrides", path)))
if err != nil {
fmt.Printf("Error creating file: %s\n", err.Error())
// TODO: exit(1)?
continue
}
err = index.SaveFile(v, file)
if err != nil {
fmt.Printf("Error copying file: %s\n", err.Error())
// TODO: exit(1)?
continue
}
}
}
cmdshared.AddNonMetafileOverrides(&index, exp)
err = exp.Close()
if err != nil {

View File

@@ -386,9 +386,12 @@ func getBestHash(v *modrinthApi.File) (string, string) {
func getInstalledProjectIDs(index *core.Index) []string {
var installedProjects []string
for _, modPath := range index.GetAllMods() {
mod, err := core.LoadMod(modPath)
if err == nil {
// Get modids of all mods
mods, err := index.LoadAllMods()
if err != nil {
fmt.Printf("Failed to determine existing projects: %v\n", err)
} else {
for _, mod := range mods {
data, ok := mod.GetParsedUpdateData("modrinth")
if ok {
updateData, ok := data.(mrUpdateData)

View File

@@ -35,7 +35,7 @@ type cachedStateStore struct {
Version *modrinthApi.Version
}
func (u mrUpdater) CheckUpdate(mods []core.Mod, pack core.Pack) ([]core.UpdateCheck, error) {
func (u mrUpdater) CheckUpdate(mods []*core.Mod, pack core.Pack) ([]core.UpdateCheck, error) {
results := make([]core.UpdateCheck, len(mods))
for i, mod := range mods {