2020-07-29 17:11:41 +08:00
|
|
|
package javagen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/logrusorgru/aurora"
|
2022-05-07 15:40:11 +08:00
|
|
|
"github.com/spf13/cobra"
|
2022-01-25 23:15:07 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
2020-07-29 17:11:41 +08:00
|
|
|
)
|
|
|
|
|
2022-05-07 15:40:11 +08:00
|
|
|
var (
|
|
|
|
// VarStringDir describes a directory.
|
|
|
|
VarStringDir string
|
|
|
|
// VarStringAPI describes an API.
|
|
|
|
VarStringAPI string
|
|
|
|
)
|
|
|
|
|
2022-04-04 13:12:05 +08:00
|
|
|
// JavaCommand generates java code command entrance.
|
2022-05-07 15:40:11 +08:00
|
|
|
func JavaCommand(_ *cobra.Command, _ []string) error {
|
|
|
|
apiFile := VarStringAPI
|
|
|
|
dir := VarStringDir
|
2020-07-29 17:11:41 +08:00
|
|
|
if len(apiFile) == 0 {
|
|
|
|
return errors.New("missing -api")
|
|
|
|
}
|
|
|
|
if len(dir) == 0 {
|
|
|
|
return errors.New("missing -dir")
|
|
|
|
}
|
|
|
|
|
2021-01-11 15:10:51 +08:00
|
|
|
api, err := parser.Parse(apiFile)
|
2020-07-29 17:11:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-21 15:22:43 +08:00
|
|
|
if err := api.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-03 20:57:03 +08:00
|
|
|
api.Service = api.Service.JoinPrefix()
|
2021-09-29 13:09:20 +08:00
|
|
|
packetName := strings.TrimSuffix(api.Service.Name, "-api")
|
2022-01-03 21:32:40 +08:00
|
|
|
logx.Must(pathx.MkdirIfNotExist(dir))
|
2020-08-14 15:08:06 +08:00
|
|
|
logx.Must(genPacket(dir, packetName, api))
|
|
|
|
logx.Must(genComponents(dir, packetName, api))
|
2020-07-29 17:11:41 +08:00
|
|
|
|
|
|
|
fmt.Println(aurora.Green("Done."))
|
|
|
|
return nil
|
|
|
|
}
|