Described all the code, added a build

This commit is contained in:
2024-09-13 21:24:03 +03:00
parent 06fcdaacd4
commit 8d6e6f3c4b
2 changed files with 45 additions and 8 deletions

29
build.sh Executable file
View File

@@ -0,0 +1,29 @@
#!/bin/bash
# Define the list of target platforms
PLATFORMS=("darwin-amd64" "linux-386" "linux-amd64" "windows-386" "windows-amd64")
# Create the dist directory if it doesnt exist
mkdir -p dist
for PLATFORM in "${PLATFORMS[@]}"; do
# Extract the GOOS and GOARCH values from the platform string
GOOS=${PLATFORM%%-*}
GOARCH=${PLATFORM##*-}
echo "Building for $GOOS/$GOARCH..."
# Set the GOOS and GOARCH environment variables
export GOOS
export GOARCH
# Determine the binary extension based on the target OS
BIN_EXT=""
if [[ "$GOOS" == "windows" ]]; then
BIN_EXT=".exe"
fi
# Build the executable
go build -o "dist/JSONUIHELPER-$GOOS-$GOARCH$BIN_EXT"
done
echo "Build completed."

24
main.go
View File

@@ -12,6 +12,7 @@ import (
"time"
)
// Config struct to hold configuration values from config.json
type Config struct {
WatchDir string `json:"watchDir"`
ZipDir string `json:"zipDir"`
@@ -26,12 +27,15 @@ var (
)
func main() {
// Load configuration from config.json
loadConfig()
// Check if watch directory and zip directory are the same
if config.WatchDir == config.ZipDir {
log.Fatal("Watch directory and zip directory cannot be the same.")
}
// Create a new file system watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
@@ -46,25 +50,22 @@ func main() {
if !ok {
return
}
// Check if the event is a write or create event
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
}
// Check if the modification time is after the last modified time
if info.ModTime().After(lastModifiedTime) {
log.Println("Modified file:", event.Name)
updateJSONFile()
// Zip or copy folder contents based on the config
if config.Zip {
err := zipFolderContents(config.WatchDir, filepath.Join(config.ZipDir, config.ZipFileName))
if err != nil {
return
}
zipFolderContents(config.WatchDir, filepath.Join(config.ZipDir, config.ZipFileName))
} else {
err := copyFolderContents(config.WatchDir, config.ZipDir)
if err != nil {
return
}
copyFolderContents(config.WatchDir, config.ZipDir)
}
}
}
@@ -77,11 +78,13 @@ func main() {
}
}()
// Add the watch directory to the watcher
err = watcher.Add(config.WatchDir)
if err != nil {
log.Fatal(err)
}
// Walk through the watch directory and add all subdirectories to the watcher
err = filepath.Walk(config.WatchDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
@@ -98,6 +101,7 @@ func main() {
<-done
}
// Load configuration from config.json
func loadConfig() {
file, err := os.Open("config.json")
if err != nil {
@@ -112,6 +116,7 @@ func loadConfig() {
}
}
// Update the JSON file with new UUIDs
func updateJSONFile() {
filePath := filepath.Join(config.WatchDir, config.JsonFile)
file, err := os.ReadFile(filePath)
@@ -141,6 +146,7 @@ func updateJSONFile() {
log.Fatal(err)
}
// Generate new UUIDs for the header and modules
jsonConfig.Header.UUID = uuid.New().String()
for i := range jsonConfig.Modules {
jsonConfig.Modules[i].UUID = uuid.New().String()
@@ -159,6 +165,7 @@ func updateJSONFile() {
lastModifiedTime = time.Now()
}
// Zip the contents of the source directory and save it to the target file
func zipFolderContents(source, target string) error {
zipfile, err := os.Create(target)
if err != nil {
@@ -216,6 +223,7 @@ func zipFolderContents(source, target string) error {
return err
}
// Copy the contents of the source directory to the target directory
func copyFolderContents(source, target string) error {
return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {