Skip to content

Commit 06fcdaa

Browse files
committed
First Commit
0 parents  commit 06fcdaa

File tree

5 files changed

+316
-0
lines changed

5 files changed

+316
-0
lines changed

README.MD

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#JSONUIHELPER
2+
3+
## English:
4+
5+
### This script allows you to view changes in a folder, then it changes the value in manifest.json and archives the folder, then copies the archive to the specified folder
6+
7+
### This script automatically changes the UUID in the manifest.json file
8+
9+
Example config.json
10+
11+
```json
12+
{
13+
"watchDir": "/path/to/path", //The folder that needs to be watched for changes
14+
"zipDir": "/path/to/path", //Folder where to copy the finished archive
15+
"jsonFile": "manifest.json", //Don't touch
16+
"zipFileName": "name_pack.zip",
17+
"zip": "true" // If you set it to false, the script simply copies the finished contents of the folder with the modified manifest.json
18+
}
19+
```
20+
21+
22+
## Русский:
23+
24+
### Этот скрипт позволяет просматривать изменения в папке, затем он изменяет значение в manifest.json и архивирует папку, затем копирует архив в указанную папку
25+
26+
27+
### Этот скрипт автоматически меняет UUID в файле manifest.json
28+
29+
Пример config.json
30+
31+
```json
32+
{
33+
"watchDir": "/path/to/path", //Папка, за которой нужно следить на предмет изменений
34+
"zipDir": "/path/to/path", //Папка, куда копировать готовый архив
35+
"jsonFile": "manifest.json", //Не трогать
36+
"zipFileName": "name_pack.zip",
37+
"zip": "true" // Если установить значение false, скрипт просто копирует готовое содержимое папки с измененным manifest.json
38+
}
39+
```

config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"watchDir": "/path/to/path",
3+
"zipDir": "/path/to/path",
4+
"jsonFile": "manifest.json",
5+
"zipFileName": "name_pack.zip",
6+
"zip": "true"
7+
}

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module JsonUI
2+
3+
go 1.23.0
4+
5+
require (
6+
github.com/fsnotify/fsnotify v1.7.0 // indirect
7+
github.com/google/uuid v1.6.0 // indirect
8+
golang.org/x/sys v0.4.0 // indirect
9+
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
2+
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
3+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5+
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
6+
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

main.go

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
package main
2+
3+
import (
4+
"archive/zip"
5+
"encoding/json"
6+
"github.com/fsnotify/fsnotify"
7+
"github.com/google/uuid"
8+
"io"
9+
"log"
10+
"os"
11+
"path/filepath"
12+
"time"
13+
)
14+
15+
type Config struct {
16+
WatchDir string `json:"watchDir"`
17+
ZipDir string `json:"zipDir"`
18+
JsonFile string `json:"jsonFile"`
19+
ZipFileName string `json:"zipFileName"`
20+
Zip bool `json:"zip"`
21+
}
22+
23+
var (
24+
config Config
25+
lastModifiedTime time.Time
26+
)
27+
28+
func main() {
29+
loadConfig()
30+
31+
if config.WatchDir == config.ZipDir {
32+
log.Fatal("Watch directory and zip directory cannot be the same.")
33+
}
34+
35+
watcher, err := fsnotify.NewWatcher()
36+
if err != nil {
37+
log.Fatal(err)
38+
}
39+
defer watcher.Close()
40+
41+
done := make(chan bool)
42+
go func() {
43+
for {
44+
select {
45+
case event, ok := <-watcher.Events:
46+
if !ok {
47+
return
48+
}
49+
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
50+
info, err := os.Stat(event.Name)
51+
if err != nil {
52+
log.Println("Error:", err)
53+
continue
54+
}
55+
if info.ModTime().After(lastModifiedTime) {
56+
log.Println("Modified file:", event.Name)
57+
updateJSONFile()
58+
if config.Zip {
59+
err := zipFolderContents(config.WatchDir, filepath.Join(config.ZipDir, config.ZipFileName))
60+
if err != nil {
61+
return
62+
}
63+
} else {
64+
err := copyFolderContents(config.WatchDir, config.ZipDir)
65+
if err != nil {
66+
return
67+
}
68+
}
69+
}
70+
}
71+
case err, ok := <-watcher.Errors:
72+
if !ok {
73+
return
74+
}
75+
log.Println("Error:", err)
76+
}
77+
}
78+
}()
79+
80+
err = watcher.Add(config.WatchDir)
81+
if err != nil {
82+
log.Fatal(err)
83+
}
84+
85+
err = filepath.Walk(config.WatchDir, func(path string, info os.FileInfo, err error) error {
86+
if err != nil {
87+
return err
88+
}
89+
if info.IsDir() {
90+
return watcher.Add(path)
91+
}
92+
return nil
93+
})
94+
if err != nil {
95+
log.Fatal(err)
96+
}
97+
98+
<-done
99+
}
100+
101+
func loadConfig() {
102+
file, err := os.Open("config.json")
103+
if err != nil {
104+
log.Fatal(err)
105+
}
106+
defer file.Close()
107+
108+
decoder := json.NewDecoder(file)
109+
err = decoder.Decode(&config)
110+
if err != nil {
111+
log.Fatal(err)
112+
}
113+
}
114+
115+
func updateJSONFile() {
116+
filePath := filepath.Join(config.WatchDir, config.JsonFile)
117+
file, err := os.ReadFile(filePath)
118+
if err != nil {
119+
log.Fatal(err)
120+
}
121+
122+
var jsonConfig struct {
123+
FormatVersion int `json:"format_version"`
124+
Header struct {
125+
Description string `json:"description"`
126+
Name string `json:"name"`
127+
UUID string `json:"uuid"`
128+
Version []int `json:"version"`
129+
MinEngineVersion []int `json:"min_engine_version"`
130+
} `json:"header"`
131+
Modules []struct {
132+
Description string `json:"description"`
133+
Type string `json:"type"`
134+
UUID string `json:"uuid"`
135+
Version []int `json:"version"`
136+
} `json:"modules"`
137+
}
138+
139+
err = json.Unmarshal(file, &jsonConfig)
140+
if err != nil {
141+
log.Fatal(err)
142+
}
143+
144+
jsonConfig.Header.UUID = uuid.New().String()
145+
for i := range jsonConfig.Modules {
146+
jsonConfig.Modules[i].UUID = uuid.New().String()
147+
}
148+
149+
updatedFile, err := json.MarshalIndent(jsonConfig, "", " ")
150+
if err != nil {
151+
log.Fatal(err)
152+
}
153+
154+
err = os.WriteFile(filePath, updatedFile, 0644)
155+
if err != nil {
156+
log.Fatal(err)
157+
}
158+
159+
lastModifiedTime = time.Now()
160+
}
161+
162+
func zipFolderContents(source, target string) error {
163+
zipfile, err := os.Create(target)
164+
if err != nil {
165+
return err
166+
}
167+
defer zipfile.Close()
168+
169+
archive := zip.NewWriter(zipfile)
170+
defer archive.Close()
171+
172+
filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
173+
if err != nil {
174+
return err
175+
}
176+
177+
if path == source {
178+
return nil
179+
}
180+
181+
header, err := zip.FileInfoHeader(info)
182+
if err != nil {
183+
return err
184+
}
185+
186+
header.Name, err = filepath.Rel(source, path)
187+
if err != nil {
188+
return err
189+
}
190+
191+
if info.IsDir() {
192+
header.Name += "/"
193+
} else {
194+
header.Method = zip.Deflate
195+
}
196+
197+
writer, err := archive.CreateHeader(header)
198+
if err != nil {
199+
return err
200+
}
201+
202+
if info.IsDir() {
203+
return nil
204+
}
205+
206+
file, err := os.Open(path)
207+
if err != nil {
208+
return err
209+
}
210+
defer file.Close()
211+
212+
_, err = io.Copy(writer, file)
213+
return err
214+
})
215+
216+
return err
217+
}
218+
219+
func copyFolderContents(source, target string) error {
220+
return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
221+
if err != nil {
222+
return err
223+
}
224+
225+
if path == source {
226+
return nil
227+
}
228+
229+
relPath, err := filepath.Rel(source, path)
230+
if err != nil {
231+
return err
232+
}
233+
234+
targetPath := filepath.Join(target, relPath)
235+
236+
if info.IsDir() {
237+
return os.MkdirAll(targetPath, info.Mode())
238+
}
239+
240+
sourceFile, err := os.Open(path)
241+
if err != nil {
242+
return err
243+
}
244+
defer sourceFile.Close()
245+
246+
targetFile, err := os.Create(targetPath)
247+
if err != nil {
248+
return err
249+
}
250+
defer targetFile.Close()
251+
252+
_, err = io.Copy(targetFile, sourceFile)
253+
return err
254+
})
255+
}

0 commit comments

Comments
 (0)