Skip to content

Commit

Permalink
Merge pull request #24 from lazygophers/luoxin
Browse files Browse the repository at this point in the history
多语言支持
  • Loading branch information
Luoxin authored Jun 8, 2024
2 parents f2e4286 + 2f7005e commit 23338a5
Show file tree
Hide file tree
Showing 178 changed files with 6,363 additions and 84 deletions.
39 changes: 22 additions & 17 deletions cli/gen.go → cli/gen/gen.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package cli
package gen

import (
"errors"
"github.com/lazygophers/codegen/cli/utils"
"github.com/lazygophers/codegen/codegen"
"github.com/lazygophers/codegen/state"
"github.com/lazygophers/log"
Expand All @@ -16,8 +17,9 @@ import (
var pb *codegen.PbPackage

var genCmd = &cobra.Command{
Use: "gen",
Short: "gen",
Use: "gen",
Short: "gen",
Aliases: []string{"g", "generate"},
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
// NOTE: 合并输入参数与配置文件
mergeGenCmdFlags(cmd)
Expand All @@ -28,7 +30,7 @@ var genCmd = &cobra.Command{
return err
}

protoFile := filepath.Join(runtime.Pwd(), getString("input", cmd))
protoFile := filepath.Join(runtime.Pwd(), utils.GetString("input", cmd))
pterm.Info.Println("proto file:", protoFile)

// NOTE: 检查proto文件是否存在
Expand All @@ -53,56 +55,56 @@ var genCmd = &cobra.Command{
func mergeGenCmdFlags(cmd *cobra.Command) {

if cmd.Flag("protoc").Changed {
state.Config.ProtocPath = getString("protoc", cmd)
state.Config.ProtocPath = utils.GetString("protoc", cmd)
}

if cmd.Flag("protoc-gen-go").Changed {
state.Config.ProtoGenGoPath = getString("protoc-gen-go", cmd)
state.Config.ProtoGenGoPath = utils.GetString("protoc-gen-go", cmd)
}

if cmd.Flag("go-module-prefix").Changed {
state.Config.GoModulePrefix = strings.TrimSuffix(getString("go-module-prefix", cmd), "/")
state.Config.GoModulePrefix = strings.TrimSuffix(utils.GetString("go-module-prefix", cmd), "/")
}

if cmd.Flag("output-path").Changed {
state.Config.OutputPath = getString("output-path", cmd)
state.Config.OutputPath = utils.GetString("output-path", cmd)
}

if cmd.Flag("editorconfig-path").Changed {
state.Config.Template.Editorconfig = getString("editorconfig-path", cmd)
state.Config.Template.Editorconfig = utils.GetString("editorconfig-path", cmd)
}

if cmd.Flag("overwrite").Changed {
state.Config.Overwrite = getBool("overwrite", cmd)
state.Config.Overwrite = utils.GetBool("overwrite", cmd)
}

if cmd.Flag("proto-files").Changed {
state.Config.ProtoFiles = getStringSlice("proto-files", cmd)
state.Config.ProtoFiles = utils.GetStringSlice("proto-files", cmd)
}

if cmd.Flag("add-proto-files").Changed {
state.Config.ProtoFiles = append(state.Config.ProtoFiles, getStringSlice("proto-files", cmd)...)
state.Config.ProtoFiles = append(state.Config.ProtoFiles, utils.GetStringSlice("proto-files", cmd)...)
}

// NOTE: tables
if cmd.Flag("tables-enable_field_id").Changed {
state.Config.Tables.DisableFieldId = !getBool("tables-enable_field_id", cmd)
state.Config.Tables.DisableFieldId = !utils.GetBool("tables-enable_field_id", cmd)
}

if cmd.Flag("tables-enable_field_created_at").Changed {
state.Config.Tables.DisableFieldId = !getBool("tables-enable_field_created_at", cmd)
state.Config.Tables.DisableFieldId = !utils.GetBool("tables-enable_field_created_at", cmd)
}

if cmd.Flag("tables-enable_field_updated_at").Changed {
state.Config.Tables.DisableFieldUpdatedAt = !getBool("tables-enable_field_updated_at", cmd)
state.Config.Tables.DisableFieldUpdatedAt = !utils.GetBool("tables-enable_field_updated_at", cmd)
}

if cmd.Flag("tables-enable_field_deleted_at").Changed {
state.Config.Tables.DisableFieldDeletedAt = !getBool("tables-enable_field_deleted_at", cmd)
state.Config.Tables.DisableFieldDeletedAt = !utils.GetBool("tables-enable_field_deleted_at", cmd)
}

if cmd.Flag("tables-enable_gorm_tag_column").Changed {
state.Config.Tables.DisableGormTagColumn = !getBool("tables-enable_gorm_tag_column", cmd)
state.Config.Tables.DisableGormTagColumn = !utils.GetBool("tables-enable_gorm_tag_column", cmd)
}
}

Expand All @@ -126,5 +128,8 @@ func init() {

genCmd.PersistentFlags().StringP("input", "i", "", "input protobuf file")

}

func Load(rootCmd *cobra.Command) {
rootCmd.AddCommand(genCmd)
}
16 changes: 8 additions & 8 deletions cli/gen_add_rpc.go → cli/gen/gen_add_rpc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli
package gen

import (
"errors"
Expand All @@ -10,7 +10,7 @@ import (
"strings"
)

var genAddRpcCmd = &cobra.Command{
var addRpcCmd = &cobra.Command{
Use: "add-rpc",
Short: "add rpc to proto with model",
RunE: func(cmd *cobra.Command, args []string) (err error) {
Expand Down Expand Up @@ -81,14 +81,14 @@ var genAddRpcCmd = &cobra.Command{
}

func init() {
genAddRpcCmd.Flags().StringP("model", "m", "", "model name,must be specified")
genAddRpcCmd.Flags().StringP("gen-to", "t", "", "generate go source code path, will be used in gen go source code")
addRpcCmd.Flags().StringP("model", "m", "", "model name,must be specified")
addRpcCmd.Flags().StringP("gen-to", "t", "", "generate go source code path, will be used in gen go source code")

genAddRpcCmd.Flags().StringP("action", "a", "", "action for adding, segmente by ';',\nsupports: add/set/get/list/del/update")
addRpcCmd.Flags().StringP("action", "a", "", "action for adding, segmente by ';',\nsupports: add/set/get/list/del/update")

genAddRpcCmd.Flags().StringP("list-option", "l", "", "list options, segmente by ';'")
addRpcCmd.Flags().StringP("list-option", "l", "", "list options, segmente by ';'")

genAddRpcCmd.Flags().String("default-role", "", "default role")
addRpcCmd.Flags().String("default-role", "", "default role")

genCmd.AddCommand(genAddRpcCmd)
genCmd.AddCommand(addRpcCmd)
}
8 changes: 5 additions & 3 deletions cli/gen_all.go → cli/gen/gen_all.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli
package gen

import (
"github.com/lazygophers/log"
Expand All @@ -13,13 +13,15 @@ var GenAllHooks = []GenHook{
runEditorconfig,
runGoreleaser,
runMakefile,
runGitignote,
runDockerignote,
runGolangci,
runGenCmd,
runGenState,
runGenImpl,
}

var genAllCmd = &cobra.Command{
var allCmd = &cobra.Command{
Use: "all",
Short: "gen all action",
Long: `Generate all action.`,
Expand All @@ -37,5 +39,5 @@ var genAllCmd = &cobra.Command{
}

func init() {
genCmd.AddCommand(genAllCmd)
genCmd.AddCommand(allCmd)
}
6 changes: 3 additions & 3 deletions cli/gen_cache.go → cli/gen/gen_cache.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cli
package gen

import (
"github.com/lazygophers/codegen/codegen"
"github.com/lazygophers/log"
"github.com/spf13/cobra"
)

var genCacheCmd = &cobra.Command{
var cacheCmd = &cobra.Command{
Use: "cache",
Short: "generate cache file",
RunE: ranGenCache,
Expand All @@ -23,5 +23,5 @@ func ranGenCache(c *cobra.Command, args []string) (err error) {
}

func init() {
genCmd.AddCommand(genCacheCmd)
genCmd.AddCommand(cacheCmd)
}
6 changes: 3 additions & 3 deletions cli/gen_cmd.go → cli/gen/gen_cmd.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cli
package gen

import (
"github.com/lazygophers/codegen/codegen"
"github.com/lazygophers/log"
"github.com/spf13/cobra"
)

var genCmdCmd = &cobra.Command{
var cmdCmd = &cobra.Command{
Use: "cmd",
Short: "Generates cmd folder",
RunE: runGenCmd,
Expand All @@ -23,5 +23,5 @@ func runGenCmd(cmd *cobra.Command, args []string) (err error) {
}

func init() {
genCmd.AddCommand(genCmdCmd)
genCmd.AddCommand(cmdCmd)
}
6 changes: 3 additions & 3 deletions cli/gen_conf.go → cli/gen/gen_conf.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cli
package gen

import (
"github.com/lazygophers/codegen/codegen"
"github.com/lazygophers/log"
"github.com/spf13/cobra"
)

var genConfCmd = &cobra.Command{
var confCmd = &cobra.Command{
Use: "conf",
Short: "generate config file",
RunE: ranGenConf,
Expand All @@ -23,5 +23,5 @@ func ranGenConf(c *cobra.Command, args []string) (err error) {
}

func init() {
genCmd.AddCommand(genConfCmd)
genCmd.AddCommand(confCmd)
}
6 changes: 3 additions & 3 deletions cli/gen_editorconfig.go → cli/gen/gen_editorconfig.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cli
package gen

import (
"github.com/lazygophers/codegen/codegen"
"github.com/lazygophers/log"
"github.com/spf13/cobra"
)

var genEditorconfigCmd = &cobra.Command{
var editorconfigCmd = &cobra.Command{
Use: "editorconfig",
Short: "Generate .editorconfig file",
RunE: runEditorconfig,
Expand All @@ -23,5 +23,5 @@ func runEditorconfig(cmd *cobra.Command, args []string) (err error) {
}

func init() {
genCmd.AddCommand(genEditorconfigCmd)
genCmd.AddCommand(editorconfigCmd)
}
6 changes: 3 additions & 3 deletions cli/gen_golanngci.go → cli/gen/gen_golanngci.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cli
package gen

import (
"github.com/lazygophers/codegen/codegen"
"github.com/lazygophers/log"
"github.com/spf13/cobra"
)

var genGolangciCmd = &cobra.Command{
var golangciCmd = &cobra.Command{
Use: "golang-lint",
Short: "Generate .golangci.yml file",
RunE: runGolangci,
Expand All @@ -23,5 +23,5 @@ func runGolangci(cmd *cobra.Command, args []string) (err error) {
}

func init() {
genCmd.AddCommand(genGolangciCmd)
genCmd.AddCommand(golangciCmd)
}
6 changes: 3 additions & 3 deletions cli/gen_goreleaser.go → cli/gen/gen_goreleaser.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cli
package gen

import (
"github.com/lazygophers/codegen/codegen"
"github.com/lazygophers/log"
"github.com/spf13/cobra"
)

var genGoreleaserCmd = &cobra.Command{
var goreleaserCmd = &cobra.Command{
Use: "goreleaser",
Short: "Generate .goreleaser.yaml file",
RunE: runGoreleaser,
Expand All @@ -23,5 +23,5 @@ func runGoreleaser(cmd *cobra.Command, args []string) (err error) {
}

func init() {
genCmd.AddCommand(genGoreleaserCmd)
genCmd.AddCommand(goreleaserCmd)
}
43 changes: 43 additions & 0 deletions cli/gen/gen_ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package gen

import (
"github.com/lazygophers/codegen/codegen"
"github.com/lazygophers/log"
"github.com/spf13/cobra"
)

var gitignoreCmd = &cobra.Command{
Use: "gitignote",
Short: "Generate .gitignote file",
RunE: runGitignote,
}

func runGitignote(cmd *cobra.Command, args []string) (err error) {
err = codegen.GenerateGitignote(pb)
if err != nil {
log.Errorf("err:%v", err)
return err
}

return nil
}

var genDockerignoteCmd = &cobra.Command{
Use: "dockerignore",
Short: "Generate .dockerignore file",
RunE: runDockerignote,
}

func runDockerignote(cmd *cobra.Command, args []string) (err error) {
err = codegen.GenerateDockerignote(pb)
if err != nil {
log.Errorf("err:%v", err)
return err
}

return nil
}

func init() {
genCmd.AddCommand(gitignoreCmd, genDockerignoteCmd)
}
6 changes: 3 additions & 3 deletions cli/gen_impl.go → cli/gen/gen_impl.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cli
package gen

import (
"github.com/lazygophers/codegen/codegen"
"github.com/lazygophers/log"
"github.com/spf13/cobra"
)

var genImplCmd = &cobra.Command{
var iImplCmd = &cobra.Command{
Use: "impl",
Short: "Generate implementation files",
RunE: runGenImpl,
Expand Down Expand Up @@ -35,5 +35,5 @@ var runGenImpl = func(cmd *cobra.Command, args []string) (err error) {
}

func init() {
genCmd.AddCommand(genImplCmd)
genCmd.AddCommand(iImplCmd)
}
6 changes: 3 additions & 3 deletions cli/gen_makefile.go → cli/gen/gen_makefile.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cli
package gen

import (
"github.com/lazygophers/codegen/codegen"
"github.com/lazygophers/log"
"github.com/spf13/cobra"
)

var genMakefileCmd = &cobra.Command{
var makefileCmd = &cobra.Command{
Use: "makefile",
Short: "Generate makefile file",
RunE: runMakefile,
Expand All @@ -23,5 +23,5 @@ func runMakefile(cmd *cobra.Command, args []string) (err error) {
}

func init() {
genCmd.AddCommand(genMakefileCmd)
genCmd.AddCommand(makefileCmd)
}
Loading

0 comments on commit 23338a5

Please sign in to comment.