Options codegen improvements
- Add extra cli switches to generate docs from options - More automation/fallbacks/default values for options
This commit is contained in:
@@ -4,20 +4,30 @@
|
||||
// the code is regenerated.
|
||||
//
|
||||
// Definitions file that controls how this file is generated:
|
||||
// {{ .Source }}
|
||||
|
||||
= {{ export $.Name }}
|
||||
|
||||
[cols="2,3,5a"]
|
||||
|===
|
||||
|Type|Default value|Description
|
||||
|
||||
{{- range $prop := $.Properties }}
|
||||
3+| *{{ toUpper $prop.Env }}*
|
||||
|`{{ $prop.Type }}`
|
||||
|{{- if $prop.Default }}
|
||||
{{- $prop.Default -}}
|
||||
{{- end -}}
|
||||
|{{ $prop.Description }}
|
||||
{{- range .Definitions }}
|
||||
// - {{ .Source }}
|
||||
{{- end }}
|
||||
|===
|
||||
|
||||
|
||||
{{ range .Definitions }}
|
||||
|
||||
= {{ .Docs.Title }}
|
||||
|
||||
{{ .Docs.Intro }}
|
||||
|
||||
{{ range .Properties }}
|
||||
|
||||
== *{{ toUpper .Env }}* `{{ .Type }}`
|
||||
|
||||
{{ if .Default }}
|
||||
Default::
|
||||
`{{ .Default }}`
|
||||
{{ end -}}
|
||||
{{ if .Description }}
|
||||
Description::
|
||||
{{ .Description }}
|
||||
{{ end -}}
|
||||
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
|
||||
+44
-8
@@ -12,11 +12,19 @@ import (
|
||||
)
|
||||
|
||||
func Proc() {
|
||||
const (
|
||||
docPathOptions = "/dev-ops-guide/server-configuration"
|
||||
)
|
||||
|
||||
var (
|
||||
err error
|
||||
|
||||
watchChanges bool
|
||||
beVerbose bool
|
||||
docPath string
|
||||
|
||||
genCode = true
|
||||
genDocs = false
|
||||
|
||||
fileList []string
|
||||
watcher *fsnotify.Watcher
|
||||
@@ -96,8 +104,9 @@ func Proc() {
|
||||
}
|
||||
)
|
||||
|
||||
flag.BoolVar(&watchChanges, "w", false, "regenerate code on template or definition change")
|
||||
flag.BoolVar(&watchChanges, "w", false, "regenerate on change of template or definition files")
|
||||
flag.BoolVar(&beVerbose, "v", false, "output loaded definitions, templates and outputs")
|
||||
flag.StringVar(&docPath, "d", "", "generate docs on template or definition change")
|
||||
flag.Parse()
|
||||
|
||||
defer func() {
|
||||
@@ -106,6 +115,17 @@ func Proc() {
|
||||
}
|
||||
}()
|
||||
|
||||
if len(docPath) > 0 {
|
||||
docPath = strings.TrimRight(docPath, "/") + "/src/modules/ROOT/pages"
|
||||
if i, err := os.Stat(docPath); err != nil {
|
||||
handleError(err)
|
||||
} else if !i.IsDir() {
|
||||
handleError(fmt.Errorf("expecting directory: %q", docPath))
|
||||
}
|
||||
|
||||
genDocs = true
|
||||
}
|
||||
|
||||
for {
|
||||
fileList = make([]string, 0, 100)
|
||||
|
||||
@@ -128,7 +148,7 @@ func Proc() {
|
||||
output("loaded %d store definitions from %s\n", len(storeSrc), storeSrcPath)
|
||||
|
||||
optionSrc = glob(optionSrcPath)
|
||||
output("loaded %d option defenitions from %s\n", len(optionSrc), optionSrcPath)
|
||||
output("loaded %d option definitions from %s\n", len(optionSrc), optionSrcPath)
|
||||
|
||||
if watchChanges {
|
||||
if watcher != nil {
|
||||
@@ -158,7 +178,9 @@ func Proc() {
|
||||
}
|
||||
|
||||
if actionDefs, err = procActions(actionSrc...); err == nil {
|
||||
err = genActions(tpls, actionDefs...)
|
||||
if genCode {
|
||||
err = genActions(tpls, actionDefs...)
|
||||
}
|
||||
}
|
||||
|
||||
if outputErr(err, "failed to process actions:\n") {
|
||||
@@ -166,7 +188,9 @@ func Proc() {
|
||||
}
|
||||
|
||||
if eventDefs, err = procEvents(eventSrc...); err == nil {
|
||||
err = genEvents(tpls, eventDefs...)
|
||||
if genCode {
|
||||
err = genEvents(tpls, eventDefs...)
|
||||
}
|
||||
}
|
||||
|
||||
if outputErr(err, "failed to process events:\n") {
|
||||
@@ -174,7 +198,9 @@ func Proc() {
|
||||
}
|
||||
|
||||
if typeDefs, err = procTypes(typeSrc...); err == nil {
|
||||
err = genTypes(tpls, typeDefs...)
|
||||
if genCode {
|
||||
err = genTypes(tpls, typeDefs...)
|
||||
}
|
||||
}
|
||||
|
||||
if outputErr(err, "failed to process types:\n") {
|
||||
@@ -182,7 +208,9 @@ func Proc() {
|
||||
}
|
||||
|
||||
if restDefs, err = procRest(restSrc...); err == nil {
|
||||
err = genRest(tpls, restDefs...)
|
||||
if genCode {
|
||||
err = genRest(tpls, restDefs...)
|
||||
}
|
||||
}
|
||||
|
||||
if outputErr(err, "failed to process rest:\n") {
|
||||
@@ -190,7 +218,9 @@ func Proc() {
|
||||
}
|
||||
|
||||
if storeDefs, err = procStore(storeSrc...); err == nil {
|
||||
err = genStore(tpls, storeDefs...)
|
||||
if genCode {
|
||||
err = genStore(tpls, storeDefs...)
|
||||
}
|
||||
}
|
||||
|
||||
if outputErr(err, "failed to process store:\n") {
|
||||
@@ -198,7 +228,13 @@ func Proc() {
|
||||
}
|
||||
|
||||
if optionDefs, err = procOptions(optionSrc...); err == nil {
|
||||
err = genOptions(tpls, optionDefs...)
|
||||
if genCode {
|
||||
err = genOptions(tpls, optionDefs...)
|
||||
}
|
||||
|
||||
if genDocs && err == nil {
|
||||
err = genOptionsDocs(tpls, docPath+docPathOptions, optionDefs...)
|
||||
}
|
||||
}
|
||||
|
||||
if outputErr(err, "fail to process options:\n") {
|
||||
|
||||
+49
-22
@@ -2,6 +2,7 @@ package codegen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/cortezaproject/corteza-server/pkg/slice"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
@@ -18,6 +19,11 @@ type (
|
||||
|
||||
Name string
|
||||
|
||||
Docs struct {
|
||||
Title string
|
||||
Intro string
|
||||
}
|
||||
|
||||
// List of imports
|
||||
// Used only by generated file and not pre-generated-user-file
|
||||
Imports []string `yaml:"imports"`
|
||||
@@ -28,10 +34,11 @@ type (
|
||||
optionsPropSet []*optionsProp
|
||||
|
||||
optionsProp struct {
|
||||
Name string
|
||||
Type string
|
||||
Env string
|
||||
Default *optionsPropDefault
|
||||
Name string
|
||||
Type string
|
||||
Env string
|
||||
Default *optionsPropDefault
|
||||
|
||||
Description string
|
||||
}
|
||||
|
||||
@@ -54,7 +61,15 @@ func procOptions(mm ...string) (dd []*optionsDef, err error) {
|
||||
|
||||
defer f.Close()
|
||||
|
||||
d = &optionsDef{}
|
||||
fname := path.Base(m)
|
||||
|
||||
d = &optionsDef{
|
||||
Name: fname[:len(fname)-len(path.Ext(fname))],
|
||||
}
|
||||
|
||||
if d.Docs.Title == "" {
|
||||
d.Docs.Title = d.Name
|
||||
}
|
||||
|
||||
if err := yaml.NewDecoder(f).Decode(d); err != nil {
|
||||
return err
|
||||
@@ -107,33 +122,45 @@ func (o optionsDef) Package() string {
|
||||
|
||||
func genOptions(tpl *template.Template, dd ...*optionsDef) (err error) {
|
||||
var (
|
||||
tplOptionsGen = tpl.Lookup("options.gen.go.tpl")
|
||||
|
||||
tplOptionsAdoc = tpl.Lookup("options.gen.adoc.tpl")
|
||||
tplOptions = tpl.Lookup("options.gen.go.tpl")
|
||||
|
||||
dst string
|
||||
)
|
||||
|
||||
for _, d := range dd {
|
||||
dst = path.Join(d.outputDir, path.Base(d.Source)[:strings.LastIndex(path.Base(d.Source), ".")]+".gen.go")
|
||||
err = goTemplate(dst, tplOptionsGen, d)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
dst = path.Join(d.outputDir, path.Base(d.Source)[:strings.LastIndex(path.Base(d.Source), ".")]+".adoc")
|
||||
err = goTemplate(dst, tplOptionsAdoc, d)
|
||||
err = goTemplate(dst, tplOptions, d)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// for _, d := range dd {
|
||||
// dst = path.Join(d.outputDir, path.Base(d.Source)[:strings.LastIndex(path.Base(d.Source), ".")]+".adoc")
|
||||
// err = goTemplate(dst, tplOptionsAdoc, d)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func genOptionsDocs(tpl *template.Template, docsPath string, dd ...*optionsDef) (err error) {
|
||||
var (
|
||||
tplOptionsAdoc = tpl.Lookup("options.gen.adoc.tpl")
|
||||
|
||||
dst string
|
||||
)
|
||||
|
||||
dst = path.Join(docsPath, "option_env_variables_gen.adoc")
|
||||
return plainTemplate(dst, tplOptionsAdoc, map[string]interface{}{
|
||||
"Definitions": dd,
|
||||
"Import": collectOptionsDefImports("", dd...),
|
||||
})
|
||||
}
|
||||
|
||||
func collectOptionsDefImports(basePkg string, dd ...*optionsDef) []string {
|
||||
ii := make([]string, 0, len(dd))
|
||||
for _, d := range dd {
|
||||
for _, i := range d.Imports {
|
||||
if !slice.HasString(ii, i) && (basePkg == "" || !strings.HasSuffix(i, basePkg)) {
|
||||
ii = append(ii, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ii
|
||||
}
|
||||
|
||||
+19
-17
@@ -44,27 +44,29 @@ func goTemplate(dst string, tpl *template.Template, payload interface{}) (err er
|
||||
return nil
|
||||
}
|
||||
|
||||
func WritePlainTo(tpl *template.Template, payload interface{}, tplName, dst string) {
|
||||
func plainTemplate(dst string, tpl *template.Template, payload interface{}) (err error) {
|
||||
var output io.WriteCloser
|
||||
buf := bytes.Buffer{}
|
||||
|
||||
if err := tpl.ExecuteTemplate(&buf, tplName, payload); err != nil {
|
||||
handleError(err)
|
||||
} else {
|
||||
if dst == "" || dst == "-" {
|
||||
output = os.Stdout
|
||||
} else {
|
||||
if output, err = os.Create(dst); err != nil {
|
||||
handleError(err)
|
||||
}
|
||||
|
||||
defer output.Close()
|
||||
}
|
||||
|
||||
if _, err := output.Write(buf.Bytes()); err != nil {
|
||||
handleError(err)
|
||||
}
|
||||
if err := tpl.Execute(&buf, payload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if dst == "" || dst == "-" {
|
||||
output = os.Stdout
|
||||
} else {
|
||||
if output, err = os.Create(dst); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer output.Close()
|
||||
}
|
||||
|
||||
if _, err := output.Write(buf.Bytes()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func camelCase(pp ...string) (out string) {
|
||||
|
||||
Reference in New Issue
Block a user