Skip to content

Commit

Permalink
enable Id generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryex committed Jul 16, 2020
1 parent 3c8a47c commit 5f8701d
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 10 deletions.
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,34 @@

Command-line utilities to pack and unpack custom assets for Dungeondraft

The pack tool does not on its own generate an ID for your package so it must have been pack at least once by Dungeondraft itself and have a valid `pack.json`
The pack tool by default does not on its own generate an ID for your package.

Windows executables are provided so you don't have to build it yourself.
So, it must have been pack at least once by Dungeondraft itself and have a valid `pack.json`
OR
A `pack.json` must be created by using the flags to pass in th pack name and version with an optional author.

Windows executable are provided so you don't have to build it yourself.

### Usage:
```dungeondraft-unpack.exe -h```
will show the help

```dungeondraft-unpack.exe [args] <.dungeondraft_pack file> <dest folder>```
The assets contained in the `.dungeondraft_pack` file will be written to a folder the same name as the package under the dest folder
The assets contained in the `.dungeondraft_pack` file will be written to a folder the same name as the package under the dest folder.

```dungeondraft-pack [args] <input folder> <dest folder>```
The assets in the input folder (provided there is a valid pack.json) will be written to a `<packname>.dungeondraft_pack` file in the destination directory
The assets in the input folder (provided there is a valid `pack.json`) will be written to a `<packname>.dungeondraft_pack` file in the destination directory.

```dungeondraft-pack [args] -G [-E] -N <packname> -V <version> [-A <author>] <input folder>```
A valid `pack.json` with a new id and the provided values will be created in the input directory (-E overwrites an existing `pack.json`).
The packer will then exit.

```dungeondraft-pack [args] [-E] -N <packname> -V <version> [-A <author>] <input folder> <dest folder>```
A valid `pack.json` with a new id and the provided values will be created in the input directory (-E overwrites an existing `pack.json`).
Then the assets in the input folder will be written to a `<packname>.dungeondraft_pack` file in the destination directory.

### If You Have Issues

If you have issues like the packager not picking up files, try passing in teh `-v` and `-vv` flags to get info and debug output. to makes sure there isn't a structural problem with your package folder.

If you can't fid the problem file an issue with the `-vv` debug output.
60 changes: 56 additions & 4 deletions cmd/dungeondraft-pack/dungeondraft-pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,26 @@ import (
)

const usageText = `Desc:
Packs the contesnts of a directory to a .dungeondraft_pack file, there must be a valid pack.json in the direcotry
Packs the contents of a directory to a .dungeondraft_pack file, there must be a valid pack.json in the direcotry
Usage:
dungeondraft-pack [args] <input folder> <dest folder>
By default this program requires a pack.json to exist, it must either be created by dungeondraft or this program.
To create the pack.json use the -name (-N), -author (-A), and -version (-V) to set it's fields.
a new pack ID will be generated every time these options are passed.
if a pack.json already exists this will fail unless you pass -editpack (-E).
all values of the existing pack.json will be overwrites, including the author if it is left blank.
If you only wish to generate the pack.json and not package the folder pass pass the -genpack (-G) flag.
<dest folder> becomes optional and will be ignored in this case
- if a package name, author, or version are specified; then package name and version can not be blank
- passing in the name, author, and version will *ALWAYS* generate a new ID
- by default the pack.json will not be over writted, pass -E | -editpack to do so
- by default the pack in the dest folder will not be over written, pass -O | -overwrite to do so
Arguments:
`

Expand All @@ -32,12 +49,34 @@ func main() {
overwritePtr := flag.Bool("overwrite", false, "overwrite output files at dest")
flag.BoolVar(overwritePtr, "O", false, "alias of -overwrite")

packNamePtr := flag.String("name", "", "pack the package with the given name")
flag.StringVar(packNamePtr, "N", "", "alias of -name")

packAuthorPtr := flag.String("author", "", "pack the package with the given author")
flag.StringVar(packAuthorPtr, "A", "", "alias of -author")

packVersionPtr := flag.String("version", "", "pack the package with the given version")
flag.StringVar(packVersionPtr, "V", "", "alias of -version")

packEditPtr := flag.Bool("editpack", false, "overwrite the pack.json with the passed values")
flag.BoolVar(packEditPtr, "E", false, "alias of -editpack")

packGenPtr := flag.Bool("genpack", false, "write the pack.json and exit")
flag.BoolVar(packGenPtr, "G", false, "alias of -genpack")

flag.Parse()

debug := *debugPtr
info := *infoPtr
overwrite := *overwritePtr

packName := *packNamePtr
packAuthor := *packAuthorPtr
packVersion := *packVersionPtr
packEdit := *packEditPtr

packGen := *packGenPtr

var inDir, outDir string
if flag.NArg() < 1 {
fmt.Println("Error: Must provide a pack folder")
Expand All @@ -49,6 +88,8 @@ func main() {
inDir = strings.TrimSpace(strings.Trim(splits[0], `"`))
outDir = strings.TrimSpace(strings.Trim(splits[1], `"`))
// "\""
} else if packGen {
inDir = flag.Arg(0)
} else {
fmt.Println("Error: Must provide a output folder")
usage()
Expand All @@ -64,7 +105,7 @@ func main() {
}

outDirPath, err := filepath.Abs(outDir)
if err != nil {
if err != nil && !packGen {
fmt.Println("could not get absolute path for dest folder", pathErr)
}

Expand All @@ -88,8 +129,19 @@ func main() {
"path": packDirPath,
"outPackagePath": outDirPath,
})

packer, err := pack.NewPackerFromFolder(l, packDirPath)
var packer *pack.Packer
if packName != "" || packAuthor != "" || packVersion != "" || packGen {
if packName == "" || packVersion == "" {
l.Fatal("if a package name, author, or version are specified, or genpack is set; then package name and version can not be blank ")
}
packer, err = pack.NewPackerFolder(l, packDirPath, packName, packAuthor, packVersion, packEdit)
if packGen {
l.Info("pack.json created")
os.Exit(0)
}
} else {
packer, err = pack.NewPackerFromFolder(l, packDirPath)
}
if err != nil {
l.Fatal("could not build Packer")
}
Expand Down
84 changes: 82 additions & 2 deletions pkg/pack/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/ryex/dungeondraft-gopackager/internal/structures"
"github.com/ryex/dungeondraft-gopackager/internal/utils"
Expand Down Expand Up @@ -37,6 +39,84 @@ func DefaultValidExt() []string {
}
}

func GenPackID() string {
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))

charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

b := make([]byte, 8)

for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}

// NewPackerFromFolder builds a new Packer from a folder with a valid pack.json
func NewPackerFolder(log logrus.FieldLogger, folderPath string, name string, author string, version string, overwrite bool) (p *Packer, err error) {

folderPath, err = filepath.Abs(folderPath)
if err != nil {
return
}

dirExists := utils.DirExists(folderPath)
if !dirExists {
err = errors.New("directory does not exists")
log.WithError(err).WithField("path", folderPath).Error("can't package a non existent folder")
return
}

packJSONPath := filepath.Join(folderPath, `pack.json`)

packExists := utils.FileExists(packJSONPath)
if packExists {
if !overwrite {
err = errors.New("a pack.json already exists and overwrite is not enabled")
log.WithError(err).WithField("path", folderPath).Error("a pack.json already exists")
return
} else {
log.WithField("path", folderPath).Warn("Overwriting pack.json")
}
}

if name == "" {
err = errors.New("name field can not be empty")
log.WithError(err).Error("invalid pack info")
return
}

if version == "" {
err = errors.New("version field can not be empty")
log.WithError(err).Error("invalid pack info")
return
}

pack := structures.Package{
Name: name,
Author: author,
Version: version,
ID: GenPackID(),
}

packJSONBytes, err := json.MarshalIndent(&pack, "", " ")
if err != nil {
log.WithError(err).WithField("path", folderPath).WithField("packJSONPath", packJSONPath).Error("can't create pack.json")
return
}

err = ioutil.WriteFile(packJSONPath, packJSONBytes, 0644)
if err != nil {
log.WithError(err).WithField("path", folderPath).WithField("packJSONPath", packJSONPath).Error("can't write pack.json")
return
}

p = NewPacker(log.WithField("path", folderPath).WithField("id", pack.ID).WithField("name", pack.Name), pack.Name, pack.ID, folderPath)
return

}

// NewPackerFromFolder builds a new Packer from a folder with a valid pack.json
func NewPackerFromFolder(log logrus.FieldLogger, folderPath string) (p *Packer, err error) {

Expand All @@ -48,15 +128,15 @@ func NewPackerFromFolder(log logrus.FieldLogger, folderPath string) (p *Packer,
dirExists := utils.DirExists(folderPath)
if !dirExists {
err = errors.New("directory does not exists")
log.WithError(err).WithField("path", folderPath).Error("can't package a non exsistant folder")
log.WithError(err).WithField("path", folderPath).Error("can't package a non existent folder")
return
}

packJSONPath := filepath.Join(folderPath, `pack.json`)

packExists := utils.FileExists(packJSONPath)
if !packExists {
err = errors.New("no pack.json in directory, package with dungeondraft first")
err = errors.New("no pack.json in directory, generate one first.")
log.WithError(err).WithField("path", folderPath).Error("can't package without a pack.json")
return
}
Expand Down

0 comments on commit 5f8701d

Please sign in to comment.