Factor code template into a file.
This allows code regeneration without rebuilding the generator binary.
This commit is contained in:
parent
0668181616
commit
a37fadc3ba
@ -7,10 +7,10 @@ import (
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/huin/goupnp"
|
||||
"github.com/huin/goupnp/scpd"
|
||||
"github.com/huin/goutil/codegen"
|
||||
)
|
||||
|
||||
// DCP collects together information about a UPnP Device Control Protocol.
|
||||
@ -86,20 +86,13 @@ func (dcp *DCP) processDeviceFile(file *zip.File) error {
|
||||
return mainErr
|
||||
}
|
||||
|
||||
func (dcp *DCP) writeCode(outFile string, useGofmt bool) error {
|
||||
func (dcp *DCP) writeCode(outFile string, codeTmpl *template.Template) error {
|
||||
packageFile, err := os.Create(outFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var output io.WriteCloser = packageFile
|
||||
if useGofmt {
|
||||
if output, err = codegen.NewGofmtWriteCloser(output); err != nil {
|
||||
packageFile.Close()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err = packageTmpl.Execute(output, dcp); err != nil {
|
||||
if err = codeTmpl.Execute(output, dcp); err != nil {
|
||||
output.Close()
|
||||
return err
|
||||
}
|
||||
|
@ -6,7 +6,9 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -22,15 +24,23 @@ func main() {
|
||||
useGofmt = flag.Bool("gofmt", true, "Pass the generated code through gofmt. "+
|
||||
"Disable this if debugging code generation and needing to see the generated code "+
|
||||
"prior to being passed through gofmt.")
|
||||
codeTmplFile = flag.String("code_tmpl_file", "", "Path to Go template to generate code from.")
|
||||
)
|
||||
flag.Parse()
|
||||
|
||||
if err := run(*dcpName, *specsDir, *useGofmt); err != nil {
|
||||
if err := run(*dcpName, *specsDir, *useGofmt, *codeTmplFile); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func run(dcpName, specsDir string, useGofmt bool) error {
|
||||
func run(dcpName, specsDir string, useGofmt bool, codeTmplFile string) error {
|
||||
codeTmpl, err := template.New(filepath.Base(codeTmplFile)).Funcs(template.FuncMap{
|
||||
"base": filepath.Base,
|
||||
}).ParseFiles(codeTmplFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing template from file: %w", err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(specsDir, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("could not create specs-dir %q: %v", specsDir, err)
|
||||
}
|
||||
@ -47,12 +57,22 @@ func run(dcpName, specsDir string, useGofmt bool) error {
|
||||
return fmt.Errorf("error processing spec %s: %v", metadata.Name, err)
|
||||
}
|
||||
|
||||
if err := dcp.writeCode(filepath.Base(metadata.Name)+".go", useGofmt); err != nil {
|
||||
filename := filepath.Base(metadata.Name) + ".go"
|
||||
if err := dcp.writeCode(filename, codeTmpl); err != nil {
|
||||
return fmt.Errorf("error writing package %q: %v", dcp.Metadata.Name, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
if !useGofmt {
|
||||
return nil
|
||||
}
|
||||
|
||||
return gofmt(filename)
|
||||
}
|
||||
|
||||
return fmt.Errorf("could not find DCP with name %q", dcpName)
|
||||
}
|
||||
|
||||
func gofmt(filename string) error {
|
||||
cmd := exec.Command("gofmt", "-w", filename)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
@ -1,2 +1,2 @@
|
||||
//go:generate goupnpdcpgen -dcp_name av1
|
||||
//go:generate goupnpdcpgen -dcp_name av1 -code_tmpl_file ../dcps.gotemplate
|
||||
package av1
|
||||
|
@ -1,15 +1,4 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var templateFuncs = template.FuncMap{
|
||||
"base": filepath.Base,
|
||||
}
|
||||
|
||||
var packageTmpl = template.Must(template.New("package").Funcs(templateFuncs).Parse(`{{$name := .Metadata.Name}}
|
||||
{{$name := .Metadata.Name -}}
|
||||
// Client for UPnP Device Control Protocol {{.Metadata.OfficialName}}.
|
||||
// {{if .DocURLs}}
|
||||
// This DCP is documented in detail at: {{range .DocURLs}}
|
||||
@ -172,4 +161,3 @@ func (client *{{$srvIdent}}) {{.Name}}({{range $winargs -}}
|
||||
{{define "argstruct"}}struct {{"{"}}
|
||||
{{range .}}{{.Name}} string
|
||||
{{end}}{{"}"}}{{end}}
|
||||
`))
|
@ -1,2 +1,2 @@
|
||||
//go:generate goupnpdcpgen -dcp_name internetgateway1
|
||||
//go:generate goupnpdcpgen -dcp_name internetgateway1 -code_tmpl_file ../dcps.gotemplate
|
||||
package internetgateway1
|
||||
|
@ -1,2 +1,2 @@
|
||||
//go:generate goupnpdcpgen -dcp_name internetgateway2
|
||||
//go:generate goupnpdcpgen -dcp_name internetgateway2 -code_tmpl_file ../dcps.gotemplate
|
||||
package internetgateway2
|
||||
|
@ -1,2 +1,2 @@
|
||||
//go:generate goupnpdcpgen -dcp_name ocf/internetgateway2
|
||||
//go:generate goupnpdcpgen -dcp_name ocf/internetgateway2 -code_tmpl_file ../../dcps.gotemplate
|
||||
package internetgateway2
|
||||
|
Loading…
Reference in New Issue
Block a user