## 主要改进 ### 架构重构 - 将单体 provider.go 拆分为多个专门的模块文件 - 实现了清晰的职责分离和模块化设计 - 遵循 SOLID 原则,提高代码可维护性 ### 新增功能 - **验证规则系统**: 实现了完整的 provider 验证框架 - **报告生成器**: 支持多种格式的验证报告 (JSON/HTML/Markdown/Text) - **解析器优化**: 重新设计了解析流程,提高性能和可扩展性 - **错误处理**: 增强了错误处理和诊断能力 ### 修复关键 Bug - 修复 @provider(job) 注解缺失 __job 注入参数的问题 - 统一了 job 和 cronjob 模式的处理逻辑 - 确保了 provider 生成的正确性和一致性 ### 代码质量提升 - 添加了完整的测试套件 - 引入了 golangci-lint 代码质量检查 - 优化了代码格式和结构 - 增加了详细的文档和规范 ### 文件结构优化 ``` pkg/ast/provider/ ├── types.go # 类型定义 ├── parser.go # 解析器实现 ├── validator.go # 验证规则 ├── report_generator.go # 报告生成 ├── renderer.go # 渲染器 ├── comment_parser.go # 注解解析 ├── modes.go # 模式定义 ├── errors.go # 错误处理 └── validator_test.go # 测试文件 ``` ### 兼容性 - 保持向后兼容性 - 支持现有的所有 provider 模式 - 优化了 API 设计和用户体验 This completes the implementation of T025-T029 tasks following TDD principles, including validation rules implementation and critical bug fixes.
100 lines
2.4 KiB
Go
100 lines
2.4 KiB
Go
package cmd
|
||
|
||
import (
|
||
"embed"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
"text/template"
|
||
|
||
"github.com/samber/lo"
|
||
"github.com/spf13/cobra"
|
||
"go.ipao.vip/atomctl/v2/templates"
|
||
)
|
||
|
||
func CommandGenService(root *cobra.Command) {
|
||
cmd := &cobra.Command{
|
||
Use: "service",
|
||
Short: "generate services",
|
||
Long: `扫描 --path 指定目录(默认 ./app/services)下的 Go 文件,汇总服务名并渲染生成 services.gen.go。
|
||
|
||
规则:
|
||
- 跳过 *_test.go 与 *.gen.go 文件,仅处理普通 .go 文件
|
||
- 以文件名作为服务名来源:
|
||
- PascalCase 作为 CamelName,用于导出类型名
|
||
- camelCase 作为 ServiceName,用于变量/字段名
|
||
- 使用内置模板 services/services.go.tpl 渲染
|
||
- 生成完成后会自动运行 gen provider 以补全注入`,
|
||
RunE: commandGenServiceE,
|
||
PostRunE: commandGenProviderE,
|
||
}
|
||
|
||
cmd.Flags().String("path", "./app/services", "base path to scan")
|
||
|
||
root.AddCommand(cmd)
|
||
}
|
||
|
||
func commandGenServiceE(cmd *cobra.Command, args []string) error {
|
||
path := cmd.Flag("path").Value.String()
|
||
|
||
files, err := os.ReadDir(path)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
type srv struct {
|
||
CamelName string
|
||
ServiceName string
|
||
}
|
||
|
||
// get services from files
|
||
var services []srv
|
||
for _, file := range files {
|
||
if file.IsDir() {
|
||
continue
|
||
}
|
||
name := file.Name()
|
||
if strings.HasSuffix(name, "_test.go") || strings.HasSuffix(name, ".gen.go") ||
|
||
!strings.HasSuffix(name, ".go") {
|
||
continue
|
||
}
|
||
|
||
name = strings.TrimSuffix(name, ".go")
|
||
|
||
services = append(services, srv{
|
||
CamelName: lo.PascalCase(name),
|
||
ServiceName: lo.CamelCase(name),
|
||
})
|
||
}
|
||
|
||
// 生成 services.gen.go 文件,使用 text/template 渲染 services.go.tpl 模板
|
||
if err := renderTemplateFS(templates.Services, "services/services.go.tpl", services, filepath.Join(path, "services.gen.go"), true); err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// renderTemplateFS 使用 text/template 渲染模板并写入目标文件
|
||
func renderTemplateFS(fs embed.FS, tplName string, data interface{}, targetPath string, overwrite bool) error {
|
||
tplContent, err := fs.ReadFile(tplName)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
tpl, err := template.New(tplName).Parse(string(tplContent))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !overwrite {
|
||
if _, err := os.Stat(targetPath); err == nil {
|
||
return nil // 文件已存在且不覆盖
|
||
}
|
||
}
|
||
f, err := os.Create(targetPath)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer f.Close()
|
||
return tpl.Execute(f, data)
|
||
}
|