Skip to content

Commit 55ff2fa

Browse files
committed
feat(hooks): support global hooks
feat(hooks): update hooks feat(hooks): update hooks feat(hooks): update hooks feat(hooks): update hooks feat(hooks): support global hooks feat(hooks): support global hooks feat(hooks): support global hooks feat(hooks): support global hooks
1 parent 449d297 commit 55ff2fa

File tree

4 files changed

+49
-99
lines changed

4 files changed

+49
-99
lines changed

cmd/gen.go

-98
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
"github.com/mitchellh/go-homedir"
1515
"github.com/spf13/cobra"
16-
"github.com/zeromicro/go-zero/core/color"
1716
"github.com/zeromicro/go-zero/tools/goctl/util/console"
1817
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
1918

@@ -25,30 +24,13 @@ import (
2524
"github.com/jzero-io/jzero/internal/gen/gensdk"
2625
"github.com/jzero-io/jzero/internal/gen/genswagger"
2726
"github.com/jzero-io/jzero/internal/gen/genzrpcclient"
28-
"github.com/jzero-io/jzero/pkg"
2927
"github.com/jzero-io/jzero/pkg/mod"
3028
)
3129

3230
// genCmd represents the gen command
3331
var genCmd = &cobra.Command{
3432
Use: "gen",
3533
Short: `Used to generate server/client code`,
36-
PreRunE: func(cmd *cobra.Command, args []string) error {
37-
if len(config.C.Gen.Hooks.Before) > 0 {
38-
fmt.Printf("%s hooks \n", color.WithColor("Start Before", color.FgGreen))
39-
}
40-
for _, v := range config.C.Gen.Hooks.Before {
41-
fmt.Printf("%s command %s\n", color.WithColor("Run", color.FgGreen), v)
42-
err := pkg.Run(v, config.C.Wd())
43-
if err != nil {
44-
return err
45-
}
46-
}
47-
if len(config.C.Gen.Hooks.Before) > 0 {
48-
fmt.Printf("%s\n", color.WithColor("Done", color.FgGreen))
49-
}
50-
return nil
51-
},
5234
RunE: func(cmd *cobra.Command, args []string) error {
5335
home, _ := os.UserHomeDir()
5436
config.C.Gen.Home, _ = homedir.Expand(config.C.Gen.Home)
@@ -58,45 +40,13 @@ var genCmd = &cobra.Command{
5840
embeded.Home = config.C.Gen.Home
5941
return gen.Run(false)
6042
},
61-
PostRunE: func(cmd *cobra.Command, args []string) error {
62-
if len(config.C.Gen.Hooks.After) > 0 {
63-
fmt.Printf("%s hooks \n", color.WithColor("Start After", color.FgGreen))
64-
}
65-
for _, v := range config.C.Gen.Hooks.After {
66-
fmt.Printf("%s command %s\n", color.WithColor("Run", color.FgGreen), v)
67-
err := pkg.Run(v, config.C.Wd())
68-
if err != nil {
69-
return err
70-
}
71-
}
72-
if len(config.C.Gen.Hooks.After) > 0 {
73-
fmt.Printf("%s\n", color.WithColor("Done", color.FgGreen))
74-
}
75-
return nil
76-
},
7743
SilenceUsage: true,
7844
}
7945

8046
// genZRpcClientCmd represents the rpcClient command
8147
var genZRpcClientCmd = &cobra.Command{
8248
Use: "zrpcclient",
8349
Short: `Gen zrpc client code by proto`,
84-
PreRunE: func(cmd *cobra.Command, args []string) error {
85-
if len(config.C.Gen.Zrpcclient.Hooks.Before) > 0 {
86-
fmt.Printf("%s hooks \n", color.WithColor("Start Before", color.FgGreen))
87-
}
88-
for _, v := range config.C.Gen.Zrpcclient.Hooks.Before {
89-
fmt.Printf("%s command %s\n", color.WithColor("Run", color.FgGreen), v)
90-
err := pkg.Run(v, config.C.Wd())
91-
if err != nil {
92-
return err
93-
}
94-
}
95-
if len(config.C.Gen.Zrpcclient.Hooks.Before) > 0 {
96-
fmt.Printf("%s\n", color.WithColor("Done", color.FgGreen))
97-
}
98-
return nil
99-
},
10050
RunE: func(cmd *cobra.Command, args []string) error {
10151
wd, err := os.Getwd()
10252
cobra.CheckErr(err)
@@ -119,22 +69,6 @@ var genZRpcClientCmd = &cobra.Command{
11969
}
12070
return genzrpcclient.Generate(genModule)
12171
},
122-
PostRunE: func(cmd *cobra.Command, args []string) error {
123-
if len(config.C.Gen.Zrpcclient.Hooks.After) > 0 {
124-
fmt.Printf("%s hooks \n", color.WithColor("Start After", color.FgGreen))
125-
}
126-
for _, v := range config.C.Gen.Zrpcclient.Hooks.After {
127-
fmt.Printf("%s command %s\n", color.WithColor("Run", color.FgGreen), v)
128-
err := pkg.Run(v, config.C.Wd())
129-
if err != nil {
130-
return err
131-
}
132-
}
133-
if len(config.C.Gen.Zrpcclient.Hooks.After) > 0 {
134-
fmt.Printf("%s\n", color.WithColor("Done", color.FgGreen))
135-
}
136-
return nil
137-
},
13872
}
13973

14074
// genSwaggerCmd represents the genSwagger command
@@ -150,22 +84,6 @@ var genSwaggerCmd = &cobra.Command{
15084
var genSdkCmd = &cobra.Command{
15185
Use: "sdk",
15286
Short: `Generate sdk client by api file and proto file`,
153-
PreRunE: func(cmd *cobra.Command, args []string) error {
154-
if len(config.C.Gen.Sdk.Hooks.Before) > 0 {
155-
fmt.Printf("%s hooks \n", color.WithColor("Start Before", color.FgGreen))
156-
}
157-
for _, v := range config.C.Gen.Sdk.Hooks.Before {
158-
fmt.Printf("%s command %s\n", color.WithColor("Run", color.FgGreen), v)
159-
err := pkg.Run(v, config.C.Wd())
160-
if err != nil {
161-
return err
162-
}
163-
}
164-
if len(config.C.Gen.Sdk.Hooks.Before) > 0 {
165-
fmt.Printf("%s\n", color.WithColor("Done", color.FgGreen))
166-
}
167-
return nil
168-
},
16987
RunE: func(cmd *cobra.Command, args []string) error {
17088
if config.C.Gen.Sdk.Language == "ts" {
17189
console.Warning("[warning] ts client is still working...")
@@ -212,22 +130,6 @@ var genSdkCmd = &cobra.Command{
212130
}
213131
return gensdk.GenSdk(genModule)
214132
},
215-
PostRunE: func(cmd *cobra.Command, args []string) error {
216-
if len(config.C.Gen.Sdk.Hooks.After) > 0 {
217-
fmt.Printf("%s hooks \n", color.WithColor("Start After", color.FgGreen))
218-
}
219-
for _, v := range config.C.Gen.Sdk.Hooks.After {
220-
fmt.Printf("%s command %s\n", color.WithColor("Run", color.FgGreen), v)
221-
err := pkg.Run(v, config.C.Wd())
222-
if err != nil {
223-
return err
224-
}
225-
}
226-
if len(config.C.Gen.Sdk.Hooks.After) > 0 {
227-
fmt.Printf("%s\n", color.WithColor("Done", color.FgGreen))
228-
}
229-
return nil
230-
},
231133
}
232134

233135
// genDocsCmd represents the genDocs command

cmd/root.go

+41
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ import (
1616
"github.com/spf13/cast"
1717
"github.com/spf13/cobra"
1818
"github.com/spf13/viper"
19+
"github.com/zeromicro/go-zero/core/color"
1920
"github.com/zeromicro/go-zero/core/logx"
2021
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
2122
"gopkg.in/yaml.v3"
2223

2324
"github.com/jzero-io/jzero/config"
25+
"github.com/jzero-io/jzero/pkg"
2426
)
2527

2628
var (
@@ -33,6 +35,9 @@ var rootCmd = &cobra.Command{
3335
Use: "jzero",
3436
Short: `Used to create project by templates and generate server/client code by proto and api file.
3537
`,
38+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
39+
return runHooks(cmd, "Before", "global", config.C.Hooks.Before)
40+
},
3641
Run: func(cmd *cobra.Command, args []string) {
3742
if parseBool, err := strconv.ParseBool(cmd.Flags().Lookup("version").Value.String()); err == nil && parseBool {
3843
getVersion()
@@ -42,6 +47,9 @@ var rootCmd = &cobra.Command{
4247
cobra.CheckErr(err)
4348
}
4449
},
50+
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
51+
return runHooks(cmd, "After", "global", config.C.Hooks.After)
52+
},
4553
}
4654

4755
// Execute adds all child commands to the root command and sets flags appropriately.
@@ -113,6 +121,28 @@ func initConfig() {
113121
}
114122
}
115123

124+
func runHooks(cmd *cobra.Command, hookAction, hooksName string, hooks []string) error {
125+
if os.Getenv("JZERO_HOOK_TRIGGERED") == "true" {
126+
return nil
127+
}
128+
129+
if len(hooks) > 0 {
130+
fmt.Printf("%s\n", color.WithColor(fmt.Sprintf("Start %s %s hooks", hookAction, hooksName), color.FgGreen))
131+
}
132+
for _, v := range hooks {
133+
fmt.Printf("%s command %s\n", color.WithColor("Run", color.FgGreen), v)
134+
err := pkg.Run(v, config.C.Wd(), "JZERO_HOOK_TRIGGERED=true")
135+
if err != nil {
136+
return err
137+
}
138+
}
139+
if len(hooks) > 0 {
140+
fmt.Printf("%s\n", color.WithColor("Done", color.FgGreen))
141+
}
142+
143+
return nil
144+
}
145+
116146
func traverseCommands(prefix string, cmd *cobra.Command) error {
117147
err := config.SetConfig(prefix, cmd.Flags())
118148
if err != nil {
@@ -124,6 +154,17 @@ func traverseCommands(prefix string, cmd *cobra.Command) error {
124154
if prefix == "" {
125155
newPrefix = subCommand.Use
126156
}
157+
158+
beforeHooks := viper.GetStringSlice(fmt.Sprintf("%s.hooks.before", newPrefix))
159+
afterHooks := viper.GetStringSlice(fmt.Sprintf("%s.hooks.after", newPrefix))
160+
161+
subCommand.PreRunE = func(cmd *cobra.Command, args []string) error {
162+
return runHooks(cmd, "Before", newPrefix, beforeHooks)
163+
}
164+
subCommand.PostRunE = func(cmd *cobra.Command, args []string) error {
165+
return runHooks(cmd, "After", newPrefix, afterHooks)
166+
}
167+
127168
err = traverseCommands(newPrefix, subCommand)
128169
if err != nil {
129170
return err

config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type Config struct {
1717
// global flags
1818
Debug bool `mapstructure:"debug"`
1919

20+
Hooks HooksConfig `mapstructure:"hooks"`
21+
2022
DebugSleepTime int `mapstructure:"debug-sleep-time"`
2123

2224
// new command

pkg/exec.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/zeromicro/go-zero/tools/goctl/vars"
1111
)
1212

13-
func Run(arg, dir string) error {
13+
func Run(arg, dir string, env ...string) error {
1414
goos := runtime.GOOS
1515
var cmd *exec.Cmd
1616
switch goos {
@@ -21,10 +21,15 @@ func Run(arg, dir string) error {
2121
default:
2222
return fmt.Errorf("unexpected os: %v", goos)
2323
}
24+
2425
if len(dir) > 0 {
2526
cmd.Dir = dir
2627
}
2728

29+
if len(env) > 0 {
30+
cmd.Env = append(os.Environ(), env...)
31+
}
32+
2833
stdout, err := cmd.StdoutPipe()
2934
if err != nil {
3035
return err

0 commit comments

Comments
 (0)