flc_pkg example
An example of using flc (ent codegen) as package rather than an executable.
In this example, we have a file named flc.go under the ./fluent directory, that holds the
configuration for the codegen:
// +build ignore
package main
import (
"log"
"strings"
"text/template"
"github.com/usalko/fluent/flc"
"github.com/usalko/fluent/flc/gen"
)
func main() {
opts := []flc.Option{
flc.Dependency(
flc.DependencyType(&http.Client{}),
),
flc.TemplateFiles(
"template/debug.tmpl",
"template/stringer.tmpl",
),
}
err := flc.Generate("./schema", &gen.Config{
Header: `
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Code generated by ent, DO NOT EDIT.
`,
// A usage for custom templates with external functions.
Templates: []*gen.Template{
gen.MustParse(gen.NewTemplate("static").
Funcs(template.FuncMap{"title": strings.ToTitle}).
ParseFiles("template/static.tmpl")),
},
}, opts...)
if err != nil {
log.Fatal("running fluent codegen:", err)
}
}
As you can see, the file is tagged with // +build ignore in order to not include it
in the fluent package. In order to run the codegen, run the file itself (using go run)
or run go generate ./fluent. The generate.go file holds the go run command:
package fluent
//go:generate go run flc.go
The generate.go file is preferred if you have many generate pragmas in your project.