From 77b6f2509412753a80f09ce5122835165511d11a Mon Sep 17 00:00:00 2001 From: jinzhu Date: Fri, 22 Apr 2022 18:19:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E8=AF=8D=E4=BF=AE=E5=A4=8D=E8=BD=AC?= =?UTF-8?q?=E4=B9=89=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 9 +++++++++ console/commands/bean.go | 11 ++++++++++- console/commands/protoc.go | 13 ++++++------- console/kernel.go | 7 +++++++ parser/go.go | 28 +++++++++++++++++++++++----- parser/help_file.go | 4 ++-- 6 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4b505bc --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +GOBIN := $(shell go env GOBIN) +ATDIR := $(shell pwd) + +# mac 系统更新path可能不全 +export PATH := $(GOBIN):$(PATH) + +build: + go build -ldflags="-w -s" -o $(GOBIN)/toolset ./ + diff --git a/console/commands/bean.go b/console/commands/bean.go index c6d7fac..7cc5268 100644 --- a/console/commands/bean.go +++ b/console/commands/bean.go @@ -25,6 +25,11 @@ func (BeanCommand) Configure() command.Configure { }, }, Option: []command.ArgParam{ + { + Name: "name", + Description: "跳过目录", + Default: "New{name}", + }, { Name: "scan", Description: "扫码目录下的源码; shell(pwd)", @@ -40,7 +45,11 @@ func (BeanCommand) Configure() command.Configure { } } +var newName = "New{name}" + func (BeanCommand) Execute(input command.Input) { + newName = input.GetOption("name") + root := getRootPath() scan := input.GetOption("scan") scan = strings.Replace(scan, "@root", root, 1) @@ -208,7 +217,7 @@ func genInitializeNewStr(name string) string { name = name[1:] } - return "New" + name + return strings.Replace(newName, "{name}", name, 1) } // 生成 import => alias diff --git a/console/commands/protoc.go b/console/commands/protoc.go index 99f8ead..7e5bdb3 100644 --- a/console/commands/protoc.go +++ b/console/commands/protoc.go @@ -31,18 +31,13 @@ func (ProtocCommand) Configure() command.Configure { { Name: "proto_path", Description: "protoc后面拼接的proto_path, 可以传入多个", - Default: "@root/protobuf/common/http", + Default: "@root/protobuf/common", }, { Name: "go_out", Description: "生成文件到指定目录", Default: "@root/generate/proto", }, - { - Name: "show", - Description: "是否打印protoc命令", - Default: "false", - }, }, }, } @@ -51,7 +46,7 @@ func (ProtocCommand) Configure() command.Configure { var show = false func (ProtocCommand) Execute(input command.Input) { - show = input.GetOption("show") != "false" + show = input.GetOption("debug") != "false" root := getRootPath() _, err := exec.LookPath("protoc") if err != nil { @@ -71,6 +66,10 @@ func (ProtocCommand) Execute(input command.Input) { for _, s := range input.GetOptions("proto_path") { s = strings.Replace(s, "@root", root, 1) pps = append(pps, "--proto_path="+s) + // 子目录也加入进来 + for _, dir := range parser.GetChildrenDir(s) { + pps = append(pps, "--proto_path="+dir.Path) + } } // path/*.proto 不是protoc命令提供的, 如果这里执行需要每一个文件一个命令 for _, dir := range parser.GetChildrenDir(path) { diff --git a/console/kernel.go b/console/kernel.go index 609fb88..1e29e35 100644 --- a/console/kernel.go +++ b/console/kernel.go @@ -25,6 +25,13 @@ func (k *Kernel) Run() { return val, true }, }) + app.AddBaseOption(command.ArgParam{ + Name: "debug", + Description: "是否显示明细", + Call: func(val string, c *command.Console) (string, bool) { + return "true", true + }, + }) for _, provider := range commands.GetAllProvider() { if v, ok := provider.(command.Command); ok { diff --git a/parser/go.go b/parser/go.go index 526de9b..6729a65 100644 --- a/parser/go.go +++ b/parser/go.go @@ -2,6 +2,7 @@ package parser import ( "fmt" + "os" "strings" ) @@ -115,7 +116,8 @@ func GetFileParser(path string) (GoFileParser, error) { for _, w := range nl { str += w.Str } - fmt.Println("文件块作用域似乎解析有错误", path, offset, str) + fmt.Println("文件块作用域似乎解析有错误\n", path, "\n", offset, "\n", str) + os.Exit(1) } } } @@ -224,11 +226,13 @@ func (d GoDoc) GetAlias() string { if w.Ty == wordT_word { if w.Str == "Bean" { if l[i-1].Str == "@" { - num = 1 + num = i } - } else if num == 1 { + } else if num == -1 { return w.Str[1 : len(w.Str)-1] } + } else if num == -i && w.Str == "(" { + num = -1 } } return "" @@ -386,6 +390,7 @@ func handleFunds(l []*word, offset int) (GoFunc, int) { interCount++ } } + if interCount != 0 { for i := 0; i <= interCount; i++ { _, et := GetBrackets(l[offset:], "{", "}") @@ -394,7 +399,8 @@ func handleFunds(l []*word, offset int) (GoFunc, int) { } else { offset = offset + et } - return GoFunc{Name: name}, offset + + return GoFunc{Name: name}, offset + 1 } func handleCosts(l []*word, offset int) (map[string]string, int) { return handleVars(l, offset) @@ -411,7 +417,11 @@ func handleVars(l []*word, offset int) (map[string]string, int) { nl := l[offset+last-1:] offset = offset + last - 1 _, et := GetBrackets(nl, "{", "}") - return nil, offset + et + 1 + offset = offset + et + 1 + // 检查是否 struct 实例化 + _, et = GetBrackets(l[offset:], "{", "}") + offset = offset + et + 1 + return nil, offset } else { nl := l[offset:] _, et := GetBrackets(nl, "{", "}") @@ -428,3 +438,11 @@ func handleVars(l []*word, offset int) (map[string]string, int) { return nil, offset + et + 1 } } + +func toStr(l []*word) string { + s := "" + for _, w := range l { + s += w.Str + } + return s +} diff --git a/parser/help_file.go b/parser/help_file.go index b271640..6230fcb 100644 --- a/parser/help_file.go +++ b/parser/help_file.go @@ -199,7 +199,7 @@ func getWordsWitchFile(path string) GoWords { case scannerStatus_quote: work = work + str stop = true - if str == "\"" && !HasSuffix(work, "\\\"") { + if str == "\"" && (!HasSuffix(work, "\\\"") || HasSuffix(work, "\\\\\"")) { got.list = append(got.list, &word{ Str: work, Ty: wordT_word, @@ -211,7 +211,7 @@ func getWordsWitchFile(path string) GoWords { case scannerStatus_quote2: work = work + str stop = true - if str == "'" { + if str == "'" && (!HasSuffix(work, "\\'") || HasSuffix(work, "\\\\'")) { got.list = append(got.list, &word{ Str: work, Ty: wordT_word,