main.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "text/template"
  10. "github.com/jinzhu/inflection"
  11. tpl_util "gogs.carduccidante.edu.it/karmen/core/util/template"
  12. )
  13. var (
  14. fnPatterns = map[string]string{
  15. "all": "%s",
  16. "show": "%s_show",
  17. "add_update": "%s_add_update",
  18. }
  19. )
  20. func toUpper(str string) string {
  21. return strings.Title(str)
  22. }
  23. func main() {
  24. flag.Usage = func() {
  25. fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
  26. flag.PrintDefaults()
  27. }
  28. outDir := flag.String("outdir", "./templates", "Output directory")
  29. tplDir := flag.String("tpldir", "./generator/templates", "Generator template directory")
  30. flag.Parse()
  31. if len(flag.Args()) == 0 {
  32. flag.Usage()
  33. return
  34. }
  35. funcMap := template.FuncMap{
  36. "toUpper": toUpper,
  37. }
  38. model := flag.Args()[0]
  39. for tplName, pattern := range fnPatterns {
  40. name := fmt.Sprintf(pattern, model)
  41. tpl, err := tpl_util.LoadTextTemplate(filepath.Join(*tplDir, tplName+".tpl"), funcMap)
  42. if err != nil {
  43. panic(err)
  44. }
  45. filename := filepath.Join(*outDir, name+".html.tpl")
  46. oFn, err := os.Create(filename)
  47. if err != nil {
  48. panic(err)
  49. }
  50. defer oFn.Close()
  51. log.Printf("Generating html template %s for model %s...", filename, name)
  52. var data struct {
  53. Model string
  54. Models string
  55. }
  56. data.Model = inflection.Singular(model)
  57. data.Models = inflection.Plural(model)
  58. err = tpl.Execute(oFn, data)
  59. if err != nil {
  60. panic(err)
  61. }
  62. }
  63. }