Code generation outputs to files.

This commit is contained in:
John Beisley 2022-06-13 07:28:26 +01:00
parent 16c9ddb4ff
commit d15a929f14
2 changed files with 41 additions and 10 deletions

View File

@ -24,6 +24,7 @@ import (
) )
var ( var (
outputDir = flag.String("output_dir", "", "Path to directory to write output in.")
srvManifests = flag.String("srv_manifests", "", "Path to srvmanifests.toml") srvManifests = flag.String("srv_manifests", "", "Path to srvmanifests.toml")
srvTemplate = flag.String("srv_template", "", "Path to srv.gotemplate.") srvTemplate = flag.String("srv_template", "", "Path to srv.gotemplate.")
upnpresourcesZip = flag.String("upnpresources_zip", "", "Path to upnpresources.zip.") upnpresourcesZip = flag.String("upnpresources_zip", "", "Path to upnpresources.zip.")
@ -44,6 +45,13 @@ func run() error {
return fmt.Errorf("unused arguments: %s", strings.Join(flag.Args(), " ")) return fmt.Errorf("unused arguments: %s", strings.Join(flag.Args(), " "))
} }
if *outputDir == "" {
return errors.New("-output_dir is a required flag.")
}
if err := os.MkdirAll(*outputDir, 0); err != nil {
return fmt.Errorf("creating output_dir %q: %w", *outputDir, err)
}
if *srvManifests == "" { if *srvManifests == "" {
return errors.New("-srv_manifests is a required flag.") return errors.New("-srv_manifests is a required flag.")
} }
@ -85,8 +93,8 @@ func run() error {
} }
for _, m := range manifests.DCPS { for _, m := range manifests.DCPS {
if err := processDCP(upnpresources, m, typeMap, tmpl); err != nil { if err := processDCP(upnpresources, m, typeMap, tmpl, *outputDir); err != nil {
return fmt.Errorf("processing DCP %s: %w", m.Path, err) return fmt.Errorf("processing DCP %s: %w", m.SpecZipPath, err)
} }
} }
return nil return nil
@ -97,13 +105,18 @@ func processDCP(
manifest *DCPSpecManifest, manifest *DCPSpecManifest,
typeMap typedesc.TypeMap, typeMap typedesc.TypeMap,
tmpl *template.Template, tmpl *template.Template,
parentOutputDir string,
) error { ) error {
dcpSpecData, err := upnpresources.OpenZip(manifest.Path) outputDir := filepath.Join(parentOutputDir, manifest.OutputDir)
if err := os.MkdirAll(outputDir, os.ModePerm); err != nil {
return fmt.Errorf("creating output directory %q for DCP: %w", outputDir, err)
}
dcpSpecData, err := upnpresources.OpenZip(manifest.SpecZipPath)
if err != nil { if err != nil {
return err return err
} }
for _, srvManifest := range manifest.Services { for _, srvManifest := range manifest.Services {
if err := processService(dcpSpecData, srvManifest, typeMap, tmpl); err != nil { if err := processService(dcpSpecData, srvManifest, typeMap, tmpl, outputDir); err != nil {
return fmt.Errorf("processing service %s: %w", srvManifest.ServiceType, err) return fmt.Errorf("processing service %s: %w", srvManifest.ServiceType, err)
} }
} }
@ -115,7 +128,13 @@ func processService(
srvManifest *ServiceManifest, srvManifest *ServiceManifest,
typeMap typedesc.TypeMap, typeMap typedesc.TypeMap,
tmpl *template.Template, tmpl *template.Template,
parentOutputDir string,
) error { ) error {
outputDir := filepath.Join(parentOutputDir, srvManifest.Package)
if err := os.MkdirAll(outputDir, os.ModePerm); err != nil {
return fmt.Errorf("creating output directory %q for service: %w", outputDir, err)
}
f, err := dcpSpecData.Open(srvManifest.Path) f, err := dcpSpecData.Open(srvManifest.Path)
if err != nil { if err != nil {
return err return err
@ -140,7 +159,13 @@ func processService(
return err return err
} }
err = tmpl.ExecuteTemplate(os.Stdout, "service", tmplArgs{ outputPath := filepath.Join(outputDir, srvManifest.Package+".go")
outFile, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("creating output service file %q: %w", outputPath, err)
}
defer outFile.Close()
err = tmpl.ExecuteTemplate(outFile, "service", tmplArgs{
Manifest: srvManifest, Manifest: srvManifest,
Imps: imps, Imps: imps,
SCPD: sd, SCPD: sd,
@ -148,6 +173,9 @@ func processService(
if err != nil { if err != nil {
return fmt.Errorf("executing srv_template: %w", err) return fmt.Errorf("executing srv_template: %w", err)
} }
if err := outFile.Close(); err != nil {
return fmt.Errorf("closing output service file %q: %w", outputPath, err)
}
return nil return nil
} }
@ -157,8 +185,10 @@ type DCPSpecManifests struct {
} }
type DCPSpecManifest struct { type DCPSpecManifest struct {
// Path is the file path within upnpresources.zip to the DCP spec ZIP file. // SpecZipPath is the file path within upnpresources.zip to the DCP spec ZIP file.
Path string `toml:"path"` SpecZipPath string `toml:"spec_zip_path"`
// OutputDir is the path relative to --output_dir which the packages are written in.
OutputDir string `toml:"output_dir"`
// Services maps from a service name (e.g. "FooBar:1") to a path within the DCP spec ZIP file // Services maps from a service name (e.g. "FooBar:1") to a path within the DCP spec ZIP file
// (e.g. "xml data files/service/FooBar1.xml"). // (e.g. "xml data files/service/FooBar1.xml").
Services []*ServiceManifest `toml:"service"` Services []*ServiceManifest `toml:"service"`

View File

@ -1,10 +1,11 @@
[[dcp]] [[dcp]]
path = "standardizeddcps/Internet Gateway_2/UPnP-gw-IGD-TestFiles-20101210.zip" spec_zip_path = "standardizeddcps/Internet Gateway_2/UPnP-gw-IGD-TestFiles-20101210.zip"
output_dir = "inetgw2"
[[dcp.service]] [[dcp.service]]
package = "lanhostconfigmanagement1" package = "lanhostcfgmng1"
type = "urn:schemas-upnp-org:service:LANHostConfigManagement:1" type = "urn:schemas-upnp-org:service:LANHostConfigManagement:1"
path = "xml data files/service/LANHostConfigManagement1.xml" path = "xml data files/service/LANHostConfigManagement1.xml"
[[dcp.service]] [[dcp.service]]
package = "wanpppconnection1" package = "wanpppconn1"
type = "urn:schemas-upnp-org:service:WANPPPConnection:1" type = "urn:schemas-upnp-org:service:WANPPPConnection:1"
path = "xml data files/service/WANPPPConnection1.xml" path = "xml data files/service/WANPPPConnection1.xml"