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
//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"

View File

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