mod 命令完善
This commit is contained in:
+48
-13
@@ -16,7 +16,7 @@ import (
|
||||
|
||||
type ModWorker interface {
|
||||
Name() string
|
||||
LoadCfg(BaseSet, []string) error
|
||||
LoadCfg(BaseSet, []string, []config.DefaultArg) error
|
||||
OutPutFiles() ([]string, error)
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ type BaseSet struct {
|
||||
Conf config.ModConfig
|
||||
}
|
||||
|
||||
func ModWorkerRun(name string, args []string) error {
|
||||
func ModWorkerRun(name string, args []string, DefaultArgs []config.DefaultArg) error {
|
||||
|
||||
xmlPath, err := zm_lib.FindPathByExecutable("./mod_config.yaml")
|
||||
if err != nil {
|
||||
@@ -62,7 +62,7 @@ func ModWorkerRun(name string, args []string) error {
|
||||
for _, worker := range AllWorkers() {
|
||||
if worker.Name() == modWorkerName {
|
||||
log.Printf("[info] 初始化开始。。。")
|
||||
if err := worker.LoadCfg(baseSet, args); err != nil {
|
||||
if err := worker.LoadCfg(baseSet, args, DefaultArgs); err != nil {
|
||||
return fmt.Errorf("初始化 Err:%v", err)
|
||||
}
|
||||
log.Printf("[info] 初始化完成 开始生成输出。。。")
|
||||
@@ -100,7 +100,8 @@ func (mod *ProtoModWorker) Name() string {
|
||||
return "proto"
|
||||
}
|
||||
|
||||
func (mod *ProtoModWorker) LoadCfg(set BaseSet, Args []string) error {
|
||||
func (mod *ProtoModWorker) LoadCfg(set BaseSet, Args []string, DefaultArgs []config.DefaultArg) error {
|
||||
mod.DefaultArgs = DefaultArgs
|
||||
mod.set = set
|
||||
mod.Args = Args
|
||||
log.Println("[info] 加载配置 set:", set.Conf, "args:", Args)
|
||||
@@ -147,28 +148,62 @@ func (mod *ProtoModWorker) LoadCfg(set BaseSet, Args []string) error {
|
||||
}
|
||||
|
||||
func (mod *ProtoModWorker) OutPutFiles() ([]string, error) {
|
||||
var dirMap = make(map[string]string)
|
||||
for _, dir := range mod.set.Conf.MakeDir {
|
||||
dir.Path = filepath.Join(mod.pluginPath, fmt.Sprintf(dir.Path, mod.Args[0]))
|
||||
log.Println("[info] make outDir:", dir.Path)
|
||||
//if err := os.MkdirAll(dir.Path, os.ModePerm); err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
dirMap[dir.Name] = dir.Path
|
||||
if err := os.MkdirAll(dir.Path, os.ModePerm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var result []string
|
||||
for _, outFs := range mod.set.Conf.OutFiles {
|
||||
log.Println("[info] make file:", fmt.Sprintf(outFs.FileName, mod.Args[0]))
|
||||
template, err := mod.templates.ExecuteTemplate(outFs.ContentTemplate, mod.FormatArgs())
|
||||
|
||||
filename := fmt.Sprintf(outFs.FileName, mod.Args[0])
|
||||
|
||||
dir, is := dirMap[outFs.BaseDir]
|
||||
if !is {
|
||||
log.Println("[info] no find outDir:", outFs.BaseDir, dirMap)
|
||||
dir = filepath.Join(mod.pluginPath, fmt.Sprintf(outFs.BaseDir, mod.Args[0]))
|
||||
}
|
||||
filePath := filepath.Join(dir, filename)
|
||||
|
||||
log.Println("[info] make file:", filePath, dir)
|
||||
|
||||
result = append(result, filePath)
|
||||
|
||||
var baseArgs = map[string]any{
|
||||
"Module": filepath.Base(filename)[:len(filepath.Base(filename))-len(filepath.Ext(filename))],
|
||||
"Desc": mod.Args,
|
||||
}
|
||||
|
||||
template, err := mod.templates.ExecuteTemplate(outFs.ContentTemplate, mod.FormatArgs(baseArgs))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Printf("\n\n >:\n%s \n\n", template)
|
||||
// fmt.Printf("\n\n >:\n%s \n\n", template)
|
||||
_, err = os.Stat(filePath)
|
||||
if err == nil {
|
||||
log.Printf("[warn] 文件已存在: %s:1", filePath)
|
||||
continue
|
||||
}
|
||||
|
||||
if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = os.WriteFile(filePath, []byte(template), os.ModePerm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (mod *ProtoModWorker) FormatArgs() map[string]any {
|
||||
var templatesArgs = make(map[string]any)
|
||||
func (mod *ProtoModWorker) FormatArgs(baseArgs map[string]any) map[string]any {
|
||||
var templatesArgs = baseArgs
|
||||
var defaultArgs = make(map[string]any)
|
||||
|
||||
for _, defaultArg := range mod.DefaultArgs {
|
||||
|
||||
Reference in New Issue
Block a user