Add pack-root option (fixes #27)

This commit is contained in:
comp500 2021-06-04 02:10:29 +01:00
parent d6bed7ad4c
commit 8cbe7d4c61
7 changed files with 26 additions and 15 deletions

View File

@ -76,7 +76,8 @@ For use on servers, add the `-g` flag to disable the GUI and `-s server` to down
## Options
- Additional options can be configured in the `[options]` section of `pack.toml`, as follows:
- `mods-folder` The folder to save mod metadata files into, for the install commands
- `mods-folder` The folder to save mod metadata files into, for the install commands (relative to the pack root)
- `acceptable-game-versions` A list of additional Minecraft versions to accept when installing or updating mods
- `no-internal-hashes` If this is set to true, packwiz will not generate hashes of local files, to prevent merge conflicts and inconsistent hashes when using git/etc.
- `packwiz refresh --build` can be used in this mode to generate internal hashes for distributing the pack with packwiz-installer
- `packwiz refresh --build` can be used in this mode to generate internal hashes for distributing the pack with packwiz-installer
- `pack-root` A custom directory, containing the modpack files and optional .packwizignore file (defaults to the index file location)

View File

@ -153,7 +153,8 @@ func (in *Index) updateFile(path string) error {
// If the file has an extension of toml and is in the mods folder, set mod to true
absFileDir, err := filepath.Abs(filepath.Dir(path))
if err == nil {
absModsDir, err := filepath.Abs(viper.GetString("mods-folder"))
modsDir := filepath.Join(in.GetPackRoot(), viper.GetString("mods-folder"))
absModsDir, err := filepath.Abs(modsDir)
if err == nil {
if absFileDir == absModsDir && strings.HasSuffix(filepath.Base(path), ".toml") {
mod = true
@ -164,6 +165,14 @@ func (in *Index) updateFile(path string) error {
return in.updateFileHashGiven(path, "sha256", hashString, mod)
}
func (in Index) GetPackRoot() string {
packRoot := viper.GetString("pack-root")
if len(packRoot) == 0 {
packRoot = filepath.Dir(in.indexFile)
}
return packRoot
}
// Refresh updates the hashes of all the files in the index, and adds new files to the index
func (in *Index) Refresh() error {
// TODO: If needed, multithreaded hashing
@ -173,8 +182,7 @@ func (in *Index) Refresh() error {
pathPF, _ := filepath.Abs(viper.GetString("pack-file"))
pathIndex, _ := filepath.Abs(in.indexFile)
// TODO: A method of specifying pack root directory?
packRoot := filepath.Dir(viper.GetString("pack-file"))
packRoot := in.GetPackRoot()
ignoreExists := true
pathIgnore, _ := filepath.Abs(filepath.Join(packRoot, ".packwizignore"))
ignore, err := gitignore.NewFromFile(filepath.Join(packRoot, ".packwizignore"))

View File

@ -71,8 +71,8 @@ func LoadMod(modFile string) (Mod, error) {
}
// SetMetaName sets the mod metadata file from a given file name (to be put in the mods folder)
func (m *Mod) SetMetaName(metaName string) string {
m.metaFile = ResolveMod(metaName)
func (m *Mod) SetMetaName(metaName string, index Index) string {
m.metaFile = ResolveMod(metaName, index)
return m.metaFile
}

View File

@ -11,8 +11,9 @@ import (
const ModExtension = ".toml"
// ResolveMod returns the path to a mod file from it's name
func ResolveMod(modName string) string {
func ResolveMod(modName string, index Index) string {
// TODO: should this work for any metadata file?
fileName := strings.ToLower(strings.TrimSuffix(modName, ModExtension)) + ModExtension
return filepath.Join(viper.GetString("mods-folder"), fileName)
modsDir := filepath.Join(index.GetPackRoot(), viper.GetString("mods-folder"))
return filepath.Join(modsDir, fileName)
}

View File

@ -187,7 +187,7 @@ func createModFile(modInfo modInfo, fileInfo modFileInfo, index *core.Index) err
},
Update: updateMap,
}
path := modMeta.SetMetaName(modInfo.Slug)
path := modMeta.SetMetaName(modInfo.Slug, *index)
// If the file already exists, this will overwrite it!!!
// TODO: Should this be improved?

View File

@ -224,7 +224,8 @@ var importCmd = &cobra.Command{
}
// TODO: just use mods-folder directly? does texture pack importing affect this?
ref, err := filepath.Abs(filepath.Join(filepath.Dir(core.ResolveMod(modInfoValue.Slug)), fileInfo.FileName))
modFilePath := core.ResolveMod(modInfoValue.Slug, index)
ref, err := filepath.Abs(filepath.Join(filepath.Dir(modFilePath), fileInfo.FileName))
if err == nil {
referencedModPaths = append(referencedModPaths, ref)
}
@ -243,9 +244,9 @@ var importCmd = &cobra.Command{
}
successes = 0
indexFolder := filepath.Dir(filepath.Join(filepath.Dir(viper.GetString("pack-file")), filepath.FromSlash(pack.Index.File)))
packRoot := index.GetPackRoot()
for _, v := range filesList {
filePath := filepath.Join(indexFolder, filepath.FromSlash(v.Name()))
filePath := filepath.Join(packRoot, filepath.FromSlash(v.Name()))
filePathAbs, err := filepath.Abs(filePath)
if err == nil {
found := false

View File

@ -205,9 +205,9 @@ func installVersion(mod Mod, version Version, pack core.Pack) error {
}
var path string
if mod.Slug != "" {
path = modMeta.SetMetaName(mod.Slug)
path = modMeta.SetMetaName(mod.Slug, index)
} else {
path = modMeta.SetMetaName(mod.Title)
path = modMeta.SetMetaName(mod.Title, index)
}
// If the file already exists, this will overwrite it!!!