redo magefile

This commit is contained in:
Merith 2025-06-22 13:16:39 -07:00
parent 90503f86b1
commit 42fc32511e
3 changed files with 99 additions and 80 deletions

12
go.mod
View file

@ -1,5 +1,13 @@
module DigitalStorageTweaks module local
go 1.24.3 go 1.24.3
require github.com/magefile/mage v1.15.0 require (
github.com/magefile/mage v1.15.0
github.com/xeipuuv/gojsonschema v1.2.0
)
require (
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
)

13
go.sum
View file

@ -1,2 +1,15 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg= github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=

View file

@ -4,125 +4,123 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"unicode" "unicode"
"unicode/utf8"
"github.com/magefile/mage/mg" "github.com/magefile/mage/mg"
"github.com/magefile/mage/sh" "github.com/magefile/mage/sh"
"github.com/xeipuuv/gojsonschema"
) )
const ( const (
zipName = "DigitalStorageTweaks.zip" zipName = "DigitalStorageTweaks.zip"
contentDir = "./ContentLib" contentDir = "./ContentLib"
pluginFile = "./DigitalStorageTweaks.uplugin" pluginFile = "./DigitalStorageTweaks.uplugin"
targetDirs = "Windows WindowsServer LinuxServer"
) )
var Default = Package // Default target
var Default = Build
// Package creates the distribution zip file // Build runs the full pipeline
func Build() {
mg.SerialDeps(Validate, Package)
}
// Package creates the distribution zip
func Package() error { func Package() error {
mg.Deps(SanitizeContent) fmt.Println("Packaging files...")
defer Clean(false) // Clean temp dirs but keep zip
fmt.Println("Creating target directories...") // Create target directories
for _, dir := range []string{"Windows", "WindowsServer", "LinuxServer"} { for _, dir := range []string{"Windows", "WindowsServer", "LinuxServer"} {
if err := os.MkdirAll(dir, 0755); err != nil { if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directory %s: %v", dir, err) return fmt.Errorf("creating %s: %w", dir, err)
} }
fmt.Printf("Copying files to %s...\n", dir)
if err := sh.Run("cp", "-r", contentDir, pluginFile, dir+"/"); err != nil { if err := sh.Run("cp", "-r", contentDir, pluginFile, dir+"/"); err != nil {
return fmt.Errorf("failed to copy files to %s: %v", dir, err) return fmt.Errorf("copying to %s: %w", dir, err)
} }
} }
fmt.Println("Creating zip archive...") // Create zip
return sh.Run("7z", "a", "-r", zipName, "Windows/", "LinuxServer/", "WindowsServer/") if err := sh.Run("7z", "a", "-r", zipName, "Windows/", "LinuxServer/", "WindowsServer/"); err != nil {
return fmt.Errorf("creating zip: %w", err)
}
// Clean temp dirs
return Clean("Windows", "WindowsServer", "LinuxServer")
} }
// Clean removes generated artifacts // Validate checks all content files
func Clean(includeZip bool) error { func Validate() error {
fmt.Println("Cleaning artifacts...") fmt.Println("Validating files...")
paths := []string{"Windows", "WindowsServer", "LinuxServer"}
if includeZip {
paths = append(paths, zipName)
}
for _, path := range paths {
if err := sh.Rm(path); err != nil {
return err
}
}
return nil
}
// SanitizeContent cleans all text files of non-ASCII and ensures CRLF line endings // Check plugin file
func SanitizeContent() error { if err := checkFile(pluginFile); err != nil {
fmt.Println("Sanitizing content files...") return fmt.Errorf("plugin file: %w", err)
// Process plugin file
if err := sanitizeFile(pluginFile); err != nil {
return fmt.Errorf("plugin file sanitization failed: %v", err)
} }
// Process content directory // Check content directory
return filepath.Walk(contentDir, func(path string, info os.FileInfo, err error) error { return filepath.Walk(contentDir, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() { if err != nil || info.IsDir() {
return err return err
} }
return checkFile(path)
})
}
func checkFile(path string) error {
// Skip binary files // Skip binary files
switch filepath.Ext(path) { switch filepath.Ext(path) {
case ".png", ".jpg", ".jpeg", ".bmp", ".gif", ".dds", ".tga", ".psd", ".fbx", ".uasset", ".umap": case ".png", ".jpg", ".jpeg", ".bmp", ".gif", ".dds", ".tga", ".psd", ".fbx", ".uasset", ".umap":
return nil return nil
case ".json":
return validateJSON(path)
default: default:
return sanitizeFile(path) return validateEncoding(path)
} }
})
} }
func sanitizeFile(path string) error { func validateJSON(path string) error {
content, err := ioutil.ReadFile(path) schemaLoader := gojsonschema.NewReferenceLoader("https://raw.githubusercontent.com/budak7273/ContentLib_Documentation/main/JsonSchemas/CL_Recipe.json")
documentLoader := gojsonschema.NewReferenceLoader("file:///" + filepath.ToSlash(path))
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil { if err != nil {
return fmt.Errorf("failed to read %s: %v", path, err) return fmt.Errorf("schema validation failed: %w", err)
} }
// First fix any encoding issues by converting to valid UTF-8 if !result.Valid() {
content = bytes.ToValidUTF8(content, []byte{}) return fmt.Errorf("invalid JSON schema: %v", result.Errors())
}
// Remove non-ASCII characters but preserve standard whitespace return nil
cleanContent := bytes.Map(func(r rune) rune { }
switch {
case r > unicode.MaxASCII: func validateEncoding(path string) error {
return -1 // Remove non-ASCII content, err := ioutil.ReadFile(path)
case r == '\t', r == '\n', r == '\r': if err != nil {
return r // Keep standard whitespace return err
case r < 32 || r == 127: }
return -1 // Remove control characters
default: // Check for non-ASCII
return r // Keep regular ASCII for i := 0; i < len(content); i++ {
if content[i] > unicode.MaxASCII {
r, _ := utf8.DecodeRune(content[i:])
return fmt.Errorf("non-ASCII character %U at position %d", r, i)
}
}
return nil
}
// Clean removes all build artifacts
func Clean(list ...string) error {
fmt.Println("Cleaning up...")
for _, f := range list {
if err := os.RemoveAll(f); err != nil {
return fmt.Errorf("failed to remove %s: %w", f, err)
}
} }
}, content)
// Normalize line endings to CRLF
cleanContent = bytes.ReplaceAll(cleanContent, []byte("\r\n"), []byte("\n"))
cleanContent = bytes.ReplaceAll(cleanContent, []byte("\n"), []byte("\r\n"))
// Only write back if changes were made
if !bytes.Equal(content, cleanContent) {
fmt.Printf("Sanitizing %s\n", path)
fileInfo, err := os.Stat(path)
if err != nil {
return fmt.Errorf("failed to get file info for %s: %v", path, err)
}
if err := ioutil.WriteFile(path, cleanContent, fileInfo.Mode()); err != nil {
return fmt.Errorf("failed to write %s: %v", path, err)
}
}
return nil return nil
} }