From 14a93579da0f3da7d28a5daa421dc49d1253d813 Mon Sep 17 00:00:00 2001 From: Tony Worm Date: Mon, 27 Dec 2021 16:33:30 -0500 Subject: [PATCH] hof regenerating self without diff Signed-off-by: Tony Worm --- cue.mods | 6 +++++- design/cli/cmds/hof.cue | 8 ++++++++ lib/gen/file.go | 13 +++---------- lib/gen/generator.go | 7 +++---- lib/gen/loadcue.go | 36 +++++++++++++++++++++++++----------- lib/templates/templatemap.go | 12 +++++++++++- 6 files changed, 55 insertions(+), 27 deletions(-) diff --git a/cue.mods b/cue.mods index 816ce0bb4..86a82c0b1 100644 --- a/cue.mods +++ b/cue.mods @@ -1,9 +1,13 @@ module github.com/hofstadter-io/hof -cue master +cue 0.4.0 require ( github.com/hofstadter-io/ghacue v0.1.0 github.com/hofstadter-io/hofmod-cli v0.7.4 github.com/hofstadter-io/hofmod-cuefig v0.0.4 ) + +replace github.com/hofstadter-io/ghacue => ../ghacue +replace github.com/hofstadter-io/hofmod-cli => ../mods/cli +replace github.com/hofstadter-io/hofmod-cuefig => ../mods/cuefig diff --git a/design/cli/cmds/hof.cue b/design/cli/cmds/hof.cue index 3d7ddcb8e..e4db65eab 100644 --- a/design/cli/cmds/hof.cue +++ b/design/cli/cmds/hof.cue @@ -31,6 +31,14 @@ import ( Long: "generator" Short: "g" }, + //{ + //Name: "f" + //Type: "[]string" + //Default: "nil" + //Help: "File globs to render, default is all discovered" + //Long: "files" + //Short: "f" + //}, ] } diff --git a/lib/gen/file.go b/lib/gen/file.go index d64e7f765..d0338eaa4 100644 --- a/lib/gen/file.go +++ b/lib/gen/file.go @@ -8,7 +8,6 @@ import ( "os" "strings" - "cuelang.org/go/cue" "github.com/epiclabs-io/diff3" "github.com/sergi/go-diff/diffmatchpatch" @@ -17,8 +16,8 @@ import ( type File struct { // Input Data, local to this file - // In map[string]interface{} - In cue.Value + In map[string]interface{} + // In cue.Value // The full path under the output location // empty implies don't generate, even though it may endup in the list @@ -222,13 +221,7 @@ func (F *File) UnifyContent() (write bool, err error) { func (F *File) RenderTemplate() error { var err error - In := make(map[string]interface{}) - err = F.In.Decode(&In) - if err != nil { - return err - } - - F.RenderContent, err = F.TemplateInstance.Render(In) + F.RenderContent, err = F.TemplateInstance.Render(F.In) if err != nil { return err } diff --git a/lib/gen/generator.go b/lib/gen/generator.go index f4e135f11..671dbc075 100644 --- a/lib/gen/generator.go +++ b/lib/gen/generator.go @@ -46,8 +46,7 @@ type Generator struct { Outdir string // "Global" input, merged with out replacing onto the files - // In map[string]interface{} - In cue.Value + In map[string]interface{} // The list fo files for hof to generate, in cue values Out []*File @@ -183,9 +182,9 @@ func (G *Generator) initPartials() []error { } for _, tg := range G.Partials { - prefix := filepath.Clean(tg.TrimPrefix) for _, glob := range tg.Globs { // setup vars + prefix := filepath.Clean(tg.TrimPrefix) if G.PackageName != "" { glob = filepath.Join(CUE_VENDOR_DIR, G.PackageName, glob) prefix = filepath.Join(CUE_VENDOR_DIR, G.PackageName, prefix) @@ -231,9 +230,9 @@ func (G *Generator) initTemplates() []error { } for _, tg := range G.Templates { - prefix := filepath.Clean(tg.TrimPrefix) for _, glob := range tg.Globs { // setup vars + prefix := filepath.Clean(tg.TrimPrefix) if G.PackageName != "" { glob = filepath.Join(CUE_VENDOR_DIR, G.PackageName, glob) prefix = filepath.Join(CUE_VENDOR_DIR, G.PackageName, prefix) diff --git a/lib/gen/loadcue.go b/lib/gen/loadcue.go index 520482325..57a654695 100644 --- a/lib/gen/loadcue.go +++ b/lib/gen/loadcue.go @@ -104,8 +104,8 @@ func (G *Generator) loadIn() error { return val.Err() } - G.In = val - return nil + G.In = make(map[string]interface{}) + return val.Decode(&G.In) } func (G *Generator) loadTemplates() error { @@ -179,8 +179,8 @@ func (G *Generator) loadOut() error { return val.Err() } - G.Out = make([]*File, 0) - err := val.Decode(&G.Out) + Out := make([]*File, 0) + err := val.Decode(&Out) if err != nil { return err } @@ -190,18 +190,32 @@ func (G *Generator) loadOut() error { if err != nil { return err } + + G.Out = make([]*File, 0) i := 0 for L.Next() { v := L.Value() in := v.LookupPath(cue.ParsePath("In")) - // If In exists - if in.Err() == nil { - // unify with G.In - G.Out[i].In = in.Unify(G.In) - } else { - // else, just use G.In - G.Out[i].In = G.In + // Only keep valid elements + // Invalid include conditional elements in CUE Gen which are not "included" + elem := Out[i] + if elem != nil { + // If In exists + if in.Err() == nil { + // merge with G.In + for k, v := range G.In { + // only copy in top-level elements which do not exist already + if _, ok := elem.In[k]; !ok { + elem.In[k] = v + } + } + } else { + // else, just use G.In + elem.In = G.In + } + + G.Out = append(G.Out, elem) } i++ } diff --git a/lib/templates/templatemap.go b/lib/templates/templatemap.go index 0c1530275..37d3d91a9 100644 --- a/lib/templates/templatemap.go +++ b/lib/templates/templatemap.go @@ -3,6 +3,7 @@ package templates import ( "fmt" "io/ioutil" + "os" "path/filepath" "github.com/mattn/go-zglob" @@ -37,7 +38,16 @@ func (M TemplateMap) ImportFromFolder(glob, prefix string, delims *Delims) error } for _, match := range matches { - err := M.importTemplate(match, prefix, delims) + info, err := os.Stat(match) + if err != nil { + return err + } + + if info.IsDir() { + continue + } + + err = M.importTemplate(match, prefix, delims) if err != nil { return err }