补充完成逻辑
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"complie-erlang/config"
|
||||
tm "complie-erlang/template"
|
||||
"complie-erlang/worker"
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v2"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExecuteSingleDir(args []string) {
|
||||
cfgFileName := args[0]
|
||||
|
||||
cfg := config.DefaultErlConfig()
|
||||
|
||||
templates := tm.NewTemplate()
|
||||
worker1 := worker.NewSingleFile(templates)
|
||||
|
||||
xmlPath, err := worker1.FindConfigXMLPath(cfgFileName)
|
||||
if err != nil {
|
||||
fmt.Printf("Err 获取可执行文件路径失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
bytes, err := os.ReadFile(xmlPath)
|
||||
if err != nil {
|
||||
fmt.Printf("Err 读取配置文件失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = yaml.Unmarshal(bytes, cfg); err != nil {
|
||||
fmt.Printf("Err 解析配置文件 %s 失败: %v", xmlPath, err)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取可执行文件所在目录
|
||||
exePath, err := os.Executable()
|
||||
if err != nil {
|
||||
log.Fatalf("获取可执行文件路径失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 加载模版报错
|
||||
if err := templates.ParseGlob(filepath.Join(filepath.Dir(exePath), cfg.TemplateDirPattern)); err != nil {
|
||||
log.Fatalf("Err 加载模版报错: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, cfgPublic := range cfg.Keywords {
|
||||
// 查询目标文件
|
||||
erlFiles, err := worker1.FindPatternErlFiles(cfgPublic.Filename)
|
||||
if err != nil {
|
||||
log.Fatalf("Err FindPatternErlFiles: %v", err)
|
||||
return
|
||||
}
|
||||
if len(erlFiles) == 0 {
|
||||
log.Printf("Info 未检索到文件: %v", cfgPublic.Filename)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, filename := range erlFiles {
|
||||
parseTemKey, err := worker1.ParseTemKey(filename, cfgPublic)
|
||||
if err != nil {
|
||||
log.Fatalf("loading templates: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 输出文件夹
|
||||
outDir, err := worker1.FindPluginCfgFiles(cfgPublic.OutputSep, cfgPublic.OutDirName, filename)
|
||||
if err != nil {
|
||||
log.Fatalf("Err 查找输出文件夹: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
baseName := strings.Replace(filepath.Base(filename), cfgPublic.InFileExt, cfgPublic.OutFileExt, 1)
|
||||
outFileNema := filepath.Join(outDir, baseName)
|
||||
|
||||
if len(parseTemKey) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
err = os.WriteFile(outFileNema, []byte(strings.Join(parseTemKey, "\n\n")), os.ModePerm)
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user