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", "go.formatTool": "default",
"[json]": {
"editor.formatOnSave": true,
},
"[jsonc]": {
"editor.formatOnSave": true,
},
"[go]": { "[go]": {
"editor.formatOnSave": true, "editor.formatOnSave": true,
"editor.codeActionsOnSave": { "editor.codeActionsOnSave": {
"source.organizeImports": "always" "source.organizeImports": "always"
} }
},
"[json]": {
"editor.formatOnSave": true,
},
"[jsonc]": {
"editor.formatOnSave": true,
} }
} }

View File

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