151 lines
3.3 KiB
Go
151 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"text/template"
|
|
|
|
tpl_util "git.andreafazzi.eu/andrea/oef/util/template"
|
|
"github.com/jinzhu/inflection"
|
|
)
|
|
|
|
var (
|
|
fnTemplatePatterns = map[string]string{
|
|
"all": "%s",
|
|
"show": "%s_show",
|
|
"add_update": "%s_add_update",
|
|
}
|
|
fnModelPatterns = map[string]string{
|
|
"model": "%s",
|
|
}
|
|
|
|
TemplateInputDir string
|
|
TemplateOutputDir string
|
|
ModelInputDir string
|
|
ModelOutputDir string
|
|
)
|
|
|
|
func toUpper(str string) string {
|
|
return strings.Title(str)
|
|
}
|
|
|
|
func toLower(str string) string {
|
|
return strings.ToLower(str)
|
|
}
|
|
|
|
func generateTemplates(patterns map[string]string, model string, funcMap ...template.FuncMap) error {
|
|
for tplName, pattern := range patterns {
|
|
var data struct {
|
|
Model string
|
|
Models string
|
|
}
|
|
|
|
data.Model = inflection.Singular(model)
|
|
data.Models = inflection.Plural(model)
|
|
|
|
name := fmt.Sprintf(pattern, strings.ToLower(data.Models))
|
|
|
|
tpl, err := tpl_util.LoadTextTemplate(filepath.Join(TemplateInputDir, tplName+".tpl"), funcMap...)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
filename := filepath.Join(TemplateOutputDir, name+".html.tpl")
|
|
if _, err := os.Stat(filename); err != nil {
|
|
oFn, err := os.Create(filename)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer oFn.Close()
|
|
|
|
log.Printf("Generating html template %s for model %s...", filename, name)
|
|
|
|
err = tpl.Execute(oFn, data)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
} else {
|
|
log.Printf("Template %s already exists. Skipping.", filename)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func generateModels(patterns map[string]string, model string, funcMap ...template.FuncMap) error {
|
|
for tplName, pattern := range patterns {
|
|
var data struct {
|
|
Model string
|
|
Models string
|
|
}
|
|
|
|
data.Model = inflection.Singular(model)
|
|
data.Models = inflection.Plural(model)
|
|
|
|
name := fmt.Sprintf(pattern, strings.ToLower(data.Model))
|
|
|
|
tpl, err := tpl_util.LoadTextTemplate(filepath.Join(ModelInputDir, tplName+".tpl"), funcMap...)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
filename := filepath.Join(ModelOutputDir, name+".go")
|
|
if _, err := os.Stat(filename); err != nil {
|
|
oFn, err := os.Create(filename)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer oFn.Close()
|
|
|
|
log.Printf("Generating model go file %s for model %s...", filename, name)
|
|
|
|
err = tpl.Execute(oFn, data)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
} else {
|
|
log.Printf("Model file %s already exists. Skipping.", filename)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func main() {
|
|
flag.Usage = func() {
|
|
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
|
flag.PrintDefaults()
|
|
}
|
|
|
|
TemplateOutputDir = *flag.String("tpl-output-dir", "./templates", "Template output directory")
|
|
TemplateInputDir = *flag.String("tpl-input-dir", "./template_generator/templates", "Generator template directory")
|
|
ModelOutputDir = *flag.String("model-output-dir", "./orm", "ORM output directory")
|
|
ModelInputDir = *flag.String("model-input-dir", "./template_generator/orm", "Generator ORM template directory")
|
|
|
|
flag.Parse()
|
|
|
|
if len(flag.Args()) == 0 {
|
|
flag.Usage()
|
|
return
|
|
}
|
|
|
|
funcMap := template.FuncMap{
|
|
"toUpper": toUpper,
|
|
"toLower": toLower,
|
|
}
|
|
|
|
model := flag.Args()[0]
|
|
|
|
if err := generateTemplates(fnTemplatePatterns, model, funcMap); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if err := generateModels(fnModelPatterns, model, funcMap); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
}
|