Skip to content

Commit

Permalink
update some logic for code gen
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 14, 2020
1 parent 40097f1 commit 8c1c3a9
Show file tree
Hide file tree
Showing 12 changed files with 3,164 additions and 7 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
go get github.com/gookit/ruxc
```

## Packages
## Dep Packages

- [rux](https://github.com/gookit/rux)
- [gcli](https://github.com/gookit/gcli)
- [ini](https://github.com/gookit/ini)
- https://github.com/swaggo/swag
- https://github.com/go-openapi/spec
- https://github.com/go-openapi/swag
- https://github.com/gookit/rux
- https://github.com/gookit/gcli
- https://github.com/gookit/ini
3 changes: 3 additions & 0 deletions cmd/http_serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package cmd


119 changes: 119 additions & 0 deletions cmd/swag2code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package cmd

import (
"errors"
"html/template"
"io/ioutil"
"path/filepath"
"strings"

"github.com/go-openapi/spec"
"github.com/go-openapi/swag"
"github.com/gookit/gcli/v2"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/ini/v2"
)

var swag2codeOpts = struct {
tplDir string
SwagFile string
Template string
OutDir string
GroupSuffix string
ActionSuffix string
}{
tplDir: "resource/templates",
}

var tplSuffix = ".tpl"

var tplFuncs = template.FuncMap{
"join": strings.Join,
}

var Swag2code = &gcli.Command{
Name: "swag2code",
UseFor: "generate rux API service codes by swagger.yaml or swagger.json",
Config: func(c *gcli.Command) {
c.StrOpt(&swag2codeOpts.SwagFile, "swagger-file", "f", "./swagger.json", "the swagger doc filepath")
c.StrOpt(&swag2codeOpts.OutDir, "output", "o", "./gocodes", `the output directory for generated codes
if input 'stdout' will print codes on terminal
`)
c.StrVar(&swag2codeOpts.Template, gcli.FlagMeta{
Name: "template",
Desc: "the template name for generate codes",
Shorts: []string{"t"},
DefVal: "rux-controller",
})
c.StrVar(&swag2codeOpts.GroupSuffix, gcli.FlagMeta{
Name: "group-suffix",
Desc: "Add suffix for group name. eg: API, Controller",
DefVal: "API",
})
c.StrVar(&swag2codeOpts.ActionSuffix, gcli.FlagMeta{
Name: "action-suffix",
Desc: "Add suffix for action name. eg: Action, Method",
})
},
Func: func(c *gcli.Command, args []string) (err error) {
swagFile := swag2codeOpts.SwagFile

var bts []byte
if swag.YAMLMatcher(swagFile) {
bts, err = swag.YAMLDoc(swagFile)
} else { // JSON file
bts, err = ioutil.ReadFile(swagFile)
}

if err != nil {
return err
}

doc := new(spec.Swagger)
err = doc.UnmarshalJSON(bts)

if len(doc.SwaggerProps.Paths.Paths) == 0 {
return errors.New("API doc 'paths' is empty")
}
dump.Config(func(d *dump.Dumper) {
d.MaxDepth = 8
})

tplDir := ini.String("swag2code.templateDir")
if tplDir != "" {
swag2codeOpts.tplDir = tplDir
}

generateByPathItem("/anything", doc.SwaggerProps.Paths.Paths["/anything"])

return
},
}

func generateByPathItem(path string, pathItem spec.PathItem) {
tmpPath := strings.Trim(path, "/")
group := tmpPath

var substr string
if strings.ContainsRune(tmpPath, '/') {
nodes := strings.SplitN(tmpPath, "/", 2)
group = nodes[0]
substr = nodes[1]
}

dump.P(group, substr)
dump.P(pathItem)

tplFile := swag2codeOpts.Template
if !strings.HasSuffix(tplFile, tplSuffix) {
tplFile += tplSuffix
}

if !fsutil.IsFile(tplFile) {
tplFile = filepath.Join(swag2codeOpts.tplDir, tplFile)
}

tpl := template.New("swag2code").Funcs(tplFuncs)
template.Must(tpl.ParseFiles(tplFile))
}
3 changes: 3 additions & 0 deletions cmd/swag_doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package cmd


9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ module github.com/gookit/ruxc

go 1.13

require github.com/gookit/gcli/v2 v2.3.3
require (
github.com/go-openapi/jsonreference v0.19.3 // indirect
github.com/go-openapi/spec v0.19.4
github.com/go-openapi/swag v0.19.5
github.com/gookit/gcli/v2 v2.3.3
github.com/gookit/goutil v0.3.1
github.com/gookit/ini/v2 v2.0.5
)
34 changes: 34 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-openapi/jsonpointer v0.19.2/go.mod h1:3akKfEdA7DF1sugOqz1dVQHBcuDBPKZGEoHC/NkiQRg=
github.com/go-openapi/jsonpointer v0.19.3 h1:gihV7YNZK1iK6Tgwwsxo2rJbD1GTbdm72325Bq8FI3w=
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonreference v0.19.2/go.mod h1:jMjeRr2HHw6nAVajTXJ4eiUwohSTlpa0o73RUL1owJc=
github.com/go-openapi/jsonreference v0.19.3 h1:5cxNfTy0UVC3X8JL5ymxzyoUZmo8iZb+jeTWn7tUa8o=
github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL98+wF9xc8zWvFonSJ8=
github.com/go-openapi/spec v0.19.4 h1:ixzUSnHTd6hCemgtAJgluaTSGYpLNpJY4mA2DIkdOAo=
github.com/go-openapi/spec v0.19.4/go.mod h1:FpwSN1ksY1eteniUU7X0N/BgJ7a4WvBFVA8Lj9mJglo=
github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/gookit/color v1.2.6 h1:f6/ehoHPXwi2tuntjpBRhpBhFLL9YjrnB2m6RWsbCRg=
github.com/gookit/color v1.2.6/go.mod h1:AhIE+pS6D4Ql0SQWbBeXPHw7gY0/sjHoA4s/n1KB7xg=
github.com/gookit/gcli/v2 v2.3.3 h1:3bGnLxFda8b7G/YsWVKWoZIzRgWBSbyCNELIhBi4MN8=
github.com/gookit/gcli/v2 v2.3.3/go.mod h1:V/FKnJUmFF5G3L3/zq9c22nO6jEWMZsgA36gAOOhHsw=
github.com/gookit/goutil v0.2.10/go.mod h1:Hk4DFtZTUY6CxsnMU3nysqUXYSm8KRattv4TqHXrUos=
github.com/gookit/goutil v0.3.1 h1:VpWkYhJ4tHqhTwa1DV7JeVK0TNdw6pNh8HB06XxQx4c=
github.com/gookit/goutil v0.3.1/go.mod h1:avL1wnSRDLyY4lC2FPAq5vNLwUB9JYaKWoUxx9lISNs=
github.com/gookit/ini/v2 v2.0.5 h1:AT24KFsTh80YPW0vW3KoBuaLDTWx68A0vWkrGbmuwKs=
github.com/gookit/ini/v2 v2.0.5/go.mod h1:z1Uhs+Kda+cL/2oLdW3uVH7dvo5e8cWORfZ6hieP4QA=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand All @@ -29,23 +51,35 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 h1:DZhuSZLsGlFL4CmhA8BcRA0mnthyA/nZ00AqCUo7vHg=
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM=
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200720211630-cb9d2d5c5666 h1:gVCS+QOncANNPlmlO1AhlU3oxs4V9z+gTtPwIk3p2N8=
golang.org/x/sys v0.0.0-20200720211630-cb9d2d5c5666/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190614205625-5aca471b1d59/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d h1:/iIZNFGxc/a7C3yWjGcnboV+Tkc7mxr+p6fDztwoxuM=
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
25 changes: 23 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
package main

import "github.com/gookit/gcli/v2"
import (
"github.com/gookit/color"
"github.com/gookit/gcli/v2"
"github.com/gookit/ini/v2"
"github.com/gookit/ruxc/cmd"
)

var configFile string

func init() {
err := ini.LoadExists("ruxc.ini")
if err != nil {
color.Error.Println("load config error:", err)
}
}

func main() {
app := gcli.NewApp(func(a *gcli.App) {
a.Name = "Ruxc Application"
a.Description = "CLI tool application for rux"

})
app.GOptsBinder = func(gf *gcli.Flags) {
gf.StrOpt(&configFile, "config", "c", "ruxc.ini", "the INI config file for ruxc")
}

loadCommands(app)

app.Run()
}

func loadCommands(app *gcli.App) {

app.AddCommand(cmd.Swag2code)
}
27 changes: 27 additions & 0 deletions resource/templates/rux-controller.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* auto generated by https://github.com/gookit/ruxc
*
* @author https://github.com/inhere
*/
package api

import (
"github.com/gookit/rux"
)

// {{ .GroupName }} {{ .GroupDesc }}
type {{ .GroupName }} struct {}

// AddRoutes register routes to the router
func (grp *Anything) AddRoutes(r *rux.Router) {
{{range _, $m := .Methods }}
r.{{ $m.METHOD }}("{{ $m.Path }}", grp.{{ $m.MethodName }})
{{end}}
}

{{range _, $m := .Methods }}
// {{ $m.MethodName }} {{ $m.MethodDesc }}
func (*{{ .GroupName}}) {{ $m.MethodName }}(c *rux.Context) {
c.Text(200, "hello")
}
{{end}}
5 changes: 5 additions & 0 deletions resource/templates/rux-single.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

func {{.MethodName}}(c *rux.Context) {
c.Text(200, "hello")
}

7 changes: 7 additions & 0 deletions ruxc.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# config file for ruxc
# homepage: https://github.com/gookit/ruxc
#

[swag2code]
templateDir = resource/templates

Loading

0 comments on commit 8c1c3a9

Please sign in to comment.