-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (109 loc) · 3.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"archive/zip"
"errors"
"flag"
"fmt"
"gw2_markers_gen/categories"
"gw2_markers_gen/files"
"gw2_markers_gen/maps"
trailbuilder "gw2_markers_gen/trail_builder"
"gw2_markers_gen/utils"
"io/fs"
"log"
"os"
)
const DefaultPackageName = "ShellshotMarkerPack"
const buildPath = "build"
var srcDirectory string
// Custom install function
// Input: relative marker pack location (EX: build/MarkerPack.taco)
// This is run on marker pack build, and can be used to automate marker pack installation
// You can override this method with a local init file.
// See installer.go.example for example code (copy the file as "installer.go")
var installScript = func(packageFile string) {}
var preInstallScript = func(packageFile string) {}
func main() {
outputPackage := *flag.String("n", DefaultPackageName, "Output Package Name")
srcDirectory = *flag.String("s", outputPackage, "Package directory containing definition")
flag.Parse()
packageZipName := fmt.Sprintf("%s.taco", outputPackage)
outputZipPath := fmt.Sprintf("%s/%s", buildPath, packageZipName)
buildFolder := fmt.Sprintf("%s/%s/", buildPath, outputPackage)
preInstallScript(outputZipPath)
maps.SetValidation(validateFile)
categories.SetValidation(validateFile)
os.RemoveAll(buildPath)
os.Mkdir(buildPath, fs.ModePerm)
trailbuilder.CompileResources(srcDirectory)
packageCatagories, warnings, err := categories.Compile(fmt.Sprintf("%s/categories", srcDirectory))
if err != nil {
log.Println(err)
return
}
for _, w := range warnings {
log.Println(w)
}
packageMaps, warnings := maps.Compile(packageCatagories, fmt.Sprintf("%s/maps", srcDirectory))
for _, w := range warnings {
log.Println(w)
}
CopyAssets(fmt.Sprintf("%s/assets", srcDirectory), fmt.Sprintf("%s/assets", buildFolder))
files.Copy(fmt.Sprintf("%s/pack.lua", srcDirectory), fmt.Sprintf("%s/pack.lua", buildFolder))
categories.Save(packageCatagories, buildFolder)
maps.Save(packageMaps, buildFolder)
err = makeZip(buildFolder, outputZipPath)
if err != nil {
panic(err)
}
installScript(outputZipPath)
}
func makeZip(path string, dstfile string) error {
outFile, err := os.Create(dstfile)
if err != nil {
log.Println(err)
}
defer outFile.Close()
w := zip.NewWriter(outFile)
err = addFiles(w, path, "")
if err != nil {
return err
}
err = w.Close()
return err
}
func addFiles(w *zip.Writer, basePath, baseInZip string) error {
//fetch file list
files, err := os.ReadDir(basePath)
if err != nil {
return err
}
for _, file := range files {
if !file.IsDir() { //Write non-directory files to zip
dat, err := os.ReadFile(basePath + file.Name())
if err != nil {
return err
}
f, err := w.Create(baseInZip + file.Name())
if err != nil {
return err
}
_, err = f.Write(dat)
if err != nil {
return err
}
} else if file.IsDir() { //recurse on directories
newBase := basePath + file.Name() + "/"
addFiles(w, newBase, baseInZip+file.Name()+"/")
}
}
return nil
}
func validateFile(v string) string {
v = utils.Trim(v)
fname := fmt.Sprintf("%s/%s", srcDirectory, v)
if _, err := os.Stat(fname); errors.Is(err, os.ErrNotExist) {
return fmt.Sprintf("File %s not found", v)
}
return ""
}