Skip to content

Commit

Permalink
hof regenerating self without diff
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Worm <tony@hofstadter.io>
  • Loading branch information
verdverm committed Dec 27, 2021
1 parent ee68b37 commit 14a9357
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 27 deletions.
6 changes: 5 additions & 1 deletion 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
8 changes: 8 additions & 0 deletions design/cli/cmds/hof.cue
Expand Up @@ -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"
//},
]

}
Expand Down
13 changes: 3 additions & 10 deletions lib/gen/file.go
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"strings"

"cuelang.org/go/cue"
"github.com/epiclabs-io/diff3"
"github.com/sergi/go-diff/diffmatchpatch"

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 3 additions & 4 deletions lib/gen/generator.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 25 additions & 11 deletions lib/gen/loadcue.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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++
}
Expand Down
12 changes: 11 additions & 1 deletion lib/templates/templatemap.go
Expand Up @@ -3,6 +3,7 @@ package templates
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/mattn/go-zglob"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 14a9357

Please sign in to comment.