Skip to content

Commit

Permalink
add mutil-level group support;add cleanup out first support
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Mar 28, 2020
1 parent ab2ed9d commit e33db79
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 48 deletions.
11 changes: 11 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type InitOpts struct {
ParserName string
SinkPath string
DefaultTag string
Cleanup bool
}

// ParserOpts used for initial parser
Expand All @@ -60,6 +61,7 @@ type ParserOpts struct {
// GeneratorOpts used for initial generator
type GeneratorOpts struct {
SinkPath string
Cleanup bool
}

// Option pass option to custom run behavior
Expand Down Expand Up @@ -90,6 +92,7 @@ func (opts *InitOpts) ParserOpts() *ParserOpts {
func (opts *InitOpts) GeneratorOpts() *GeneratorOpts {
return &GeneratorOpts{
SinkPath: opts.SinkPath,
Cleanup: opts.Cleanup,
}
}

Expand Down Expand Up @@ -173,6 +176,13 @@ func SinkPath(path string) Option {
})
}

// Cleanup set generator cleanup out first when re-generate code
func Cleanup(enable bool) Option {
return optFunc(func(opts *InitOpts) {
opts.Cleanup = enable
})
}

// DefaultTag set parser's default struct field tag string key
func DefaultTag(tag string) Option {
return optFunc(func(opts *InitOpts) {
Expand Down Expand Up @@ -251,5 +261,6 @@ func defaultInitOpts() *InitOpts {
ParserName: ParserStructTag,
SinkPath: ".gen",
DefaultTag: "mir",
Cleanup: true,
}
}
26 changes: 22 additions & 4 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"sync"
Expand All @@ -31,8 +32,9 @@ func init() {
}

type mirGenerator struct {
sinkPath string
name string
sinkPath string
name string
isCleanup bool
}

type mirWriter struct {
Expand Down Expand Up @@ -63,12 +65,15 @@ func (g *mirGenerator) Init(opts *core.GeneratorOpts) (err error) {
if opts == nil {
return errors.New("init opts is nil")
}
g.isCleanup = opts.Cleanup
g.sinkPath, err = evalSinkPath(opts.SinkPath)
return
}

// Generate serial generate interface code
func (g *mirGenerator) Generate(ds core.Descriptors) error {
// cleanup out first if need
g.cleanup()
return generate(g.name, g.sinkPath, ds)
}

Expand All @@ -85,10 +90,13 @@ func (g *mirGenerator) GenerateContext(ctx core.MirCtx) {
return os.MkdirAll(path, 0755)
})

// cleanup out first if need
g.cleanup()

var t *template.Template
wg := &sync.WaitGroup{}
for iface := range ifaceSource {
dirPath := filepath.Join(apiPath, iface.SnakeGroup())
dirPath := filepath.Join(apiPath, iface.Group)
if err = onceSet.Add(dirPath); err != nil {
goto FuckErr
}
Expand Down Expand Up @@ -122,6 +130,16 @@ func (g *mirGenerator) Clone() core.Generator {
}
}

func (g *mirGenerator) cleanup() {
if g.isCleanup {
apiPath := path.Join(g.sinkPath, "api")
core.Logus("cleanup out: %s", apiPath)
if err := os.RemoveAll(apiPath); err != nil {
core.Logus("want cleanup out first but failure: %s.do it later by yourself.", err)
}
}
}

func notEmptyStr(s string) bool {
return s != ""
}
Expand Down Expand Up @@ -192,7 +210,7 @@ func generate(generatorName string, sinkPath string, ds core.Descriptors) error
FuckErr:
for key, ifaces := range ds {
group := ds.GroupFrom(key)
dirPath = filepath.Join(apiPath, ds.SnakeStr(group))
dirPath = filepath.Join(apiPath, group)
if err = os.MkdirAll(dirPath, 0755); err != nil {
break
}
Expand Down
6 changes: 5 additions & 1 deletion internal/parser/reflex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package parser

import (
"reflect"
"strings"

"github.com/alimy/mir/v2/core"
)
Expand Down Expand Up @@ -114,7 +115,10 @@ func (r *reflex) inflateGroupInfo(d *core.IfaceDescriptor, v reflect.Value, t *t
} else {
d.Group = t.group
}
d.SetPkgName(d.Group)
if d.Group != "" {
pkgName := strings.Split(d.Group, "/")
d.SetPkgName(pkgName[len(pkgName)-1])
}
}

// fieldFrom build tagField from entry and tagInfo
Expand Down
2 changes: 1 addition & 1 deletion mirc/cmd/templates/chi_go_mod.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module {{ .PkgName }}
go 1.12

require (
github.com/alimy/mir/v2 v2.3.0
github.com/alimy/mir/v2 v2.3.1
github.com/go-chi/chi v4.0.3+incompatible
)
2 changes: 1 addition & 1 deletion mirc/cmd/templates/echo_go_mod.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module {{ .PkgName }}
go 1.12

require (
github.com/alimy/mir/v2 v2.3.0
github.com/alimy/mir/v2 v2.3.1
github.com/labstack/echo/v4 v4.1.15
)
2 changes: 1 addition & 1 deletion mirc/cmd/templates/gin_go_mod.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module {{ .PkgName }}
go 1.12

require (
github.com/alimy/mir/v2 v2.3.0
github.com/alimy/mir/v2 v2.3.1
github.com/gin-gonic/gin v1.5.0
)
2 changes: 1 addition & 1 deletion mirc/cmd/templates/httprouter_go_mod.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module {{ .PkgName }}
go 1.12

require (
github.com/alimy/mir/v2 v2.3.0
github.com/alimy/mir/v2 v2.3.1
github.com/julienschmidt/httprouter v1.3.0
)
2 changes: 1 addition & 1 deletion mirc/cmd/templates/iris_go_mod.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module {{ .PkgName }}
go 1.12

require (
github.com/alimy/mir/v2 v2.3.0
github.com/alimy/mir/v2 v2.3.1
github.com/kataras/iris/v12 v12.1.8
)
2 changes: 1 addition & 1 deletion mirc/cmd/templates/macaron_go_mod.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module {{ .PkgName }}
go 1.12

require (
github.com/alimy/mir/v2 v2.3.0
github.com/alimy/mir/v2 v2.3.1
gopkg.in/macaron.v1 v1.3.5
)
3 changes: 0 additions & 3 deletions mirc/cmd/templates/makefile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ run: fmt

.PHONY: generate
generate:
@cd mirc
@-rm -rf gen
@go generate mirc/main.go
@cd ..
@$(GOFMT) ./

.PHONY: fmt
Expand Down
2 changes: 1 addition & 1 deletion mirc/cmd/templates/mux_go_mod.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module {{ .PkgName }}
go 1.12

require (
github.com/alimy/mir/v2 v2.3.0
github.com/alimy/mir/v2 v2.3.1
github.com/gorilla/mux v1.7.4
)
Loading

0 comments on commit e33db79

Please sign in to comment.