Skip to content

Commit dd13b14

Browse files
authored
feat(jzero): cmd jzero support plugin (#441)
feat(jzero): cmd jzero support plugin feat(jzero): add pkg/plugin for building plugins feat(jzero): add pkg/plugin for building plugins
1 parent 6db6e5b commit dd13b14

20 files changed

Lines changed: 536 additions & 33 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
.claude
44
dist
55
node_modules
6-
docs/package-lock.json
6+
docs/package-lock.json
7+
.omc

cmd/jzero/internal/command/gen/gen/gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func Run() error {
9393
}
9494

9595
// collectAndSaveMetadata 收集并保存项目元数据(复用已解析的数据)
96-
func collectAndSaveMetadata(apiSpecMap map[string]*spec.ApiSpec, protoSpecMap map[string]rpcparser.Proto) error {
96+
func collectAndSaveMetadata(apiSpecMap map[string]*spec.ApiSpec, protoSpecMap map[string]*rpcparser.Proto) error {
9797
if len(apiSpecMap) == 0 && len(protoSpecMap) == 0 {
9898
return nil
9999
}

cmd/jzero/internal/command/gen/genmodel/genmodel.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (jm *JzeroModel) Gen() error {
121121
sqlFiles []string
122122
genCodeSqlFiles []string
123123
)
124-
genCodeSqlSpecMap := make(map[string][]*ddlparser.Table)
124+
genCodeSqlSpecMap := make(map[string]*ddlparser.Table)
125125

126126
if !config.C.Gen.ModelDatasource {
127127
sqlFiles, err = jzerodesc.FindSqlFiles(config.C.SqlDir())
@@ -217,7 +217,7 @@ func (jm *JzeroModel) Gen() error {
217217
}
218218
mu.Lock()
219219
defer mu.Unlock()
220-
genCodeSqlSpecMap[f] = tableParsers
220+
genCodeSqlSpecMap[f] = tableParsers[0]
221221

222222
bf := strings.TrimSuffix(filepath.Base(f), ".sql")
223223

@@ -246,10 +246,8 @@ func (jm *JzeroModel) Gen() error {
246246
eg.SetLimit(len(genCodeSqlFiles))
247247
for _, f := range genCodeSqlFiles {
248248
eg.Go(func() error {
249-
tableParsers := genCodeSqlSpecMap[f]
250-
for _, tp := range tableParsers {
251-
genCodeTables = append(genCodeTables, tp.Name)
252-
}
249+
tableParser := genCodeSqlSpecMap[f]
250+
genCodeTables = append(genCodeTables, tableParser.Name)
253251
return generateModelFromSqlFile(f, goctlHome)
254252
})
255253
}

cmd/jzero/internal/command/gen/genrpc/genrpc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (l RegisterLines) String() string {
4141
return "\n\t\t" + strings.Join(l, "\n\t\t")
4242
}
4343

44-
func (jr *JzeroRpc) Gen() (map[string]rpcparser.Proto, error) {
44+
func (jr *JzeroRpc) Gen() (map[string]*rpcparser.Proto, error) {
4545
var (
4646
serverImports ImportLines
4747
pbImports ImportLines
@@ -58,7 +58,7 @@ func (jr *JzeroRpc) Gen() (map[string]rpcparser.Proto, error) {
5858
return nil, nil
5959
}
6060

61-
protoSpecMap := make(map[string]rpcparser.Proto, len(protoFiles))
61+
protoSpecMap := make(map[string]*rpcparser.Proto, len(protoFiles))
6262
for _, v := range protoFiles {
6363
// parse proto
6464
protoParser := rpcparser.NewDefaultProtoParser()
@@ -67,12 +67,12 @@ func (jr *JzeroRpc) Gen() (map[string]rpcparser.Proto, error) {
6767
if err != nil {
6868
return nil, err
6969
}
70-
protoSpecMap[v] = parse
70+
protoSpecMap[v] = &parse
7171
}
7272

7373
// 获取需要生成代码的proto 文件
7474
var genCodeProtoFiles []string
75-
genCodeProtoSpecMap := make(map[string]rpcparser.Proto, len(protoFiles))
75+
genCodeProtoSpecMap := make(map[string]*rpcparser.Proto, len(protoFiles))
7676

7777
switch {
7878
case config.C.Gen.GitChange && gitstatus.IsGitRepo(filepath.Join(config.C.Wd())) && len(config.C.Gen.Desc) == 0:

cmd/jzero/internal/command/gen/genrpc/logic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type LogicFile struct {
3636
ServerStream bool
3737
}
3838

39-
func (jr *JzeroRpc) GetAllLogicFiles(descFilepath string, protoSpec rpcparser.Proto) ([]LogicFile, error) {
39+
func (jr *JzeroRpc) GetAllLogicFiles(descFilepath string, protoSpec *rpcparser.Proto) ([]LogicFile, error) {
4040
var logicFiles []LogicFile
4141
for _, service := range protoSpec.Service {
4242
for _, rpc := range service.RPC {

cmd/jzero/internal/command/gen/genrpc/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (jr *JzeroRpc) genServer(serverImports, pbImports ImportLines, registerServ
5050
return nil
5151
}
5252

53-
func (jr *JzeroRpc) GetAllServerFiles(descFilepath string, protoSpec rpcparser.Proto) ([]ServerFile, error) {
53+
func (jr *JzeroRpc) GetAllServerFiles(descFilepath string, protoSpec *rpcparser.Proto) ([]ServerFile, error) {
5454
var serverFiles []ServerFile
5555
for _, service := range protoSpec.Service {
5656
namingFormat, err := format.FileNamingFormat(config.C.Style, service.Name+"Server")

cmd/jzero/internal/command/gen/genswagger/genswagger.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/jzero-io/jzero/cmd/jzero/internal/embeded"
2424
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/osx"
2525
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/stringx"
26-
"github.com/jzero-io/jzero/cmd/jzero/internal/plugin"
26+
"github.com/jzero-io/jzero/cmd/jzero/internal/serverless"
2727
)
2828

2929
func Gen() (err error) {
@@ -58,7 +58,7 @@ func Gen() (err error) {
5858
}
5959

6060
// 增加 plugins 的 api 文件
61-
plugins, err := plugin.GetPlugins()
61+
plugins, err := serverless.GetPlugins()
6262
if err == nil {
6363
for _, p := range plugins {
6464
if pathx.FileExists(filepath.Join(p.Path, "desc", "api")) {
@@ -359,7 +359,7 @@ func Gen() (err error) {
359359
}
360360

361361
// 增加 plugins 的 proto 文件
362-
plugins, err := plugin.GetPlugins()
362+
plugins, err := serverless.GetPlugins()
363363
if err == nil {
364364
for _, p := range plugins {
365365
if pathx.FileExists(filepath.Join(p.Path, "desc", "proto")) {

cmd/jzero/internal/command/gen/genzrpcclient/genzrpcclient.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/mod"
2626
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/osx"
2727
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/templatex"
28-
"github.com/jzero-io/jzero/cmd/jzero/internal/plugin"
28+
"github.com/jzero-io/jzero/cmd/jzero/internal/serverless"
2929
)
3030

3131
type DirContext struct {
@@ -152,7 +152,7 @@ func Generate(genModule bool) (err error) {
152152
return err
153153
}
154154

155-
plugins, _ := plugin.GetPlugins()
155+
plugins, _ := serverless.GetPlugins()
156156

157157
excludeThirdPartyProtoFiles, err := desc.FindExcludeThirdPartyProtoFiles(config.C.ProtoDir())
158158
if err != nil {
@@ -372,7 +372,7 @@ func Generate(genModule bool) (err error) {
372372
return nil
373373
}
374374

375-
func generatePluginFiles(plugins []plugin.Plugin) error {
375+
func generatePluginFiles(plugins []serverless.Plugin) error {
376376
if len(plugins) == 0 {
377377
return nil
378378
}
@@ -592,7 +592,7 @@ func generatePluginFiles(plugins []plugin.Plugin) error {
592592
return nil
593593
}
594594

595-
func genPluginNoRpcServiceExcludeThirdPartyProto(plugin plugin.Plugin, goModule, output string) error {
595+
func genPluginNoRpcServiceExcludeThirdPartyProto(plugin serverless.Plugin, goModule, output string) error {
596596
pluginProtoDir := filepath.Join(plugin.Path, "desc", "proto")
597597
pluginThirdPartyProtoDir := filepath.Join(plugin.Path, "desc", "proto", "third_party")
598598

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright © 2024 jaronnie <jaron@jaronnie.com>
3+
4+
*/
5+
6+
package plugin
7+
8+
import (
9+
"github.com/spf13/cobra"
10+
11+
"github.com/jzero-io/jzero/cmd/jzero/internal/command/plugin/pluginlist"
12+
"github.com/jzero-io/jzero/cmd/jzero/internal/command/plugin/pluginremove"
13+
)
14+
15+
var (
16+
// ValidPluginFilenamePrefixes defines the allowed plugin prefix to search
17+
ValidPluginFilenamePrefixes = []string{"jzero"}
18+
)
19+
20+
// pluginCmd represents the plugin command
21+
var pluginCmd = &cobra.Command{
22+
Use: "plugin",
23+
Short: "Manage jzero plugins",
24+
Long: `Provides utilities for interacting with plugins.
25+
Plugins provide extended functionality that is not part of the major command-line distribution.
26+
Plugins must be prefixed with "jzero-" and are stored in ~/.jzero/plugins.`,
27+
}
28+
29+
var pluginListCmd = &cobra.Command{
30+
Use: "list",
31+
Short: "List all installed jzero plugins",
32+
Long: `List all installed plugins from ~/.jzero/plugins directory.
33+
Plugins must be prefixed with "jzero-" to be recognized.`,
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
return pluginlist.Run()
36+
},
37+
}
38+
39+
var pluginRemoveCmd = &cobra.Command{
40+
Use: "remove <plugin-name>",
41+
Short: "Remove an installed jzero plugin",
42+
Long: `Remove an installed jzero plugin from ~/.jzero/plugins.
43+
44+
Example:
45+
jzero plugin remove jzero-myplugin`,
46+
Args: cobra.ExactArgs(1),
47+
RunE: func(cmd *cobra.Command, args []string) error {
48+
return pluginremove.Run(args[0])
49+
},
50+
}
51+
52+
func GetCommand() *cobra.Command {
53+
pluginCmd.AddCommand(pluginListCmd)
54+
pluginCmd.AddCommand(pluginRemoveCmd)
55+
56+
pluginListCmd.Flags().BoolVar(&pluginlist.NameOnly, "name-only", false, "If true, display only the binary name of each plugin, rather than its full path")
57+
pluginRemoveCmd.Flags().BoolVar(&pluginremove.Force, "force", false, "Force removal without confirmation")
58+
59+
return pluginCmd
60+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Copyright © 2024 jaronnie <jaron@jaronnie.com>
3+
4+
*/
5+
6+
package pluginlist
7+
8+
import (
9+
"errors"
10+
"fmt"
11+
"io/fs"
12+
"os"
13+
"path/filepath"
14+
"sort"
15+
"strings"
16+
17+
"github.com/zeromicro/go-zero/core/logx"
18+
)
19+
20+
var (
21+
NameOnly bool
22+
)
23+
24+
func Run() error {
25+
homeDir, err := os.UserHomeDir()
26+
if err != nil {
27+
logx.Errorf("Failed to get home directory: %v", err)
28+
return err
29+
}
30+
31+
pluginDir := filepath.Join(homeDir, ".jzero", "plugins")
32+
pluginsFound := false
33+
34+
// Check if plugin directory exists
35+
if _, err := os.Stat(pluginDir); errors.Is(err, fs.ErrNotExist) {
36+
fmt.Fprintf(os.Stderr, "Plugin directory %s does not exist. No plugins installed.\n", pluginDir)
37+
return nil
38+
}
39+
40+
// Collect plugins first
41+
var plugins []string
42+
err = filepath.WalkDir(pluginDir, func(path string, d fs.DirEntry, err error) error {
43+
if err != nil {
44+
return err
45+
}
46+
47+
// Skip directories
48+
if d.IsDir() {
49+
return nil
50+
}
51+
52+
// Check if file has valid prefix
53+
filename := filepath.Base(path)
54+
if !hasValidPrefix(filename, []string{"jzero"}) {
55+
return nil
56+
}
57+
58+
plugins = append(plugins, path)
59+
return nil
60+
})
61+
62+
if err != nil {
63+
logx.Errorf("Error walking plugin directory: %v", err)
64+
return err
65+
}
66+
67+
// Sort plugins
68+
sort.Strings(plugins)
69+
70+
// Display plugins
71+
if len(plugins) > 0 {
72+
fmt.Fprint(os.Stderr, "The following jzero plugins are installed:\n\n")
73+
pluginsFound = true
74+
75+
for _, pluginPath := range plugins {
76+
filename := filepath.Base(pluginPath)
77+
if NameOnly {
78+
fmt.Fprintf(os.Stdout, "%s\n", filename)
79+
} else {
80+
fmt.Fprintf(os.Stdout, "%s\n", pluginPath)
81+
}
82+
}
83+
}
84+
85+
if !pluginsFound {
86+
fmt.Fprintf(os.Stderr, "No jzero plugins found in %s\n", pluginDir)
87+
fmt.Fprint(os.Stderr, "\nPlugins must be prefixed with \"jzero-\" to be recognized.\n")
88+
}
89+
90+
return nil
91+
}
92+
93+
// hasValidPrefix checks if the filepath has a valid prefix
94+
func hasValidPrefix(filepath string, validPrefixes []string) bool {
95+
for _, prefix := range validPrefixes {
96+
if strings.HasPrefix(filepath, prefix+"-") {
97+
return true
98+
}
99+
}
100+
return false
101+
}

0 commit comments

Comments
 (0)