-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConfigHelper.go
106 lines (93 loc) · 2.3 KB
/
ConfigHelper.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
package main
import (
"archive/zip"
"encoding/json"
"io"
"os"
"path/filepath"
"strings"
)
func handleConfig(f *zip.File, baseDir string) {
rc, err := f.Open()
if err != nil {
panic(err)
}
content, err := io.ReadAll(rc)
if err != nil {
panic(err)
}
if err := os.MkdirAll(baseDir, 0755); err != nil {
panic(err)
}
config := parseLuaConfig(string(content))
description := config.Description
config.Description = ""
output, _ := json.MarshalIndent(config, "", " ")
configFilePath := filepath.Join(baseDir, "mfile")
err = os.WriteFile(configFilePath, output, 0644)
if err != nil {
panic(err)
}
readmeFilePath := filepath.Join(baseDir, "README.md")
err = os.WriteFile(readmeFilePath, []byte(description), 0644)
if err != nil {
panic(err)
}
}
func parseLuaConfig(content string) PackageConfig {
var config PackageConfig
var captureDescription bool
var descriptionBuilder strings.Builder
var outputFileFound bool
lines := strings.Split(content, "\n")
for _, line := range lines {
if captureDescription {
if strings.Contains(line, "]]") {
captureDescription = false
descriptionBuilder.WriteString(strings.Trim(line, "[\"]"))
config.Description = descriptionBuilder.String()
continue
}
descriptionBuilder.WriteString(line + "\n")
continue
}
if strings.Contains(line, "=") {
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
switch key {
case "mpackage":
config.Package = strings.Trim(value, "[\"]")
case "author":
config.Author = strings.Trim(value, "[\"]")
case "icon":
config.Icon = strings.Trim(value, "[\"]")
case "title":
config.Title = strings.Trim(value, "[\"]")
case "description":
if !captureDescription && strings.Contains(value, "]]") {
config.Description = strings.Trim(value, "[\"]")
continue
}
if strings.Contains(value, "[[") {
captureDescription = true
}
descriptionBuilder.WriteString(strings.Trim(value, "[\"]"))
case "outputFile":
outputFileFound = true
if strings.Trim(value, "[\"]") == "true" {
config.OutputFile = true
} else {
config.OutputFile = false
}
}
if !outputFileFound {
config.OutputFile = true
}
}
}
return config
}