style: autoformat codegen files

This commit is contained in:
Genqila
2025-12-28 02:16:30 +07:00
committed by guiyomu-dev
parent b510968f7d
commit d3d93034b9
2 changed files with 21 additions and 12 deletions

12
.vscode/settings.json vendored
View File

@@ -1,15 +1,15 @@
{
"go.formatTool": "default",
"[json]": {
"editor.formatOnSave": true,
},
"[jsonc]": {
"editor.formatOnSave": true,
},
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "always"
}
},
"[json]": {
"editor.formatOnSave": true,
},
"[jsonc]": {
"editor.formatOnSave": true,
}
}

View File

@@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"go/format"
"os"
"regexp"
"strings"
@@ -117,13 +118,21 @@ func generateFile(sourcePath, destinationPath, tpl string) error {
t := template.Must(template.New("tpl").Funcs(funcMap).Parse(tpl))
file, err := os.Create(destinationPath)
if err != nil {
var b bytes.Buffer
if err := t.Execute(&b, fileContent); err != nil {
return err
}
defer file.Close()
return t.Execute(file, fileContent)
output := b.Bytes()
if strings.HasSuffix(destinationPath, ".go") {
formatted, err := format.Source(output)
if err != nil {
return err
}
output = formatted
}
return os.WriteFile(destinationPath, output, 0644)
}
func generateItems() error {
@@ -217,9 +226,9 @@ func generateItems() error {
}
itemsFileContent += "}"
err = os.WriteFile("pkg/data/item/items.go", []byte(itemsFileContent), 0644)
formatted, err := format.Source([]byte(itemsFileContent))
if err != nil {
return err
}
return nil
return os.WriteFile("pkg/data/item/items.go", formatted, 0644)
}