Cleaner store API
This commit is contained in:
+190
-59
@@ -1,83 +1,214 @@
|
||||
package codegen
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/Masterminds/sprig"
|
||||
"github.com/cortezaproject/corteza-server/pkg/cli"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type (
|
||||
definitions struct {
|
||||
App string
|
||||
|
||||
Rest []*restDef
|
||||
Actions []*actionsDef
|
||||
Events []*eventsDef
|
||||
Types []*typesDef
|
||||
Store []*storeDef
|
||||
}
|
||||
)
|
||||
|
||||
func Proc() {
|
||||
var (
|
||||
err error
|
||||
|
||||
def = &definitions{}
|
||||
watchChanges bool
|
||||
beVerbose bool
|
||||
|
||||
tpls = template.New("").Funcs(map[string]interface{}{
|
||||
"camelCase": camelCase,
|
||||
"pubIdent": pubIdent,
|
||||
"unpubIdent": unpubIdent,
|
||||
"toLower": strings.ToLower,
|
||||
"cc2underscore": cc2underscore,
|
||||
"normalizeImport": normalizeImport,
|
||||
"comment": func(text string, skip1st bool) string {
|
||||
ll := strings.Split(text, "\n")
|
||||
s := 0
|
||||
out := ""
|
||||
if skip1st {
|
||||
s = 1
|
||||
out = ll[0] + "\n"
|
||||
}
|
||||
fileList []string
|
||||
watcher *fsnotify.Watcher
|
||||
|
||||
for ; s < len(ll); s++ {
|
||||
out += "// " + ll[s] + "\n"
|
||||
}
|
||||
templatesPath = filepath.Join("pkg", "codegen", "assets", "*.tpl")
|
||||
templatesSrc []string
|
||||
|
||||
return out
|
||||
},
|
||||
})
|
||||
actionSrcPath = filepath.Join("*", "service", "*_actions.yaml")
|
||||
actionSrc []string
|
||||
actionDefs []*actionsDef
|
||||
|
||||
eventSrcPath = filepath.Join("*", "service", "event", "events.yaml")
|
||||
eventSrc []string
|
||||
eventDefs []*eventsDef
|
||||
|
||||
typeSrcPath = filepath.Join("*", "*", "types.yaml")
|
||||
typeSrc []string
|
||||
typeDefs []*typesDef
|
||||
|
||||
restSrcPath = filepath.Join("*", "rest.yaml")
|
||||
restSrc []string
|
||||
restDefs []*restDef
|
||||
|
||||
storeSrcPath = filepath.Join("store", "*.yaml")
|
||||
storeSrc []string
|
||||
storeDefs []*storeDef
|
||||
|
||||
tpls *template.Template
|
||||
tplBase = template.New("").
|
||||
Funcs(map[string]interface{}{
|
||||
"camelCase": camelCase,
|
||||
"export": export,
|
||||
"unexport": unexport,
|
||||
"toggleExport": toggleExport,
|
||||
"toLower": strings.ToLower,
|
||||
"cc2underscore": cc2underscore,
|
||||
"normalizeImport": normalizeImport,
|
||||
"comment": func(text string, skip1st bool) string {
|
||||
ll := strings.Split(text, "\n")
|
||||
s := 0
|
||||
out := ""
|
||||
if skip1st {
|
||||
s = 1
|
||||
out = ll[0] + "\n"
|
||||
}
|
||||
|
||||
for ; s < len(ll); s++ {
|
||||
out += "// " + ll[s] + "\n"
|
||||
}
|
||||
|
||||
return out
|
||||
},
|
||||
}).
|
||||
Funcs(sprig.TxtFuncMap())
|
||||
|
||||
output = func(format string, aa ...interface{}) {
|
||||
if beVerbose {
|
||||
fmt.Fprintf(os.Stdout, format, aa...)
|
||||
}
|
||||
}
|
||||
|
||||
outputErr = func(err error, format string, aa ...interface{}) bool {
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stdout, format, aa...)
|
||||
fmt.Fprintf(os.Stdout, "%v\n", err)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
)
|
||||
|
||||
tpls = template.Must(tpls.ParseGlob("pkg/codegen/assets/*.tpl"))
|
||||
flag.BoolVar(&watchChanges, "w", false, "regenerate code on template or definition change")
|
||||
flag.BoolVar(&beVerbose, "v", false, "output loaded definitions, templates and outputs")
|
||||
flag.Parse()
|
||||
|
||||
if def.Actions, err = procActions(); err != nil {
|
||||
cli.HandleError(err)
|
||||
} else {
|
||||
cli.HandleError(genActions(tpls, def.Actions))
|
||||
}
|
||||
defer func() {
|
||||
if watcher != nil {
|
||||
watcher.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
if def.Events, err = procEvents(); err != nil {
|
||||
cli.HandleError(err)
|
||||
} else {
|
||||
cli.HandleError(genEvents(tpls, def.Events))
|
||||
}
|
||||
for {
|
||||
fileList = make([]string, 0, 100)
|
||||
|
||||
if def.Types, err = procTypes(); err != nil {
|
||||
cli.HandleError(err)
|
||||
} else {
|
||||
cli.HandleError(genTypes(tpls, def.Types))
|
||||
}
|
||||
templatesSrc = glob(templatesPath)
|
||||
output("loaded %d templates from %s\n", len(templatesSrc), templatesPath)
|
||||
|
||||
if def.Rest, err = procRest(); err != nil {
|
||||
cli.HandleError(err)
|
||||
} else {
|
||||
cli.HandleError(genRest(tpls, def.Rest))
|
||||
}
|
||||
actionSrc = glob(actionSrcPath)
|
||||
output("loaded %d action definitions from %s\n", len(actionSrc), actionSrcPath)
|
||||
|
||||
if def.Store, err = procStore(); err != nil {
|
||||
cli.HandleError(err)
|
||||
} else {
|
||||
cli.HandleError(genStore(tpls, def.Store))
|
||||
eventSrc = glob(eventSrcPath)
|
||||
output("loaded %d event definitions from %s\n", len(eventSrc), eventSrcPath)
|
||||
|
||||
typeSrc = glob(typeSrcPath)
|
||||
output("loaded %d type definitions from %s\n", len(typeSrc), typeSrcPath)
|
||||
|
||||
restSrc = glob(restSrcPath)
|
||||
output("loaded %d rest definitions from %s\n", len(restSrc), restSrcPath)
|
||||
|
||||
storeSrc = glob(storeSrcPath)
|
||||
output("loaded %d store definitions from %s\n", len(storeSrc), storeSrcPath)
|
||||
|
||||
if watchChanges {
|
||||
if watcher != nil {
|
||||
watcher.Close()
|
||||
}
|
||||
|
||||
watcher, err = fsnotify.NewWatcher()
|
||||
cli.HandleError(err)
|
||||
|
||||
fileList = append(fileList, templatesSrc...)
|
||||
fileList = append(fileList, actionSrc...)
|
||||
fileList = append(fileList, eventSrc...)
|
||||
fileList = append(fileList, typeSrc...)
|
||||
fileList = append(fileList, restSrc...)
|
||||
fileList = append(fileList, storeSrc...)
|
||||
|
||||
for _, d := range fileList {
|
||||
cli.HandleError(watcher.Add(d))
|
||||
}
|
||||
}
|
||||
|
||||
func() {
|
||||
tpls, err = tplBase.ParseFiles(templatesSrc...)
|
||||
if outputErr(err, "could not parse templates:\n") {
|
||||
return
|
||||
}
|
||||
|
||||
if actionDefs, err = procActions(actionSrc...); err == nil {
|
||||
err = genActions(tpls, actionDefs...)
|
||||
}
|
||||
|
||||
if outputErr(err, "failed to process actions:\n") {
|
||||
return
|
||||
}
|
||||
|
||||
if eventDefs, err = procEvents(eventSrc...); err == nil {
|
||||
err = genEvents(tpls, eventDefs...)
|
||||
}
|
||||
|
||||
if outputErr(err, "failed to process events:\n") {
|
||||
return
|
||||
}
|
||||
|
||||
if typeDefs, err = procTypes(typeSrc...); err == nil {
|
||||
err = genTypes(tpls, typeDefs...)
|
||||
}
|
||||
|
||||
if outputErr(err, "failed to process types:\n") {
|
||||
return
|
||||
}
|
||||
|
||||
if restDefs, err = procRest(restSrc...); err == nil {
|
||||
err = genRest(tpls, restDefs...)
|
||||
}
|
||||
|
||||
if outputErr(err, "failed to process rest:\n") {
|
||||
return
|
||||
}
|
||||
|
||||
if storeDefs, err = procStore(storeSrc...); err == nil {
|
||||
err = genStore(tpls, storeDefs...)
|
||||
}
|
||||
|
||||
if outputErr(err, "failed to process store:\n") {
|
||||
return
|
||||
}
|
||||
|
||||
}()
|
||||
|
||||
if !watchChanges {
|
||||
break
|
||||
}
|
||||
|
||||
// @todo fix this (without causing too many "too-many-files" issues :)
|
||||
output("waiting for changes (if you add a new file, restart codegen manually)\n")
|
||||
|
||||
select {
|
||||
case <-watcher.Events:
|
||||
case err = <-watcher.Errors:
|
||||
cli.HandleError(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func glob(path string) []string {
|
||||
src, err := filepath.Glob(path)
|
||||
if err != nil {
|
||||
cli.HandleError(fmt.Errorf("failed to glob %q: %w", path, err))
|
||||
}
|
||||
|
||||
return src
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user