First Commit
This commit is contained in:
39
README.MD
Normal file
39
README.MD
Normal file
@@ -0,0 +1,39 @@
|
||||
#JSONUIHELPER
|
||||
|
||||
## English:
|
||||
|
||||
### 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
|
||||
|
||||
### This script automatically changes the UUID in the manifest.json file
|
||||
|
||||
Example config.json
|
||||
|
||||
```json
|
||||
{
|
||||
"watchDir": "/path/to/path", //The folder that needs to be watched for changes
|
||||
"zipDir": "/path/to/path", //Folder where to copy the finished archive
|
||||
"jsonFile": "manifest.json", //Don't touch
|
||||
"zipFileName": "name_pack.zip",
|
||||
"zip": "true" // If you set it to false, the script simply copies the finished contents of the folder with the modified manifest.json
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Русский:
|
||||
|
||||
### Этот скрипт позволяет просматривать изменения в папке, затем он изменяет значение в manifest.json и архивирует папку, затем копирует архив в указанную папку
|
||||
|
||||
|
||||
### Этот скрипт автоматически меняет UUID в файле manifest.json
|
||||
|
||||
Пример config.json
|
||||
|
||||
```json
|
||||
{
|
||||
"watchDir": "/path/to/path", //Папка, за которой нужно следить на предмет изменений
|
||||
"zipDir": "/path/to/path", //Папка, куда копировать готовый архив
|
||||
"jsonFile": "manifest.json", //Не трогать
|
||||
"zipFileName": "name_pack.zip",
|
||||
"zip": "true" // Если установить значение false, скрипт просто копирует готовое содержимое папки с измененным manifest.json
|
||||
}
|
||||
```
|
||||
7
config.json
Normal file
7
config.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"watchDir": "/path/to/path",
|
||||
"zipDir": "/path/to/path",
|
||||
"jsonFile": "manifest.json",
|
||||
"zipFileName": "name_pack.zip",
|
||||
"zip": "true"
|
||||
}
|
||||
9
go.mod
Normal file
9
go.mod
Normal file
@@ -0,0 +1,9 @@
|
||||
module JsonUI
|
||||
|
||||
go 1.23.0
|
||||
|
||||
require (
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
golang.org/x/sys v0.4.0 // indirect
|
||||
)
|
||||
6
go.sum
Normal file
6
go.sum
Normal file
@@ -0,0 +1,6 @@
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
|
||||
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
255
main.go
Normal file
255
main.go
Normal file
@@ -0,0 +1,255 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"encoding/json"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/google/uuid"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
WatchDir string `json:"watchDir"`
|
||||
ZipDir string `json:"zipDir"`
|
||||
JsonFile string `json:"jsonFile"`
|
||||
ZipFileName string `json:"zipFileName"`
|
||||
Zip bool `json:"zip"`
|
||||
}
|
||||
|
||||
var (
|
||||
config Config
|
||||
lastModifiedTime time.Time
|
||||
)
|
||||
|
||||
func main() {
|
||||
loadConfig()
|
||||
|
||||
if config.WatchDir == config.ZipDir {
|
||||
log.Fatal("Watch directory and zip directory cannot be the same.")
|
||||
}
|
||||
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer watcher.Close()
|
||||
|
||||
done := make(chan bool)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case event, ok := <-watcher.Events:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
|
||||
info, err := os.Stat(event.Name)
|
||||
if err != nil {
|
||||
log.Println("Error:", err)
|
||||
continue
|
||||
}
|
||||
if info.ModTime().After(lastModifiedTime) {
|
||||
log.Println("Modified file:", event.Name)
|
||||
updateJSONFile()
|
||||
if config.Zip {
|
||||
err := zipFolderContents(config.WatchDir, filepath.Join(config.ZipDir, config.ZipFileName))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
err := copyFolderContents(config.WatchDir, config.ZipDir)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case err, ok := <-watcher.Errors:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
log.Println("Error:", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
err = watcher.Add(config.WatchDir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = filepath.Walk(config.WatchDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return watcher.Add(path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
<-done
|
||||
}
|
||||
|
||||
func loadConfig() {
|
||||
file, err := os.Open("config.json")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
decoder := json.NewDecoder(file)
|
||||
err = decoder.Decode(&config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func updateJSONFile() {
|
||||
filePath := filepath.Join(config.WatchDir, config.JsonFile)
|
||||
file, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var jsonConfig struct {
|
||||
FormatVersion int `json:"format_version"`
|
||||
Header struct {
|
||||
Description string `json:"description"`
|
||||
Name string `json:"name"`
|
||||
UUID string `json:"uuid"`
|
||||
Version []int `json:"version"`
|
||||
MinEngineVersion []int `json:"min_engine_version"`
|
||||
} `json:"header"`
|
||||
Modules []struct {
|
||||
Description string `json:"description"`
|
||||
Type string `json:"type"`
|
||||
UUID string `json:"uuid"`
|
||||
Version []int `json:"version"`
|
||||
} `json:"modules"`
|
||||
}
|
||||
|
||||
err = json.Unmarshal(file, &jsonConfig)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
jsonConfig.Header.UUID = uuid.New().String()
|
||||
for i := range jsonConfig.Modules {
|
||||
jsonConfig.Modules[i].UUID = uuid.New().String()
|
||||
}
|
||||
|
||||
updatedFile, err := json.MarshalIndent(jsonConfig, "", " ")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile(filePath, updatedFile, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
lastModifiedTime = time.Now()
|
||||
}
|
||||
|
||||
func zipFolderContents(source, target string) error {
|
||||
zipfile, err := os.Create(target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer zipfile.Close()
|
||||
|
||||
archive := zip.NewWriter(zipfile)
|
||||
defer archive.Close()
|
||||
|
||||
filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if path == source {
|
||||
return nil
|
||||
}
|
||||
|
||||
header, err := zip.FileInfoHeader(info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
header.Name, err = filepath.Rel(source, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
header.Name += "/"
|
||||
} else {
|
||||
header.Method = zip.Deflate
|
||||
}
|
||||
|
||||
writer, err := archive.CreateHeader(header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = io.Copy(writer, file)
|
||||
return err
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func copyFolderContents(source, target string) error {
|
||||
return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if path == source {
|
||||
return nil
|
||||
}
|
||||
|
||||
relPath, err := filepath.Rel(source, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetPath := filepath.Join(target, relPath)
|
||||
|
||||
if info.IsDir() {
|
||||
return os.MkdirAll(targetPath, info.Mode())
|
||||
}
|
||||
|
||||
sourceFile, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sourceFile.Close()
|
||||
|
||||
targetFile, err := os.Create(targetPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer targetFile.Close()
|
||||
|
||||
_, err = io.Copy(targetFile, sourceFile)
|
||||
return err
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user