Skip to content

Commit

Permalink
improve unification of render into gen
Browse files Browse the repository at this point in the history
  • Loading branch information
verdverm committed Jul 4, 2022
1 parent f28fe0b commit 9f03b4a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .hof/shadow/Cli/cmd/hof/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ hof gen data.cue ...
hof gen app.cue -G frontend -G backend -G migrations
https://docs.hofstadter.io/first-example/
# You can extend or override a generator by using
# You can mix adhof with generators by using
# both the -G and -T/-P flags`

func init() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/hof/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ hof gen data.cue ...
hof gen app.cue -G frontend -G backend -G migrations
https://docs.hofstadter.io/first-example/
# You can extend or override a generator by using
# You can mix adhof with generators by using
# both the -G and -T/-P flags`

func init() {
Expand Down Expand Up @@ -138,4 +138,4 @@ func init() {
GenCmd.SetHelpFunc(help)
GenCmd.SetUsageFunc(usage)

}
}
2 changes: 1 addition & 1 deletion design/cli/cmds/gen.cue
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ hof gen data.cue ...
hof gen app.cue -G frontend -G backend -G migrations
https://docs.hofstadter.io/first-example/
# You can extend or override a generator by using
# You can mix adhof with generators by using
# both the -G and -T/-P flags
"""
27 changes: 23 additions & 4 deletions lib/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,33 @@ import (

func Gen(args []string, rootflags flags.RootPflagpole, cmdflags flags.GenFlagpole) error {

if len(cmdflags.Generator) == 0 && len(cmdflags.Template) > 0 {
return Render(args, rootflags, cmdflags)
}

verystart := time.Now()

var errs []error

if len(cmdflags.Template) > 0 {
err := Render(args, rootflags, cmdflags)
if err != nil {
errs = append(errs, err)
}
if len(cmdflags.Generator) == 0 {
var err error
if len(errs) > 0 {
for _, e := range errs {
cuetils.PrintCueError(e)
}
err = fmt.Errorf("\nErrors during adhoc gen\n")
}
if cmdflags.Stats {
veryend := time.Now()
elapsed := veryend.Sub(verystart).Round(time.Millisecond)
fmt.Printf("\nTotal Elapsed Time: %s\n\n", elapsed)
}
return err
}
}


R := NewRuntime(args, cmdflags)

errs = R.LoadCue()
Expand Down
9 changes: 8 additions & 1 deletion lib/gen/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Render(args []string, rootflags flags.RootPflagpole, cmdflags flags.GenFlag

RC.RootValue = crt.CueValue

RC.G = NewGenerator("HofRenderCmd", RC.RootValue)
RC.G = NewGenerator("AdhocGen", RC.RootValue)
RC.G.UseDiff3 = cmdflags.Diff3

// everything loaded, now process
Expand All @@ -87,6 +87,13 @@ func Render(args []string, rootflags flags.RootPflagpole, cmdflags flags.GenFlag
return errs[0]
}


if cmdflags.Stats {
RC.G.Stats.CalcTotals(RC.G)
fmt.Printf("\nGen: %s\n==========================\n", RC.G.Name)
fmt.Println(RC.G.Stats)
}

return nil
}

Expand Down
38 changes: 26 additions & 12 deletions lib/gen/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ func (R *Runtime) LoadCue() []error {
}

func (R *Runtime) ExtractGenerators() {
allGen := len(R.Flagpole.Generator) == 1 && R.Flagpole.Generator[0] == "*"
hasT := len(R.Flagpole.Template) > 0

// loop ever all top level structs
for _, S := range R.TopLevelStructs {

Expand All @@ -127,22 +130,33 @@ func (R *Runtime) ExtractGenerators() {
// does it have "@gen()"
if A.Name() == "gen" {

// are there flags to match?
if len(R.Flagpole.Generator) > 0 {
vals := cuetils.AttrToMap(A)
match := false
for _, g := range R.Flagpole.Generator {
if _, ok := vals[g]; ok {
match = true
break
// if -G '*', then we skip the following checks
if !allGen {
// some -G was set, but was not '*'
if len(R.Flagpole.Generator) > 0 {
vals := cuetils.AttrToMap(A)
match := false
for _, g := range R.Flagpole.Generator {
if _, ok := vals[g]; ok {
match = true
break
}
}
}

if !match {
continue
if !match {
continue
}
} else {
// not -G was set, if a -T was set...
// we are in adhoc mode and skip all gens
// (hmmm) will we even get here?
// an earlier shortcircuit may prevent this
// this is defensive anyhow
if hasT {
continue
}
}
}

// passed, we should generate
hasgen = true
break
Expand Down

0 comments on commit 9f03b4a

Please sign in to comment.