2018-10-13 14:04:17 +00:00
|
|
|
// Command to generate DCP package source from the XML specification.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-08-14 09:17:22 +00:00
|
|
|
"os/exec"
|
2018-10-13 14:04:17 +00:00
|
|
|
"path/filepath"
|
2021-08-14 09:17:22 +00:00
|
|
|
"text/template"
|
2018-10-13 14:04:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
deviceURNPrefix = "urn:schemas-upnp-org:device:"
|
|
|
|
serviceURNPrefix = "urn:schemas-upnp-org:service:"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
dcpName = flag.String("dcp_name", "", "Name of the DCP to generate.")
|
|
|
|
specsDir = flag.String("specs_dir", ".", "Path to the specification storage directory. "+
|
|
|
|
"This is used to find (and download if not present) the specification ZIP files.")
|
|
|
|
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.")
|
2021-08-14 09:17:22 +00:00
|
|
|
codeTmplFile = flag.String("code_tmpl_file", "", "Path to Go template to generate code from.")
|
2018-10-13 14:04:17 +00:00
|
|
|
)
|
|
|
|
flag.Parse()
|
|
|
|
|
2021-08-14 09:17:22 +00:00
|
|
|
if err := run(*dcpName, *specsDir, *useGofmt, *codeTmplFile); err != nil {
|
2018-10-13 14:04:17 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-14 09:17:22 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-10-13 14:04:17 +00:00
|
|
|
if err := os.MkdirAll(specsDir, os.ModePerm); err != nil {
|
2021-07-11 13:44:55 +00:00
|
|
|
return fmt.Errorf("could not create specs-dir %q: %v", specsDir, err)
|
2018-10-13 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:46:32 +00:00
|
|
|
for _, metadata := range dcpMetadata {
|
|
|
|
if metadata.Name != dcpName {
|
2018-10-13 14:04:17 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-03-21 13:46:32 +00:00
|
|
|
|
|
|
|
dcp := newDCP(metadata)
|
|
|
|
|
|
|
|
err := metadata.Src.process(".", metadata.Name, dcp)
|
2018-10-13 14:04:17 +00:00
|
|
|
if err != nil {
|
2022-03-21 13:46:32 +00:00
|
|
|
return fmt.Errorf("error processing spec %s: %v", metadata.Name, err)
|
2018-10-13 14:04:17 +00:00
|
|
|
}
|
2022-03-21 13:46:32 +00:00
|
|
|
|
2021-08-14 09:17:22 +00:00
|
|
|
filename := filepath.Base(metadata.Name) + ".go"
|
|
|
|
if err := dcp.writeCode(filename, codeTmpl); err != nil {
|
2018-10-13 14:04:17 +00:00
|
|
|
return fmt.Errorf("error writing package %q: %v", dcp.Metadata.Name, err)
|
|
|
|
}
|
|
|
|
|
2021-08-14 09:17:22 +00:00
|
|
|
if !useGofmt {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return gofmt(filename)
|
2018-10-13 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("could not find DCP with name %q", dcpName)
|
|
|
|
}
|
2021-08-14 09:17:22 +00:00
|
|
|
|
|
|
|
func gofmt(filename string) error {
|
|
|
|
cmd := exec.Command("gofmt", "-w", filename)
|
|
|
|
return cmd.Run()
|
|
|
|
}
|