uic: add main flag handling, add go:generate comment with preserved flags

This commit is contained in:
mappu 2024-09-20 18:51:47 +12:00
parent 6198406bfb
commit 5a2c8b726f
2 changed files with 31 additions and 11 deletions

View File

@ -2,33 +2,48 @@ package main
import (
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
)
func main() {
in, err := ioutil.ReadFile(`test.ui`)
inFile := flag.String("InFile", "", "Input .ui file")
outFile := flag.String("OutFile", "-", "Output .go file, or - for stdout")
packageName := flag.String("Package", "main", "Custom package name")
flag.Parse()
if *inFile == "" {
flag.Usage()
os.Exit(1)
}
inXml, err := ioutil.ReadFile(*inFile)
if err != nil {
panic(err)
}
var parsed UiFile
err = xml.Unmarshal(in, &parsed)
err = xml.Unmarshal(inXml, &parsed)
if err != nil {
panic(err)
}
ret, err := xml.MarshalIndent(parsed, "", " ")
gosrc, err := generate(*packageName, strings.Join(os.Args[1:], " "), parsed)
if err != nil {
panic(err)
}
fmt.Println(string(ret))
if *outFile == "-" {
fmt.Println(string(gosrc))
gosrc, err := generate("main", parsed)
if err != nil {
panic(err)
} else {
err = ioutil.WriteFile(*outFile, gosrc, 0644)
if err != nil {
panic(err)
}
}
fmt.Println(string(gosrc))
}

View File

@ -32,9 +32,14 @@ func collectClassNames(u UiFile) []string {
return ret
}
func generate(packageName string, u UiFile) ([]byte, error) {
func generate(packageName string, goGenerateArgs string, u UiFile) ([]byte, error) {
ret := strings.Builder{}
ret.WriteString(`package ` + packageName + `
ret.WriteString(`// Generated by miqt-uic. To update this file, edit the .ui file in
// Qt Designer, and then run 'go generate'.
//
//go:` + `generate miqt-uic ` + goGenerateArgs + `
package ` + packageName + `
import (
"github.com/mappu/miqt"