feat: 优化项目创建命令,增加模板渲染支持

This commit is contained in:
Rogee
2025-09-10 14:17:11 +08:00
parent 1fac55115d
commit a714d4a3a9

View File

@@ -1,19 +1,20 @@
package cmd package cmd
import ( import (
"fmt" "bytes"
"io/fs" "fmt"
"os" "io/fs"
"path/filepath" "os"
"regexp" "path/filepath"
"strings" "regexp"
"text/template" "strings"
"text/template"
"go.ipao.vip/atomctl/templates" "github.com/pkg/errors"
"go.ipao.vip/atomctl/pkg/utils/gomod" log "github.com/sirupsen/logrus"
"github.com/pkg/errors" "github.com/spf13/cobra"
log "github.com/sirupsen/logrus" "go.ipao.vip/atomctl/pkg/utils/gomod"
"github.com/spf13/cobra" "go.ipao.vip/atomctl/templates"
) )
// 验证包名是否合法:支持域名、路径分隔符和常见字符 // 验证包名是否合法:支持域名、路径分隔符和常见字符
@@ -35,137 +36,144 @@ func CommandNewProject(root *cobra.Command) {
} }
func commandNewProjectE(cmd *cobra.Command, args []string) error { func commandNewProjectE(cmd *cobra.Command, args []string) error {
var ( var (
moduleName string moduleName string
inPlace bool inPlace bool
) )
if len(args) == 0 { if len(args) == 0 {
if _, err := os.Stat("go.mod"); err == nil { if _, err := os.Stat("go.mod"); err == nil {
pwd, _ := os.Getwd() pwd, _ := os.Getwd()
if err := gomod.Parse(filepath.Join(pwd, "go.mod")); err != nil { if err := gomod.Parse(filepath.Join(pwd, "go.mod")); err != nil {
return fmt.Errorf("parse go.mod failed: %v", err) return fmt.Errorf("parse go.mod failed: %v", err)
} }
moduleName = gomod.GetModuleName() moduleName = gomod.GetModuleName()
inPlace = true inPlace = true
} else { } else {
return fmt.Errorf("module name required or run inside an existing module (go.mod)") return fmt.Errorf("module name required or run inside an existing module (go.mod)")
} }
} else { } else {
moduleName = args[0] moduleName = args[0]
if !isValidGoPackageName(moduleName) { if !isValidGoPackageName(moduleName) {
return fmt.Errorf("invalid module name: %s, should be a valid go package name", moduleName) return fmt.Errorf("invalid module name: %s, should be a valid go package name", moduleName)
} }
} }
log.Info("创建项目: ", moduleName) log.Info("创建项目: ", moduleName)
var projectInfo struct { var projectInfo struct {
ModuleName string ModuleName string
ProjectName string ProjectName string
} }
projectInfo.ModuleName = moduleName projectInfo.ModuleName = moduleName
moduleSplitInfo := strings.Split(projectInfo.ModuleName, "/") moduleSplitInfo := strings.Split(projectInfo.ModuleName, "/")
projectInfo.ProjectName = moduleSplitInfo[len(moduleSplitInfo)-1] projectInfo.ProjectName = moduleSplitInfo[len(moduleSplitInfo)-1]
force, _ := cmd.Flags().GetBool("force") force, _ := cmd.Flags().GetBool("force")
rootDir := "." rootDir := "."
if !inPlace { if !inPlace {
rootDir = projectInfo.ProjectName rootDir = projectInfo.ProjectName
if _, err := os.Stat(rootDir); err == nil { if _, err := os.Stat(rootDir); err == nil {
if !force { if !force {
return fmt.Errorf("project directory %s already exists", rootDir) return fmt.Errorf("project directory %s already exists", rootDir)
} }
log.Warnf("强制删除已存在的目录: %s", rootDir) log.Warnf("强制删除已存在的目录: %s", rootDir)
if err := os.RemoveAll(rootDir); err != nil { if err := os.RemoveAll(rootDir); err != nil {
return fmt.Errorf("failed to remove existing directory: %v", err) return fmt.Errorf("failed to remove existing directory: %v", err)
} }
} }
if err := os.MkdirAll(rootDir, 0o755); err != nil { if err := os.MkdirAll(rootDir, 0o755); err != nil {
return fmt.Errorf("failed to create project directory: %v", err) return fmt.Errorf("failed to create project directory: %v", err)
} }
} }
if err := fs.WalkDir(templates.Project, "project", func(path string, d fs.DirEntry, err error) error { if err := fs.WalkDir(templates.Project, "project", func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
return err return err
} }
relPath, err := filepath.Rel("project", path) relPath, err := filepath.Rel("project", path)
if err != nil { if err != nil {
return err return err
} }
fileName := filepath.Base(relPath) fileName := filepath.Base(relPath)
if strings.HasPrefix(fileName, "-") { if strings.HasPrefix(fileName, "-") {
fileName = "." + strings.TrimPrefix(fileName, "-") fileName = "." + strings.TrimPrefix(fileName, "-")
relPath = filepath.Join(filepath.Dir(relPath), fileName) relPath = filepath.Join(filepath.Dir(relPath), fileName)
} }
targetPath := filepath.Join(rootDir, relPath) targetPath := filepath.Join(rootDir, relPath)
if d.IsDir() { if d.IsDir() {
log.Infof("创建目录: %s", targetPath) log.Infof("创建目录: %s", targetPath)
return os.MkdirAll(targetPath, 0o755) return os.MkdirAll(targetPath, 0o755)
} }
content, err := templates.Project.ReadFile(path) content, err := templates.Project.ReadFile(path)
if err != nil { if err != nil {
return err return err
} }
if strings.HasSuffix(path, ".tpl") { // 渲染判定:优先(.tpl/.raw) > 行内指令 > 模板分隔符
if inPlace && strings.HasSuffix(path, string(os.PathSeparator)+"go.mod.tpl") { isTpl := false
log.Infof("跳过已有文件: %s", filepath.Join(rootDir, "go.mod")) if strings.HasSuffix(path, ".tpl") {
return nil isTpl = true
} targetPath = strings.TrimSuffix(targetPath, ".tpl")
} else if strings.HasSuffix(path, ".raw") {
isTpl = false
targetPath = strings.TrimSuffix(targetPath, ".raw")
} else if bytes.Contains(content, []byte("atomctl:mode=tpl")) {
isTpl = true
} else if bytes.Contains(content, []byte("atomctl:mode=raw")) {
isTpl = false
} else if bytes.Contains(content, []byte("{{")) && bytes.Contains(content, []byte("}}")) {
isTpl = true
}
tmpl, err := template.New(filepath.Base(path)).Parse(string(content)) if inPlace && strings.HasSuffix(path, string(os.PathSeparator)+"go.mod.tpl") {
if err != nil { if _, err := os.Stat(filepath.Join(rootDir, "go.mod")); err == nil {
return err log.Infof("跳过已有文件: %s", filepath.Join(rootDir, "go.mod"))
} return nil
}
}
targetPath = strings.TrimSuffix(targetPath, ".tpl") if !force {
if !force { if _, err := os.Stat(targetPath); err == nil {
if _, err := os.Stat(targetPath); err == nil { log.Warnf("文件已存在,跳过: %s", targetPath)
log.Warnf("文件已存在,跳过: %s", targetPath) return nil
return nil }
} }
}
log.Infof("创建文件: %s", targetPath)
f, err := os.Create(targetPath)
if err != nil {
return errors.Wrapf(err, "创建文件失败 %s", targetPath)
}
defer f.Close()
return tmpl.Execute(f, projectInfo)
}
if strings.HasSuffix(path, ".raw") { if isTpl {
targetPath = strings.TrimSuffix(targetPath, ".raw") tmpl, err := template.New(filepath.Base(path)).Parse(string(content))
} if err != nil {
return err
}
log.Infof("[渲染] 文件: %s", targetPath)
f, err := os.Create(targetPath)
if err != nil {
return errors.Wrapf(err, "创建文件失败 %s", targetPath)
}
defer f.Close()
return tmpl.Execute(f, projectInfo)
}
if !force { log.Infof("[复制] 文件: %s", targetPath)
if _, err := os.Stat(targetPath); err == nil { return os.WriteFile(targetPath, content, 0o644)
log.Warnf("文件已存在,跳过: %s", targetPath) }); err != nil {
return nil return err
} }
}
log.Infof("创建文件: %s", targetPath)
return os.WriteFile(targetPath, content, 0o644)
}); err != nil {
return err
}
if inPlace { if inPlace {
log.Info("🎉 项目初始化成功 (当前目录)!") log.Info("🎉 项目初始化成功 (当前目录)!")
} else { } else {
log.Info("🎉 项目创建成功!") log.Info("🎉 项目创建成功!")
log.Info("后续步骤:") log.Info("后续步骤:")
log.Infof(" cd %s", projectInfo.ProjectName) log.Infof(" cd %s", projectInfo.ProjectName)
} }
log.Info(" go mod tidy") log.Info(" go mod tidy")
return nil return nil
} }