feat: 新增 xlsx 配置导出工具及模板,可手动编译后使用
This commit is contained in:
107
planner/pce/exporter/cmd/exportgo.go
Normal file
107
planner/pce/exporter/cmd/exportgo.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/kercylan98/minotaur/planner/pce"
|
||||
"github.com/kercylan98/minotaur/planner/pce/cs"
|
||||
"github.com/kercylan98/minotaur/planner/pce/tmpls"
|
||||
"github.com/kercylan98/minotaur/utils/file"
|
||||
"github.com/kercylan98/minotaur/utils/hash"
|
||||
"github.com/kercylan98/minotaur/utils/str"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/tealeg/xlsx"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
var filePath, outPath, exclude string
|
||||
|
||||
exportGo := &cobra.Command{
|
||||
Use: "go",
|
||||
Short: "Export go language configuration code | 导出 go 语言配置代码",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
isDir, err := file.IsDir(outPath)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
isDir = filepath.Ext(outPath) == ""
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if isDir {
|
||||
_ = os.MkdirAll(outPath, os.ModePerm)
|
||||
outPath = filepath.Join(outPath, "config.go")
|
||||
} else {
|
||||
_ = os.MkdirAll(filepath.Dir(outPath), os.ModePerm)
|
||||
}
|
||||
|
||||
fpd, err := file.IsDir(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var xlsxFiles []string
|
||||
if fpd {
|
||||
files, err := os.ReadDir(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, f := range files {
|
||||
if f.IsDir() || !strings.HasSuffix(f.Name(), ".xlsx") || strings.HasPrefix(f.Name(), "~") {
|
||||
continue
|
||||
}
|
||||
xlsxFiles = append(xlsxFiles, filepath.Join(filePath, f.Name()))
|
||||
}
|
||||
} else {
|
||||
xlsxFiles = append(xlsxFiles, filePath)
|
||||
}
|
||||
|
||||
var golang []*pce.TmplStruct
|
||||
var exporter = pce.NewExporter()
|
||||
loader := pce.NewLoader(pce.GetFields())
|
||||
|
||||
excludes := hash.ToMapBool(str.SplitTrimSpace(exclude, ","))
|
||||
for _, xlsxFile := range xlsxFiles {
|
||||
xf, err := xlsx.OpenFile(xlsxFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, sheet := range xf.Sheets {
|
||||
cx := cs.NewXlsx(sheet, cs.XlsxExportTypeServer)
|
||||
if strings.HasPrefix(cx.GetDisplayName(), "#") || strings.HasPrefix(cx.GetConfigName(), "#") || excludes[cx.GetConfigName()] || excludes[cx.GetDisplayName()] {
|
||||
continue
|
||||
}
|
||||
golang = append(golang, loader.LoadStruct(cx))
|
||||
}
|
||||
}
|
||||
|
||||
if raw, err := exporter.ExportStruct(tmpls.NewGolang(filepath.Base(filepath.Dir(outPath))), golang...); err != nil {
|
||||
return err
|
||||
} else {
|
||||
if err := file.WriterFile(outPath, raw); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
_ = exec.Command("gofmt", "-w", outPath).Run()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
exportGo.Flags().StringVarP(&filePath, "xlsx", "f", "", "xlsx file path or directory path | xlsx 文件路径或所在目录路径")
|
||||
exportGo.Flags().StringVarP(&outPath, "output", "o", "", "output path | 输出的 go 文件路径")
|
||||
exportGo.Flags().StringVarP(&exclude, "exclude", "e", "", "excluded configuration names or display names (comma separated) | 排除的配置名或显示名(英文逗号分隔)")
|
||||
if err := exportGo.MarkFlagRequired("xlsx"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := exportGo.MarkFlagRequired("output"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
rootCmd.AddCommand(exportGo)
|
||||
}
|
||||
114
planner/pce/exporter/cmd/exportjson.go
Normal file
114
planner/pce/exporter/cmd/exportjson.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/kercylan98/minotaur/planner/pce"
|
||||
"github.com/kercylan98/minotaur/planner/pce/cs"
|
||||
"github.com/kercylan98/minotaur/planner/pce/tmpls"
|
||||
"github.com/kercylan98/minotaur/utils/file"
|
||||
"github.com/kercylan98/minotaur/utils/hash"
|
||||
"github.com/kercylan98/minotaur/utils/str"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/tealeg/xlsx"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
var filePath, outPath, exclude, exportType, prefix string
|
||||
|
||||
exportJson := &cobra.Command{
|
||||
Use: "json",
|
||||
Short: "Export json configuration data | 导出 json 配置数据",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
isDir, err := file.IsDir(outPath)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
isDir = filepath.Ext(outPath) == ""
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !isDir {
|
||||
return errors.New("output must be a directory path")
|
||||
}
|
||||
_ = os.MkdirAll(outPath, os.ModePerm)
|
||||
|
||||
fpd, err := file.IsDir(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var xlsxFiles []string
|
||||
if fpd {
|
||||
files, err := os.ReadDir(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, f := range files {
|
||||
if f.IsDir() || !strings.HasSuffix(f.Name(), ".xlsx") || strings.HasPrefix(f.Name(), "~") {
|
||||
continue
|
||||
}
|
||||
xlsxFiles = append(xlsxFiles, filepath.Join(filePath, f.Name()))
|
||||
}
|
||||
} else {
|
||||
xlsxFiles = append(xlsxFiles, filePath)
|
||||
}
|
||||
|
||||
var exporter = pce.NewExporter()
|
||||
loader := pce.NewLoader(pce.GetFields())
|
||||
|
||||
excludes := hash.ToMapBool(str.SplitTrimSpace(exclude, ","))
|
||||
for _, xlsxFile := range xlsxFiles {
|
||||
xf, err := xlsx.OpenFile(xlsxFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, sheet := range xf.Sheets {
|
||||
var cx *cs.Xlsx
|
||||
switch strings.TrimSpace(strings.ToLower(exportType)) {
|
||||
case "c":
|
||||
cx = cs.NewXlsx(sheet, cs.XlsxExportTypeClient)
|
||||
case "s":
|
||||
cx = cs.NewXlsx(sheet, cs.XlsxExportTypeServer)
|
||||
}
|
||||
if strings.HasPrefix(cx.GetDisplayName(), "#") || strings.HasPrefix(cx.GetConfigName(), "#") || excludes[cx.GetConfigName()] || excludes[cx.GetDisplayName()] {
|
||||
continue
|
||||
}
|
||||
|
||||
if raw, err := exporter.ExportData(tmpls.NewJSON(), loader.LoadData(cx)); err != nil {
|
||||
return err
|
||||
} else {
|
||||
jsonPath := filepath.Join(outPath, fmt.Sprintf("%s.%s.json", prefix, cx.GetConfigName()))
|
||||
if err := file.WriterFile(jsonPath, raw); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
exportJson.Flags().StringVarP(&filePath, "xlsx", "f", "", "xlsx file path or directory path | xlsx 文件路径或所在目录路径")
|
||||
exportJson.Flags().StringVarP(&outPath, "output", "o", "", "directory path of the output json file | 输出的 json 文件所在目录路径")
|
||||
exportJson.Flags().StringVarP(&exportType, "type", "t", "", "export server configuration[s] or client configuration[c] | 导出服务端配置[s]还是客户端配置[c]")
|
||||
exportJson.Flags().StringVarP(&prefix, "prefix", "p", "", "export configuration file name prefix | 导出配置文件名前缀")
|
||||
exportJson.Flags().StringVarP(&exclude, "exclude", "e", "", "excluded configuration names or display names (comma separated) | 排除的配置名或显示名(英文逗号分隔)")
|
||||
if err := exportJson.MarkFlagRequired("xlsx"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := exportJson.MarkFlagRequired("output"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := exportJson.MarkFlagRequired("type"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
rootCmd.AddCommand(exportJson)
|
||||
}
|
||||
23
planner/pce/exporter/cmd/root.go
Normal file
23
planner/pce/exporter/cmd/root.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
)
|
||||
|
||||
// rootCmd 在没有任何子命令的情况下调用时的基本命令
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "exporter",
|
||||
Short: "An exporter suitable for exporting xlsx configuration templates into go language configuration code and json data files. | 一个适合将 xlsx 配置模板导出为 go 语言配置代码和 json 数据文件的导出器。",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return cmd.Help()
|
||||
},
|
||||
}
|
||||
|
||||
// Execute 将所有子命令添加到根命令并适当设置标志。这是由 main.main() 调用的。 rootCmd 只需要发生一次
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
7
planner/pce/exporter/main.go
Normal file
7
planner/pce/exporter/main.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "github.com/kercylan98/minotaur/planner/pce/exporter/cmd"
|
||||
|
||||
func main() {
|
||||
cmd.Execute()
|
||||
}
|
||||
BIN
planner/pce/exporter/xlsx_template.xlsx
Normal file
BIN
planner/pce/exporter/xlsx_template.xlsx
Normal file
Binary file not shown.
Reference in New Issue
Block a user