97 lines
2.4 KiB
Go
97 lines
2.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"complie-erlang/config"
|
|
tm "complie-erlang/template"
|
|
"complie-erlang/worker"
|
|
"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 {
|
|
log.Printf("[error] 获取可执行文件路径失败: %v", err)
|
|
return
|
|
}
|
|
|
|
bytes, err := os.ReadFile(xmlPath)
|
|
if err != nil {
|
|
log.Printf("[error] 读取配置文件失败: %v", err)
|
|
return
|
|
}
|
|
|
|
if err = yaml.Unmarshal(bytes, cfg); err != nil {
|
|
log.Printf("[error] 解析配置文件 %s 失败: %v", xmlPath, err)
|
|
return
|
|
}
|
|
|
|
// 获取可执行文件所在目录
|
|
exePath, err := os.Executable()
|
|
if err != nil {
|
|
log.Printf("[error] 获取可执行文件路径失败: %v", err)
|
|
return
|
|
}
|
|
|
|
// 加载模版报错
|
|
if err := templates.ParseGlob(filepath.Join(filepath.Dir(exePath), cfg.TemplateDirPattern)); err != nil {
|
|
log.Printf("[error] 加载模版报错: %v", err)
|
|
return
|
|
}
|
|
|
|
for _, cfgPublic := range cfg.Keywords {
|
|
// 查询目标文件
|
|
erlFiles, err := worker1.FindPatternErlFiles(cfgPublic.Filename)
|
|
if err != nil {
|
|
log.Printf("[error] FindPatternErlFiles: %v", err)
|
|
return
|
|
}
|
|
if len(erlFiles) == 0 {
|
|
// log.Printf("[info] 未检索到文件: %v", cfgPublic.Filename)
|
|
continue
|
|
}
|
|
|
|
for _, filename := range erlFiles {
|
|
log.Printf("[info] 检索到文件: %v %s\n", cfgPublic.Filename, filename)
|
|
// 根据配置解析出 文件数据
|
|
parseTemKey, err := worker1.ParseTemKey(filename, cfgPublic)
|
|
if err != nil {
|
|
log.Printf("[error] loading templates: %v", err)
|
|
return
|
|
}
|
|
|
|
// 输出文件夹
|
|
outDir, err := worker1.FindPluginCfgFiles(cfgPublic.OutputSep, cfgPublic.OutDirName, filename)
|
|
if err != nil {
|
|
log.Printf("[error] 查找输出文件夹: %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)
|
|
if err != nil {
|
|
log.Printf("[warn] 生成文件失败: %v %s", err, outFileNema)
|
|
continue
|
|
}
|
|
log.Printf("[info] 生成文件: %s\n", outFileNema)
|
|
}
|
|
}
|
|
|
|
}
|