Assume generated code lands in a subdirectory

This commit is contained in:
Brandon Dyck 2019-04-02 20:05:48 -06:00
parent 6948cdde4f
commit 94a7c075c7
2 changed files with 22 additions and 33 deletions

View File

@ -1,6 +1,6 @@
package html5 package html5
//go:generate go run ../internal/codegen/codegen.go -input defs.json -output generated.go -package html5 -import //go:generate go run ../internal/codegen/codegen.go -input defs.json -output generated.go -package html5
import "gitlab.codemonkeysoftware.net/b/hatmill" import "gitlab.codemonkeysoftware.net/b/hatmill"

View File

@ -15,14 +15,12 @@ const headerFmt = `// GENERATED BY gitlab.codemonkeysoftware.net/b/hatmill/inter
// DO NOT EDIT! // DO NOT EDIT!
package %s package %s
import "gitlab.codemonkeysoftware.net/b/hatmill"
` `
func fileHeader(packageName string, needImport bool) string { func fileHeader(packageName string) string {
header := fmt.Sprintf(headerFmt, packageName) return fmt.Sprintf(headerFmt, packageName)
if needImport {
header += `import "gitlab.codemonkeysoftware.net/b/hatmill"` + "\n"
}
return header
} }
type AttribType int type AttribType int
@ -58,32 +56,28 @@ type AttribDef struct {
Type AttribType `json:"type"` Type AttribType `json:"type"`
} }
func (def AttribDef) Generate(qualified bool) string { func (def AttribDef) Generate() string {
const ( const (
boolType = "bool" boolType = "bool"
stringType = "string" stringType = "string"
stringTemplate = `// %[1]s creates a "%[2]s" attribute stringTemplate = `// %[1]s creates a "%[2]s" attribute
func %[1]s(value string) %[3]sAttrib { func %[1]s(value string) hatmill.Attrib {
return %[3]sAttrib{ return hatmill.Attrib{
Key: "%[2]s", Key: "%[2]s",
Value: value, Value: value,
} }
} }
` `
boolTemplate = `// %[1]s creates a "%[2]s" attribute boolTemplate = `// %[1]s creates a "%[2]s" attribute
func %[1]s() %[3]sAttrib { func %[1]s() hatmill.Attrib {
return %[3]sAttrib{ return hatmill.Attrib{
Key: "%[2]s", Key: "%[2]s",
} }
} }
` `
) )
var pkg string
if qualified {
pkg = "hatmill."
}
var template string var template string
switch def.Type { switch def.Type {
case Bool: case Bool:
@ -94,7 +88,7 @@ func (def AttribDef) Generate(qualified bool) string {
panic(fmt.Errorf("unknown attribute type: %v", def.Type)) panic(fmt.Errorf("unknown attribute type: %v", def.Type))
} }
return fmt.Sprintf(template, strings.Title(def.Name), def.Name, pkg) return fmt.Sprintf(template, strings.Title(def.Name), def.Name)
} }
type ElemDef struct { type ElemDef struct {
@ -102,13 +96,13 @@ type ElemDef struct {
Void bool `json:"void"` Void bool `json:"void"`
} }
func (def ElemDef) Generate(qualified bool) string { func (def ElemDef) Generate() string {
const ( const (
parentTemplate = `// %[1]s creates a <%[2]s> element. parentTemplate = `// %[1]s creates a <%[2]s> element.
func %[1]s(attribs ...%[3]sAttrib) func(children ...%[3]sTerm) %[3]sParentElement { func %[1]s(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
return func(children ...%[3]sTerm) %[3]sParentElement { return func(children ...hatmill.Term) hatmill.ParentElement {
return %[3]sParentElement{ return hatmill.ParentElement{
VoidElement: %[3]sVoidElement{ VoidElement: hatmill.VoidElement{
TagName: "%[2]s", TagName: "%[2]s",
Attribs: attribs, Attribs: attribs,
}, },
@ -118,8 +112,8 @@ func (def ElemDef) Generate(qualified bool) string {
} }
` `
voidTemplate = `// %[1]s creates a <%[2]s> element. voidTemplate = `// %[1]s creates a <%[2]s> element.
func %[1]s(attribs ...%[3]sAttrib) %[3]sVoidElement { func %[1]s(attribs ...hatmill.Attrib) hatmill.VoidElement {
return %[3]sVoidElement{ return hatmill.VoidElement{
TagName: "%[2]s", TagName: "%[2]s",
Attribs: attribs, Attribs: attribs,
} }
@ -127,15 +121,11 @@ func (def ElemDef) Generate(qualified bool) string {
` `
) )
var pkg string
if qualified {
pkg = "hatmill."
}
template := parentTemplate template := parentTemplate
if def.Void { if def.Void {
template = voidTemplate template = voidTemplate
} }
return fmt.Sprintf(template, strings.Title(def.Name), def.Name, pkg) return fmt.Sprintf(template, strings.Title(def.Name), def.Name)
} }
type Defs struct { type Defs struct {
@ -147,7 +137,6 @@ func main() {
inputPath := flag.String("input", "", "JSON input file") inputPath := flag.String("input", "", "JSON input file")
outputPath := flag.String("output", "", ".go output file") outputPath := flag.String("output", "", ".go output file")
packageName := flag.String("package", "", "output package name") packageName := flag.String("package", "", "output package name")
addImport := flag.Bool("import", false, "import hatmill in output package")
flag.Parse() flag.Parse()
input, err := ioutil.ReadFile(*inputPath) input, err := ioutil.ReadFile(*inputPath)
@ -162,12 +151,12 @@ func main() {
} }
output := new(bytes.Buffer) output := new(bytes.Buffer)
output.WriteString(fileHeader(*packageName, *addImport)) output.WriteString(fileHeader(*packageName))
for _, elemDef := range defs.Elements { for _, elemDef := range defs.Elements {
output.WriteString(elemDef.Generate(*addImport)) output.WriteString(elemDef.Generate())
} }
for _, attribDef := range defs.Attributes { for _, attribDef := range defs.Attributes {
output.WriteString(attribDef.Generate(*addImport)) output.WriteString(attribDef.Generate())
} }
formatted, err := format.Source(output.Bytes()) formatted, err := format.Source(output.Bytes())