This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repackager.go
160 lines (143 loc) · 3.84 KB
/
repackager.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"github.com/saracen/fastzip"
"io"
"log"
"os"
"path"
"path/filepath"
"regexp"
"strings"
)
type Repackager struct {
BuildDir string
ContainsLvoFiles bool
ContainsDeliFiles bool
ContainsSideloaderFiles bool
RequiresH3VRUtilities bool
}
func (rpkg *Repackager) Do(ps *PullService) {
baseDir := ps.BoneTomeMetadata.Asset.Name + "._build"
_, lstatErr := os.Lstat(baseDir)
if !(os.IsNotExist(lstatErr)) {
err := os.RemoveAll(baseDir)
if err != nil {
log.Fatal(err)
}
}
err := os.Mkdir(baseDir, 0775)
if err != nil {
log.Fatal(err)
}
rpkg.BuildDir = baseDir
files := rpkg.getFiles(ps.TempDirectory)
rpkg.moveFiles(files)
err = os.RemoveAll(ps.TempDirectory)
if err != nil {
log.Fatal(err)
}
}
func (rpkg *Repackager) moveFiles(files []string) {
fileNameRegexp := regexp.MustCompilePOSIX("[^/\\\\]+$")
lvoPath := path.Join("plugins", "LegacyVirtualObjects")
deliPath := path.Join("plugins", "DeliMods")
fileLocations := map[string][]string{
"Sideloader": {},
"plugins": {},
lvoPath: {},
deliPath: {},
}
for fileIndex := range files {
fileLower := strings.ToLower(files[fileIndex])
if strings.HasSuffix(fileLower, ".hotmod") || strings.HasSuffix(fileLower, ".h3mod") {
fileLocations["Sideloader"] = append(fileLocations["Sideloader"], files[fileIndex])
rpkg.ContainsSideloaderFiles = true
} else if strings.HasSuffix(fileLower, ".dll") {
fileLocations["plugins"] = append(fileLocations["plugins"], files[fileIndex])
} else if strings.HasSuffix(fileLower, ".deli") {
fileLocations[deliPath] = append(fileLocations[deliPath], files[fileIndex])
rpkg.ContainsDeliFiles = true
} else {
fileName := fileNameRegexp.FindString(files[fileIndex])
if !strings.Contains(fileName, ".") {
// LVO
fileLocations[lvoPath] = append(fileLocations[lvoPath], files[fileIndex])
rpkg.ContainsLvoFiles = true
} else {
if strings.HasSuffix(fileLower, ".manifest") {
nameWithoutExtension := fileName[:len(fileName)-len(".manifest")]
fileDir := files[fileIndex][:len(files[fileIndex])-len(fileName)]
_, osErr := os.Lstat(path.Join(fileDir, nameWithoutExtension))
if !os.IsNotExist(osErr) {
fileLocations[lvoPath] = append(fileLocations[lvoPath], files[fileIndex])
}
} else {
fileLocations["plugins"] = append(fileLocations["plugins"], files[fileIndex])
}
}
}
}
for k, v := range fileLocations {
if len(v) == 0 {
continue
}
err := os.MkdirAll(path.Join(rpkg.BuildDir, k), 0775)
if err != nil {
log.Fatal(err)
}
for fileIndex := range v {
// Get file name, stripping path and separators.
fileName := fileNameRegexp.FindString(v[fileIndex])
file, err := os.Create(path.Join(rpkg.BuildDir, k, fileName))
defer file.Close()
if err != nil {
log.Fatal(err)
}
reader, err := os.Open(v[fileIndex])
defer reader.Close()
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(file, reader)
if err != nil {
log.Fatal(err)
}
}
}
}
// https://stackoverflow.com/a/49196644
func (rpkg *Repackager) getFiles(dir string) []string {
var files []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
log.Fatal(err)
}
return files
}
func (rpkg *Repackager) ZipBuildContents(outFile string) {
w, err := os.Create(outFile)
if err != nil {
log.Fatal(err)
}
defer w.Close()
a, err := fastzip.NewArchiver(w, rpkg.BuildDir)
if err != nil {
log.Fatal(err)
}
defer a.Close()
files := make(map[string]os.FileInfo)
err = filepath.Walk(rpkg.BuildDir, func(pathname string, info os.FileInfo, err error) error {
files[pathname] = info
return nil
})
// Archive
if err = a.Archive(context.Background(), files); err != nil {
log.Fatal(err)
}
}