-
Notifications
You must be signed in to change notification settings - Fork 2
/
modBuildScript.go
311 lines (261 loc) · 9.07 KB
/
modBuildScript.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
this dont work at the moment
this dont work at the moment
this dont work at the moment
this dont work at the moment
use the sh-version
package main
import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
)
func main() {
// Extract the value of the mod_name variable from the mod.json file
modName, err := extractModName("mod.json")
if err != nil {
fmt.Println("Failed to extract mod name:", err)
return
}
fmt.Println("modName:", modName)
// Remove existing ${mod_name}_temp directory and create a new one
tempDir := "../" + modName + "_temp"
err = os.RemoveAll(tempDir)
if err != nil {
fmt.Println("Failed to remove existing temp directory:", err)
return
}
err = os.Mkdir(tempDir, 0755)
if err != nil {
fmt.Println("Failed to create temp directory:", err)
return
}
// Copy directories directly within the copy_dir_from directory to the ${mod_name}_temp directory
copyDirFrom := "/home/seeh/.local/share/0ad/mods/autocivp"
err = copyDirectories(copyDirFrom, tempDir)
if err != nil {
fmt.Println("Failed to copy directories:", err)
return
}
// Copy non-hidden files from the copy_dir_from directory to the mod_temp directory, excluding specific files
err = copyFiles(copyDirFrom, tempDir, []string{"exclude_file1.txt", "exclude_file2.txt"})
if err != nil {
fmt.Println("Failed to copy files:", err)
return
}
// Create a zip file (${mod_name}_temp.zip) for the mod by compressing the contents of the ${mod_name}_temp directory
tempZipFile := tempDir + ".zip"
err = createZip(tempDir, tempZipFile)
if err != nil {
fmt.Println("Failed to create temp zip file:", err)
return
}
// Create a second temp directory (autocivP_temp2) and copy the mod.json file and the ${mod_name}_temp.zip file into it
tempDir2 := "autocivP_temp2"
err = os.RemoveAll(tempDir2)
if err != nil {
fmt.Println("Failed to remove existing temp2 directory:", err)
return
}
err = os.Mkdir(tempDir2, 0755)
if err != nil {
fmt.Println("Failed to create temp2 directory:", err)
return
}
err = copyFile("mod.json", filepath.Join(tempDir2, "mod.json"))
if err != nil {
fmt.Println("Failed to copy mod.json:", err)
return
}
err = copyFile(tempZipFile, filepath.Join(tempDir2, tempZipFile))
if err != nil {
fmt.Println("Failed to copy temp zip file:", err)
return
}
// Create a zip file (${mod_name}.zip) for the mod by compressing the contents of the ${mod_name}_temp2 directory
finalZipFile := modName + ".zip"
err = createZip(tempDir2, finalZipFile)
if err != nil {
fmt.Println("Failed to create final zip file:", err)
return
}
/* Open a web browser (Firefox) with a specific URL related to the mod
err = openBrowser("https://example.com/" + modName)
if err != nil {
fmt.Println("Failed to open web browser:", err)
return
}
*/
fmt.Println("Script completed successfully!")
}
func extractModName(jsonFile string) (string, error) {
// Implement the logic to extract the mod name from the JSON file using your preferred method/library
return "autocivp", nil
}
func copyDirectories(srcDir, destDir string) error {
// Open the source directory
src, err := os.Open(srcDir)
if err != nil {
return fmt.Errorf("failed to open source directory: %w", err)
}
defer src.Close()
// Create the destination directory
err = os.MkdirAll(destDir, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create destination directory: %w", err)
}
// Get the list of directory entries in the source directory
entries, err := src.Readdir(-1)
if err != nil {
return fmt.Errorf("failed to read source directory: %w", err)
}
// Iterate over the directory entries
for _, entry := range entries {
// Check if the entry is a directory
if entry.IsDir() {
// Create the corresponding directory in the destination directory
err := os.MkdirAll(filepath.Join(destDir, entry.Name()), os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create directory in destination: %w", err)
}
// Recursively copy the subdirectories
err = copyDirectories(filepath.Join(srcDir, entry.Name()), filepath.Join(destDir, entry.Name()))
if err != nil {
return fmt.Errorf("failed to copy subdirectory: %w", err)
}
}
}
return nil
}
func copyFiles(copyDirFrom, copyDirTo string, excludeFiles []string) error {
// Open the source directory
src, err := os.Open(copyDirFrom)
if err != nil {
return fmt.Errorf("failed to open source directory: %w", err)
}
defer src.Close()
// Create the destination directory
err = os.MkdirAll(copyDirTo, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create destination directory: %w", err)
}
// Get the list of directory entries in the source directory
entries, err := src.Readdir(-1)
if err != nil {
return fmt.Errorf("failed to read source directory: %w", err)
}
// Iterate over the directory entries
for _, entry := range entries {
// Check if the entry is a file
if entry.Mode().IsRegular() {
// Check if the file should be excluded
exclude := false
for _, excludeFile := range excludeFiles {
if entry.Name() == excludeFile {
exclude = true
break
}
}
if !exclude {
// Open the source file
srcFile, err := os.Open(filepath.Join(copyDirFrom, entry.Name()))
if err != nil {
return fmt.Errorf("failed to open source file: %w", err)
}
defer srcFile.Close()
// Create the destination file
dstFile, err := os.Create(filepath.Join(copyDirTo, entry.Name()))
if err != nil {
return fmt.Errorf("failed to create destination file: %w", err)
}
defer dstFile.Close()
// Copy the contents from source to destination
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return fmt.Errorf("failed to copy file contents: %w", err)
}
}
}
}
return nil
}
func createZip(sourceDir, destinationFile string) error {
// Create a new zip file
zipFile, err := os.Create(destinationFile)
if err != nil {
return fmt.Errorf("failed to create zip file: %w", err)
}
defer zipFile.Close()
// Create a new zip writer
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
// Walk through the source directory and add files to the zip
err = filepath.Walk(sourceDir, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip directories
if info.IsDir() {
return nil
}
// Open the source file
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open source file: %w", err)
}
defer file.Close()
// Create a new file in the zip archive
zipEntry, err := zipWriter.Create(filePath)
if err != nil {
return fmt.Errorf("failed to create zip entry: %w", err)
}
// Copy the contents from source file to the zip entry
_, err = io.Copy(zipEntry, file)
if err != nil {
return fmt.Errorf("failed to copy file contents to zip: %w", err)
}
return nil
})
if err != nil {
return fmt.Errorf("failed to walk through source directory: %w", err)
}
return nil
}
func copyFile(sourcePath, destinationPath string) error {
// Open the source file
sourceFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("failed to open source file: %w", err)
}
defer sourceFile.Close()
// Create the destination file
destinationFile, err := os.Create(destinationPath)
if err != nil {
return fmt.Errorf("failed to create destination file: %w", err)
}
defer destinationFile.Close()
// Copy the contents from source to destination
_, err = io.Copy(destinationFile, sourceFile)
if err != nil {
return fmt.Errorf("failed to copy file contents: %w", err)
}
return nil
}
func openBrowser(url string) error {
/*
* var err error
* switch runtime.GOOS {
case "darwin":
err = exec.Command("open", url).Start()
case "windows":
err = exec.Command("cmd", "/c", "start", url).Start()
default:
err = exec.Command("xdg-open", url).Start()
}
if err != nil {
return fmt.Errorf("failed to open browser: %w", err)
}
*/
return nil
}