XCLI 是一个智能项目配置生成工具,能够自动识别项目类型并生成相应的开发配置文件。
| 项目类型 | 识别依据 | 生成的配置文件 |
|---|---|---|
| Go | 存在 go.mod | Makefile |
| Vue | package.json 中包含 "vue" | .eslintrc.js, prettier.config.js |
| React | package.json 中包含 "react" | .eslintrc.js, prettier.config.js |
go build -o xcli main.go
在项目根目录下执行:
./xcli
工具会自动检测项目类型并生成相应的配置文件。
.
├── main.go # 程序入口
├── go.mod # Go 模块定义
├── pkg/
│ ├── detector/
│ │ └── detector.go # 项目类型探测器
│ ├── generator/
│ │ ├── generator.go # 生成器接口定义
│ │ ├── factory.go # 工厂模式:生成器创建
│ │ ├── templates.go # 模板处理逻辑
│ │ ├── go_generator.go # Go 项目生成器
│ │ ├── vue_generator.go # Vue 项目生成器
│ │ ├── react_generator.go # React 项目生成器
│ │ └── templates/ # 模板文件目录
│ │ ├── go/
│ │ │ └── Makefile.tmpl
│ │ ├── vue/
│ │ │ ├── eslintrc.js.tmpl
│ │ │ └── prettier.config.js.tmpl
│ │ └── react/
│ │ ├── eslintrc.js.tmpl
│ │ └── prettier.config.js.tmpl
│ └── utils/
│ └── file_utils.go # 文件工具函数
└── README.md
项目采用策略模式定义生成器接口,通过工厂模式根据项目类型创建对应的生成器实例:
┌─────────────┐ ┌─────────────┐ ┌─────────────────┐
│ Detector │────▶│ Factory │────▶│ Generator │
│ (探测类型) │ │ (创建生成器) │ │ (执行生成逻辑) │
└─────────────┘ └─────────────┘ └─────────────────┘
│
▼
┌─────────────┐
│ Templates │
│ (嵌入的模板) │
└─────────────┘
模板文件存放在 pkg/generator/templates/ 目录下,按项目类型分子目录存放:
.tmpl 后缀//go:embed 指令嵌入到可执行文件中.tmpl 后缀后生成目标文件pkg/detector/detector.go 中添加新的项目类型常量Detect() 函数中添加识别逻辑pkg/generator/ 下创建对应的生成器(如 xxx_generator.go)pkg/generator/factory.go 中注册生成器pkg/generator/templates/ 下创建模板目录和模板文件detector.go:
const (
// ...
TypeNode ProjectType = "NODE"
)
func Detect() ProjectType {
// ...
if utils.FileExists("package.json") {
return TypeNode
}
}
node_generator.go:
type NodeGenerator struct{}
func (n *NodeGenerator) Generate() error {
fmt.Println("🚀 正在为 Node.js 项目生成常用配置...")
return GenerateFromDir("node")
}
factory.go:
case detector.TypeNode:
return &NodeGenerator{}
MIT