diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ddcebb9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/test_plugin/ diff --git a/cmd/pb2port.go b/cmd/pb2port.go index f50a4dc..0d146b7 100644 --- a/cmd/pb2port.go +++ b/cmd/pb2port.go @@ -78,6 +78,8 @@ func (s *Pb2Port) run(_ *cobra.Command, arg []string) { return } if err == nil { + abs1, _ := filepath.Abs(s.out) + fmt.Printf("[warn] 原文件已存在 > %s:1\n", abs1) s.out = filepath.Base(s.out)[:len(filepath.Base(s.out))-len(filepath.Ext(s.out))] s.out = s.out + "_gen.erl" } @@ -91,7 +93,9 @@ func (s *Pb2Port) run(_ *cobra.Command, arg []string) { log.Printf("Err 写入文件失败: %v", err) return } - fmt.Println("ok") + + abs, _ := filepath.Abs(s.out) + fmt.Printf(" \n生成文件\n > %s:0\n", abs) } func init() { var singleSet = new(Pb2Port) diff --git a/cmd/single_dir.go b/cmd/single_dir.go index d1ab085..0496591 100644 --- a/cmd/single_dir.go +++ b/cmd/single_dir.go @@ -4,6 +4,8 @@ import ( "complie-erlang/config" tm "complie-erlang/template" "complie-erlang/worker" + "fmt" + "github.com/spf13/cobra" "gopkg.in/yaml.v2" "log" "os" @@ -62,7 +64,7 @@ func ExecuteSingleDir(args []string) { } for _, filename := range erlFiles { - log.Printf("[info] 检索到文件: %v %s\n", cfgPublic.Filename, filename) + fmt.Printf("[info] 检索到文件: %v %s:1\n", cfgPublic.Filename, filename) // 根据配置解析出 文件数据 parseTemKey, err := worker1.ParseTemKey(filename, cfgPublic) if err != nil { @@ -89,8 +91,23 @@ func ExecuteSingleDir(args []string) { log.Printf("[warn] 生成文件失败: %v %s", err, outFileNema) continue } - log.Printf("[info] 生成文件: %s\n", outFileNema) + fmt.Printf("[info] 生成文件: %s:1\n\n", outFileNema) } } } + +func init() { + var logsCmd = &cobra.Command{ + Use: "cfg", + Short: "配置生成", + Long: `根据 标注 构建 功能配置模版`, + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + args = []string{"config.yaml"} + } + ExecuteSingleDir(args) + }, + } + rootCmd.AddCommand(logsCmd) +} diff --git a/parser/erlang_module.go b/parser/erlang_module.go index 1a50b6a..2960a83 100644 --- a/parser/erlang_module.go +++ b/parser/erlang_module.go @@ -124,6 +124,14 @@ func parseFunctions(lines []string, exportedFunctions map[string]bool) []ErlangF inFunction = false currentFunction = ErlangFunction{} } + + // 函数结束条件:行以 ; 结束,且括号和花括号匹配 + if strings.HasSuffix(trimmedLine, ";") && braceCount == 0 && parenCount == 0 { + currentFunction.EndLine = i + 1 + functions = append(functions, currentFunction.ParseComments()) + inFunction = false + currentFunction = ErlangFunction{} + } } else { // 不在函数中时,遇到非注释行就清空注释缓存 if trimmedLine != "" && !strings.HasPrefix(trimmedLine, "%") { diff --git a/parser/zm_lib/public_tools.go b/parser/zm_lib/public_tools.go index e313fc7..a5eb46c 100644 --- a/parser/zm_lib/public_tools.go +++ b/parser/zm_lib/public_tools.go @@ -51,3 +51,22 @@ func FprintfArgsMap(layer int, out io.Writer, argsMap map[string]any) error { } return nil } + +// FindPathByWd 返回目录基于执行目录 +func FindPathByWd(path string) (string, error) { + return FindPathByBasePath(os.Getwd, path) +} + +// FindPathByExecutable 返回目录基于exe路径 +func FindPathByExecutable(path string) (string, error) { + return FindPathByBasePath(os.Executable, path) +} + +func FindPathByBasePath(basefun func() (basePath string, err error), path string) (string, error) { + basePath, err := basefun() + if err != nil { + return "", err + } + path = filepath.Join(filepath.Dir(basePath), path) + return path, nil +} diff --git a/template/template_engine.go b/template/template_engine.go index bb5e624..c239acf 100644 --- a/template/template_engine.go +++ b/template/template_engine.go @@ -1,6 +1,7 @@ package template import ( + "fmt" "strings" "text/template" ) @@ -18,6 +19,16 @@ var DefaultFuncMap = template.FuncMap{ splits := strings.Split(str, "/") return splits[len(splits)-1] }, + "exports": func(port []any) string { + var out []string + for _, p := range port { + p2 := p.(map[string]interface{}) + splits := strings.Split(p2["Cmd"].(string), "/") + funcName := splits[len(splits)-1] + out = append(out, fmt.Sprintf("%s/5", funcName)) + } + return strings.Join(out, ",") + }, } func hd(str []string) string {