check vim no bomb
This commit is contained in:
parent
222615a18a
commit
1d89459673
4 changed files with 216 additions and 122 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,5 +1,6 @@
|
||||||
.recipes
|
.recipes
|
||||||
*.zip
|
*.zip
|
||||||
|
*.swp
|
||||||
LinuxServer
|
LinuxServer
|
||||||
Windows
|
Windows
|
||||||
WindowsServer
|
WindowsServer
|
Binary file not shown.
Binary file not shown.
277
magefile.go
277
magefile.go
|
@ -9,7 +9,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -28,127 +27,188 @@ const (
|
||||||
schemaDir = "./schema"
|
schemaDir = "./schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
var binaryExtensions = map[string]bool{
|
var (
|
||||||
|
binaryExtensions = map[string]bool{
|
||||||
".png": true, ".jpg": true, ".jpeg": true, ".bmp": true, ".gif": true,
|
".png": true, ".jpg": true, ".jpeg": true, ".bmp": true, ".gif": true,
|
||||||
".dds": true, ".tga": true, ".psd": true, ".fbx": true, ".uasset": true,
|
".dds": true, ".tga": true, ".psd": true, ".fbx": true, ".uasset": true,
|
||||||
".umap": true,
|
".umap": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
targetPlatforms = []string{"Windows", "WindowsServer", "LinuxServer"}
|
||||||
|
)
|
||||||
|
|
||||||
var Default = Build
|
var Default = Build
|
||||||
|
|
||||||
func Build() {
|
func Build() {
|
||||||
mg.SerialDeps(Validate, Package)
|
mg.SerialDeps(Validate, Package)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Package creates distribution packages for all target platforms
|
||||||
func Package() error {
|
func Package() error {
|
||||||
fmt.Println("Packaging files...")
|
fmt.Println("Packaging files...")
|
||||||
for _, dir := range []string{"Windows", "WindowsServer", "LinuxServer"} {
|
|
||||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
if err := createPlatformDirectories(); err != nil {
|
||||||
return fmt.Errorf("creating %s: %w", dir, err)
|
return fmt.Errorf("failed to create platform directories: %w", err)
|
||||||
}
|
|
||||||
if err := sh.Run("cp", "-r", contentDir, pluginFile, dir+"/"); err != nil {
|
|
||||||
return fmt.Errorf("copying to %s: %w", dir, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := createZip(zipName, "Windows", "WindowsServer", "LinuxServer"); err != nil {
|
|
||||||
return fmt.Errorf("creating zip: %w", err)
|
|
||||||
}
|
|
||||||
return Clean("Windows", "WindowsServer", "LinuxServer")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Validate() error {
|
if err := createZip(zipName, targetPlatforms...); err != nil {
|
||||||
fmt.Println("Validating files...")
|
return fmt.Errorf("failed to create zip archive: %w", err)
|
||||||
compiler := jsonschema.NewCompiler()
|
|
||||||
compiler.LoadURL = func(url string) (io.ReadCloser, error) {
|
|
||||||
return nil, fmt.Errorf("remote schema loading disabled: %s", url)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := filepath.Walk(schemaDir, func(path string, info os.FileInfo, err error) error {
|
return Clean(targetPlatforms...)
|
||||||
if err != nil || info.IsDir() || filepath.Ext(path) != ".json" {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
relPath, err := filepath.Rel(schemaDir, path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
id := schemaBaseURL + filepath.ToSlash(relPath)
|
|
||||||
data, err := ioutil.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return compiler.AddResource(id, bytes.NewReader(data))
|
|
||||||
}); err != nil {
|
|
||||||
return fmt.Errorf("failed to load schemas: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
schema, err := compiler.Compile(schemaBaseURL + "CL_Recipe.json")
|
func createPlatformDirectories() error {
|
||||||
if err != nil {
|
for _, platform := range targetPlatforms {
|
||||||
return fmt.Errorf("invalid main schema: %w", err)
|
if err := os.MkdirAll(platform, 0755); err != nil {
|
||||||
|
return fmt.Errorf("failed to create directory %s: %w", platform, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var failed []string
|
if err := sh.Run("cp", "-r", contentDir, pluginFile, platform+"/"); err != nil {
|
||||||
paths := []string{pluginFile}
|
return fmt.Errorf("failed to copy files to %s: %w", platform, err)
|
||||||
_ = filepath.Walk(contentDir, func(path string, info os.FileInfo, err error) error {
|
}
|
||||||
if err == nil && !info.IsDir() {
|
|
||||||
paths = append(paths, path)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
|
||||||
|
|
||||||
for _, path := range paths {
|
|
||||||
if err := validateFile(path, schema); err != nil {
|
|
||||||
failed = append(failed, fmt.Sprintf("%s: %v", path, err))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(failed) > 0 {
|
// Validate checks all files for proper formatting and schema compliance
|
||||||
fmt.Println("Validation errors:")
|
func Validate() error {
|
||||||
for _, msg := range failed {
|
fmt.Println("Validating files...")
|
||||||
fmt.Println(" -", msg)
|
|
||||||
|
schema, err := setupSchemaValidator()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("schema setup failed: %w", err)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("%d file(s) failed validation", len(failed))
|
|
||||||
|
filesToValidate, err := collectFilesToValidate()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to collect files for validation: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
validationErrors := validateFiles(filesToValidate, schema)
|
||||||
|
|
||||||
|
if len(validationErrors) > 0 {
|
||||||
|
logValidationErrors(validationErrors)
|
||||||
|
return fmt.Errorf("%d file(s) failed validation", len(validationErrors))
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("All files validated successfully.")
|
fmt.Println("All files validated successfully.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateFile(path string, schema *jsonschema.Schema) error {
|
func setupSchemaValidator() (*jsonschema.Schema, error) {
|
||||||
if binaryExtensions[filepath.Ext(path)] {
|
compiler := jsonschema.NewCompiler()
|
||||||
return nil
|
compiler.LoadURL = func(url string) (io.ReadCloser, error) {
|
||||||
}
|
return nil, fmt.Errorf("remote schema loading disabled: %s", url)
|
||||||
if filepath.Ext(path) == ".json" {
|
|
||||||
return validateJSON(path, schema)
|
|
||||||
}
|
|
||||||
return validateEncoding(path)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateJSON(path string, schema *jsonschema.Schema) error {
|
if err := loadLocalSchemas(compiler); err != nil {
|
||||||
data, err := ioutil.ReadFile(path)
|
return nil, fmt.Errorf("failed to load schemas: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return compiler.Compile(schemaBaseURL + "CL_Recipe.json")
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadLocalSchemas(compiler *jsonschema.Compiler) error {
|
||||||
|
return filepath.Walk(schemaDir, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil || info.IsDir() || filepath.Ext(path) != ".json" {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
relPath, err := filepath.Rel(schemaDir, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
data = sanitizeJSONBytes(data)
|
|
||||||
|
|
||||||
var lines [][]byte
|
id := schemaBaseURL + filepath.ToSlash(relPath)
|
||||||
for _, line := range bytes.Split(data, []byte("\n")) {
|
data, err := os.ReadFile(path)
|
||||||
trim := bytes.TrimSpace(line)
|
if err != nil {
|
||||||
if !bytes.HasPrefix(trim, []byte("//")) && len(trim) > 0 {
|
|
||||||
lines = append(lines, line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
clean := bytes.Join(lines, []byte("\n"))
|
|
||||||
|
|
||||||
var v interface{}
|
|
||||||
if err := json.Unmarshal(clean, &v); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return schema.Validate(v)
|
|
||||||
|
return compiler.AddResource(id, bytes.NewReader(data))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func collectFilesToValidate() ([]string, error) {
|
||||||
|
var files []string
|
||||||
|
files = append(files, pluginFile)
|
||||||
|
|
||||||
|
err := filepath.Walk(contentDir, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err == nil && !info.IsDir() {
|
||||||
|
files = append(files, path)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
|
||||||
|
return files, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateFiles(files []string, schema *jsonschema.Schema) []string {
|
||||||
|
var validationErrors []string
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
if err := validateFile(file, schema); err != nil {
|
||||||
|
validationErrors = append(validationErrors, fmt.Sprintf("%s: %v", file, err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return validationErrors
|
||||||
|
}
|
||||||
|
|
||||||
|
func logValidationErrors(errors []string) {
|
||||||
|
fmt.Println("Validation errors:")
|
||||||
|
for _, msg := range errors {
|
||||||
|
fmt.Println(" -", msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateFile(path string, schema *jsonschema.Schema) error {
|
||||||
|
ext := filepath.Ext(path)
|
||||||
|
|
||||||
|
if binaryExtensions[ext] {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if ext == ".json" {
|
||||||
|
return validateJSONFile(path, schema)
|
||||||
|
}
|
||||||
|
|
||||||
|
return validateTextFileEncoding(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateJSONFile(path string, schema *jsonschema.Schema) error {
|
||||||
|
cleanJSON, err := readAndCleanJSONFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var v interface{}
|
||||||
|
if err := json.Unmarshal(cleanJSON, &v); err != nil {
|
||||||
|
return fmt.Errorf("JSON parsing error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := schema.Validate(v); err != nil {
|
||||||
|
return fmt.Errorf("schema validation error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func readAndCleanJSONFile(path string) ([]byte, error) {
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
data = sanitizeJSONBytes(data)
|
||||||
|
return removeJSONComments(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func sanitizeJSONBytes(data []byte) []byte {
|
func sanitizeJSONBytes(data []byte) []byte {
|
||||||
data = bytes.TrimPrefix(data, []byte{0xEF, 0xBB, 0xBF})
|
data = bytes.TrimPrefix(data, []byte{0xEF, 0xBB, 0xBF})
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
|
|
||||||
for len(data) > 0 {
|
for len(data) > 0 {
|
||||||
r, size := utf8.DecodeRune(data)
|
r, size := utf8.DecodeRune(data)
|
||||||
if r == utf8.RuneError && size == 1 || r == '\x00' {
|
if r == utf8.RuneError && size == 1 || r == '\x00' {
|
||||||
|
@ -158,17 +218,31 @@ func sanitizeJSONBytes(data []byte) []byte {
|
||||||
out.WriteRune(r)
|
out.WriteRune(r)
|
||||||
data = data[size:]
|
data = data[size:]
|
||||||
}
|
}
|
||||||
|
|
||||||
return out.Bytes()
|
return out.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateEncoding(path string) error {
|
func removeJSONComments(data []byte) []byte {
|
||||||
data, err := ioutil.ReadFile(path)
|
var lines [][]byte
|
||||||
if err != nil {
|
|
||||||
return err
|
for _, line := range bytes.Split(data, []byte("\n")) {
|
||||||
|
trim := bytes.TrimSpace(line)
|
||||||
|
if !bytes.HasPrefix(trim, []byte("//")) && len(trim) > 0 {
|
||||||
|
lines = append(lines, line)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes.Join(lines, []byte("\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateTextFileEncoding(path string) error {
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < len(data); {
|
for i := 0; i < len(data); {
|
||||||
if data[i] > 127 {
|
if data[i] > 127 {
|
||||||
// DO NOT declare "size", it is unused and will cause a compiler error
|
|
||||||
r, _ := utf8.DecodeRune(data[i:])
|
r, _ := utf8.DecodeRune(data[i:])
|
||||||
if r == utf8.RuneError {
|
if r == utf8.RuneError {
|
||||||
return fmt.Errorf("invalid UTF-8 sequence at position %d", i)
|
return fmt.Errorf("invalid UTF-8 sequence at position %d", i)
|
||||||
|
@ -177,32 +251,39 @@ func validateEncoding(path string) error {
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Clean(list ...string) error {
|
// Clean removes temporary directories
|
||||||
|
func Clean(dirs ...string) error {
|
||||||
fmt.Println("Cleaning up...")
|
fmt.Println("Cleaning up...")
|
||||||
for _, f := range list {
|
|
||||||
if err := os.RemoveAll(f); err != nil {
|
for _, dir := range dirs {
|
||||||
return fmt.Errorf("failed to remove %s: %w", f, err)
|
if err := os.RemoveAll(dir); err != nil {
|
||||||
|
return fmt.Errorf("failed to remove %s: %w", dir, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createZip(zipPath string, dirs ...string) error {
|
func createZip(zipPath string, dirs ...string) error {
|
||||||
zipFile, err := os.Create(zipPath)
|
zipFile, err := os.Create(zipPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("creating zip file: %w", err)
|
return fmt.Errorf("failed to create zip file: %w", err)
|
||||||
}
|
}
|
||||||
defer zipFile.Close()
|
defer zipFile.Close()
|
||||||
|
|
||||||
zipWriter := zip.NewWriter(zipFile)
|
zipWriter := zip.NewWriter(zipFile)
|
||||||
defer zipWriter.Close()
|
defer zipWriter.Close()
|
||||||
|
|
||||||
for _, dir := range dirs {
|
for _, dir := range dirs {
|
||||||
if err := addDirToZip(zipWriter, dir); err != nil {
|
if err := addDirToZip(zipWriter, dir); err != nil {
|
||||||
return fmt.Errorf("adding %s to zip: %w", dir, err)
|
return fmt.Errorf("failed to add directory %s to zip: %w", dir, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,43 +292,55 @@ func addDirToZip(zipWriter *zip.Writer, dirPath string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
relPath, err := filepath.Rel(dirPath, filePath)
|
relPath, err := filepath.Rel(dirPath, filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
zipPath := filepath.Join(filepath.Base(dirPath), relPath)
|
zipPath := filepath.Join(filepath.Base(dirPath), relPath)
|
||||||
|
return addFileToZip(zipWriter, filePath, zipPath, info)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func addFileToZip(zipWriter *zip.Writer, filePath, zipPath string, info os.FileInfo) error {
|
||||||
header, err := zip.FileInfoHeader(info)
|
header, err := zip.FileInfoHeader(info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
header.Name = filepath.ToSlash(zipPath)
|
header.Name = filepath.ToSlash(zipPath)
|
||||||
header.Method = zip.Deflate
|
header.Method = zip.Deflate
|
||||||
|
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
header.Name += "/"
|
header.Name += "/"
|
||||||
_, err := zipWriter.CreateHeader(header)
|
_, err := zipWriter.CreateHeader(header)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
writer, err := zipWriter.CreateHeader(header)
|
writer, err := zipWriter.CreateHeader(header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if binaryExtensions[strings.ToLower(filepath.Ext(filePath))] {
|
if binaryExtensions[strings.ToLower(filepath.Ext(filePath))] {
|
||||||
_, err = io.Copy(writer, file)
|
_, err = io.Copy(writer, file)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
content, err := ioutil.ReadAll(file)
|
|
||||||
|
content, err := io.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
content = convertToDOSLineEndings(content)
|
|
||||||
_, err = writer.Write(content)
|
_, err = writer.Write(convertToDOSLineEndings(content))
|
||||||
return err
|
return err
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertToDOSLineEndings(content []byte) []byte {
|
func convertToDOSLineEndings(content []byte) []byte {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue