Skip to content

Commit

Permalink
hof/gen: fix some bugs, wordsmith
Browse files Browse the repository at this point in the history
  • Loading branch information
verdverm committed Jul 10, 2022
1 parent bbaed78 commit 4795126
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 27 deletions.
1 change: 1 addition & 0 deletions changelogs/0.6.x.md
Expand Up @@ -65,6 +65,7 @@ so you do not need to setup a generator to use.
- `--template`/`-T` flag to specify templates, input data, schemas, and output files
- `--partial`/`-P` flag to support adhoc partial templates
- `--watch`/`-w` flag to suport watching globs and regenerating (also works for generators)
- `--as-module` flag turns your other flags into a reusable and sharable generator module

The `-T` flag has a flexible format so you can
supply multiple templates and control the data.
Expand Down
24 changes: 16 additions & 8 deletions lib/gen/adhoc.go
Expand Up @@ -41,6 +41,9 @@ func (R *Runtime) CreateAdhocGenerator() error {
if err != nil {
return err
}
if R.Verbosity > 2 {
fmt.Printf("%s -> %#v\n", tf, cfg)
}
tcfgs = append(tcfgs, cfg)
globs = append(globs, cfg.Filepath)
}
Expand Down Expand Up @@ -95,20 +98,21 @@ func (R *Runtime) CreateAdhocGenerator() error {
return nil
}

// check if val is a list
if cfg.Repeated {
if iter, ierr := Val.List(); ierr == nil {
// check if val is a list
if iter, ierr := val.List(); ierr == nil {
for iter.Next() {
val := iter.Value()
err := addFile(val)
v := iter.Value()
err := addFile(v)
if err != nil {
return err
}
}
} else if iter, ierr := Val.Fields(); ierr == nil {
// check if val is a struct
} else if iter, ierr := val.Fields(); ierr == nil {
for iter.Next() {
val := iter.Value()
err := addFile(val)
v := iter.Value()
err := addFile(v)
if err != nil {
return err
}
Expand All @@ -117,7 +121,7 @@ func (R *Runtime) CreateAdhocGenerator() error {
return fmt.Errorf("repeated template value is not iterable")
}
} else {
err := addFile(Val)
err := addFile(val)
if err != nil {
return err
}
Expand All @@ -131,6 +135,10 @@ func (R *Runtime) CreateAdhocGenerator() error {
return fmt.Errorf("while initializing adhoc generator")
}

if R.Verbosity > 2 {
fmt.Printf("G: %#v\n", G)
}

R.Generators["AdhocGen"] = G
return nil
}
Expand Down
45 changes: 28 additions & 17 deletions lib/gen/asmodule.go
Expand Up @@ -2,6 +2,7 @@ package gen

import (
"fmt"
"os"
"sort"
"strings"

Expand All @@ -13,7 +14,6 @@ import (
func (R *Runtime) AsModule() error {
FP := R.Flagpole
name := FP.AsModule
var content string

if R.Verbosity > 0 {
fmt.Println("modularizing", name)
Expand Down Expand Up @@ -78,7 +78,7 @@ func (R *Runtime) AsModule() error {

// get generator names that were loaded by -G
gens := []string{}
for label, _ := range R.Generators {
for label := range R.Generators {
if label == "AdhocGen" {
continue
}
Expand Down Expand Up @@ -109,18 +109,17 @@ func (R *Runtime) AsModule() error {
return err
}

// format content (or bs if bytes are better)
content = string(bs)

// stdout or write file
if name == "-" {
fmt.Println(content)
fmt.Println(string(bs))
} else {
// TODO write to file
fmt.Println(content)
// write to file
err := os.WriteFile(name + ".cue", bs, 0644)
if err != nil {
return err
}

// also write mod file

// render template
ft, err := templates.CreateFromString("cue-mods", cuemodsTemplate, nil)
if err != nil {
Expand All @@ -130,15 +129,21 @@ func (R *Runtime) AsModule() error {
if err != nil {
return err
}
err = os.WriteFile("cue.mods", bs, 0644)

// format content (or bs if bytes are better)
content = string(bs)

// TODO write to 'cue.mods'
// fmt.Println(content)
// todo, init module and fetch deps

// parting message
ft, err = templates.CreateFromString("final-msg", finalMsg, nil)
if err != nil {
return err
}
bs, err = ft.Render(data)
if err != nil {
return err
}
fmt.Println(string(bs))

// fmt.Println(finalMsg + name + "\n")
}

if R.Verbosity > 0 {
Expand All @@ -150,8 +155,10 @@ func (R *Runtime) AsModule() error {

const finalMsg = `
Now run
hof mod init cue hof.io/{{ .Name }}
hof mod vendor cue
hof gen -G `
hof gen -G {{ .Name }}
`

const asModuleTemplate = `
package {{ .Name }}
Expand Down Expand Up @@ -277,9 +284,13 @@ import (
// These are the -T mappings
{{ range $i, $cfg := .Configs -}}
t_{{ $i }}: {{ if not .Repeated }}{
{{ if .Cuepath }}In: In.{{.Cuepath}}{{ end }}
{{ if .Schema }}In: {{.Schema}}{{end}}
TemplatePath: "{{ .Filepath }}"
Filepath: "{{ .Outpath }}"
}{{ else }}[{
}{{ else }}[ for _,el in In.{{.Cuepath}} {
{{ if .Cuepath }}In: el{{ end }}
{{ if .Schema }}In: {{.Schema}}{{end}}
TemplatePath: "{{ .Filepath }}"
Filepath: "{{ .Outpath }}"
}]{{ end }}
Expand Down
5 changes: 3 additions & 2 deletions notes/curr.md
Expand Up @@ -5,10 +5,11 @@ v0.6.3
- [x] --watch-templates & clear function
- [x] only do adhoc things if needed (when a `-T` flag is present)
- [x] list / info flags (for available gens, or maybe just part of help?)
- [x] --as-module flag (also --create-module) or --module-* flags? (including subgens from -G)

- [ ] --as-module flag (also --create-module) or --module-* flags? (including subgens from -G)
- [ ] write out --as-module files

- [ ] record demo
- [ ] adhoc to module, show new mode & flags
- [ ] generating types across languages (also as example for docs)

- [ ] update docs
1 change: 1 addition & 0 deletions test/watch/yaml.txt
@@ -0,0 +1 @@
{{ yaml . }}

0 comments on commit 4795126

Please sign in to comment.