Skip to content

Commit

Permalink
mirc add custom package name feature
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Mar 2, 2020
1 parent 27a481b commit 186b117
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 48 deletions.
27 changes: 22 additions & 5 deletions mirc/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"log"
"os"
"path/filepath"
"text/template"

"github.com/spf13/cobra"
)

var (
dstPath string
pkgName string
style string
)

Expand All @@ -23,6 +25,7 @@ func init() {

// parse flags for agentCmd
newCmd.Flags().StringVarP(&dstPath, "dst", "d", ".", "genereted destination target directory")
newCmd.Flags().StringVarP(&pkgName, "pkg", "p", "github.com/alimy/mir-example", "project's package name")
newCmd.Flags().StringVarP(&style, "type", "t", "gin", "generated engine type style(eg: gin,chi,mux,httprout)")

// register agentCmd as sub-command
Expand Down Expand Up @@ -53,19 +56,23 @@ func newRun(_cmd *cobra.Command, _args []string) {
log.Fatal("not exist style engine")
}

if err = genProject(path, tmpls); err != nil {
ctx := &tmplCtx{
PkgName: pkgName,
}
if err = genProject(ctx, path, tmpls); err != nil {
log.Fatal(err)
}
}

func genProject(dstPath string, tmpls map[string]string) error {
func genProject(ctx *tmplCtx, dstPath string, tmpls map[string]tmplInfo) error {
var (
err error
filePath, dirPath string
file *os.File
)

for fileName, assetName := range tmpls {
tmpl := template.New("mirc")
for fileName, assetInfo := range tmpls {
filePath = filepath.Join(dstPath, fileName)
dirPath = filepath.Dir(filePath)
if err = os.MkdirAll(dirPath, 0755); err != nil {
Expand All @@ -75,8 +82,18 @@ func genProject(dstPath string, tmpls map[string]string) error {
if err != nil {
break
}
if _, err = file.Write(MustAsset(assetName)); err != nil {
break
if assetInfo.isTmpl {
t, err := tmpl.Parse(MustAssetString(assetInfo.name))
if err != nil {
break
}
if err = t.Execute(file, ctx); err != nil {
break
}
} else {
if _, err = file.Write(MustAsset(assetInfo.name)); err != nil {
break
}
}
}
return err
Expand Down
30 changes: 21 additions & 9 deletions mirc/cmd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@ package cmd

//go:generate go-bindata -nomemcopy -pkg=${GOPACKAGE} -ignore=README.md -prefix=templates -debug=false -o=templates_gen.go templates/...

var tmplFiles = map[string]map[string]string{
// tmplCtx template context for generate project
type tmplCtx struct {
PkgName string
}

// tmplInfo template data info
type tmplInfo struct {
name string
isTmpl bool
}

// tmplFiles map of generated file's assets info
var tmplFiles = map[string]map[string]tmplInfo{
"gin": {
"Makefile": "makefile.tmpl",
"README.md": "readme.tmpl",
"go.mod": "gin_go_mod.tmpl",
"main.go": "gin_main.tmpl",
"mirc/main.go": "gin_mirc_main.tmpl",
"mirc/routes/site.go": "gin_mirc_routes_site.tmpl",
"mirc/routes/v1/site.go": "gin_mirc_routes_site_v1.tmpl",
"mirc/routes/v2/site.go": "gin_mirc_routes_site_v2.tmpl",
"Makefile": {"makefile.tmpl", false},
"README.md": {"readme.tmpl", false},
"go.mod": {"gin_go_mod.tmpl", true},
"main.go": {"gin_main.tmpl", false},
"mirc/main.go": {"gin_mirc_main.tmpl", true},
"mirc/routes/site.go": {"gin_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"gin_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"gin_mirc_routes_site_v2.tmpl", false},
},
}
4 changes: 2 additions & 2 deletions mirc/cmd/templates/gin_go_mod.tmpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/alimy/mir-examples
module {{ .PkgName }}

go 1.12

require (
github.com/alimy/mir/v2 v2.0.0-alpha.1
github.com/gin-gonic/gin v1.4.0
github.com/alimy/mir/v2 v2.0.0
)
4 changes: 0 additions & 4 deletions mirc/cmd/templates/gin_main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import (
"log"

"github.com/gin-gonic/gin"

"github.com/alimy/mir-examples/mirc/gen/api"
"github.com/alimy/mir-examples/mirc/gen/api/v1"
"github.com/alimy/mir-examples/mirc/gen/api/v2"
)

func main() {
Expand Down
7 changes: 4 additions & 3 deletions mirc/cmd/templates/gin_mirc_main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (

"github.com/alimy/mir/v2/core"
"github.com/alimy/mir/v2/engine"
"github.com/alimy/mir-examples/mirc/routes"
"github.com/alimy/mir-examples/mirc/routes/v1"
"github.com/alimy/mir-examples/mirc/routes/v2"

routes "{{ .PkgName }}/mirc/routes"
v1 "{{ .PkgName }}/mirc/routes/v1"
v2 "{{ .PkgName }}/mirc/routes/v2"
)

//go:generate go run main.go
Expand Down
50 changes: 25 additions & 25 deletions mirc/cmd/templates_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions mirc/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
Expand Down Expand Up @@ -74,8 +75,10 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.6 h1:breEStsVwemnKh2/s6gMvSdMEkwW0sK8vGStnlVBMCs=
github.com/spf13/cobra v0.0.6/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down

0 comments on commit 186b117

Please sign in to comment.