diff --git a/protoc-gen-go-crud/deliveries/wire.go b/protoc-gen-go-crud/deliveries/wire.go index fe5fdec..b1b3078 100644 --- a/protoc-gen-go-crud/deliveries/wire.go +++ b/protoc-gen-go-crud/deliveries/wire.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "path/filepath" "strings" "text/template" ) @@ -28,11 +29,17 @@ var ProviderSet = wire.NewSet({{ range $index, $element := .FunctionNames }}{{ i f := strings.Split(path, "V1") filename := fmt.Sprintf("./internal/%s/deliveries/wire.go", strings.ToLower(f[0])) - dirname := fmt.Sprintf("./internal/%s", strings.ToLower(f[0])) + oldWireName := fmt.Sprintf("./internal/%s/deliveries/deliveries.go", strings.ToLower(f[0])) // 判断文件是否存在 - if _, err := os.Stat(strings.TrimPrefix(dirname, "../")); err == nil { - log.Println("目录已存在,跳过生成:", dirname) + if _, err := os.Stat(strings.TrimPrefix(filename, "../")); err == nil { + log.Println("wire文件已存在,跳过生成") + return nil + } + + // 兼容历史数据 + if _, err := os.Stat(strings.TrimPrefix(oldWireName, "../")); err == nil { + log.Println("wire文件已存在,跳过生成") return nil } @@ -45,18 +52,29 @@ var ProviderSet = wire.NewSet({{ range $index, $element := .FunctionNames }}{{ i FunctionNames: functions, } + // 文件检查 + dir := filepath.Dir(filename) + if _, err := os.Stat(dir); os.IsNotExist(err) { + err := os.MkdirAll(dir, 0755) + if err != nil { + log.Fatalf("failed to create directory: %v", err) + } + } + // 检查文件是否存在,如果不存在则创建它 if _, err := os.Stat(filename); os.IsNotExist(err) { file, err := os.Create(filename) if err != nil { + log.Printf("create file err = %v\n", err) return err } defer file.Close() } // 打开文件 - file, err := os.OpenFile(filename, os.O_RDWR, 0644) + file, err := os.OpenFile(filename, os.O_RDWR, os.ModePerm) if err != nil { + log.Printf("open file err = %v\n", err) return err } defer file.Close() diff --git a/protoc-gen-go-crud/internal/authorization/deliveries/wire.go b/protoc-gen-go-crud/internal/authorization/deliveries/wire.go new file mode 100644 index 0000000..3f90fca --- /dev/null +++ b/protoc-gen-go-crud/internal/authorization/deliveries/wire.go @@ -0,0 +1,5 @@ +package deliveries + +import "github.com/google/wire" + +var ProviderSet = wire.NewSet(NewRoleService, NewAuthService) \ No newline at end of file diff --git a/protoc-gen-go-crud/internal/authorization/repositories/wire.go b/protoc-gen-go-crud/internal/authorization/repositories/wire.go new file mode 100644 index 0000000..7b2dc63 --- /dev/null +++ b/protoc-gen-go-crud/internal/authorization/repositories/wire.go @@ -0,0 +1,5 @@ +package repositories + +import "github.com/google/wire" + +var ProviderSet = wire.NewSet(NewRoleRepo, NewAuthRepo) \ No newline at end of file diff --git a/protoc-gen-go-crud/internal/authorization/usecase/wire.go b/protoc-gen-go-crud/internal/authorization/usecase/wire.go new file mode 100644 index 0000000..11e53df --- /dev/null +++ b/protoc-gen-go-crud/internal/authorization/usecase/wire.go @@ -0,0 +1,5 @@ +package usecase + +import "github.com/google/wire" + +var ProviderSet = wire.NewSet(NewRoleUseCase, NewAuthUseCase) \ No newline at end of file diff --git a/protoc-gen-go-crud/internal/users/deliveries/wire.go b/protoc-gen-go-crud/internal/users/deliveries/wire.go new file mode 100644 index 0000000..0c967e6 --- /dev/null +++ b/protoc-gen-go-crud/internal/users/deliveries/wire.go @@ -0,0 +1,5 @@ +package deliveries + +import "github.com/google/wire" + +var ProviderSet = wire.NewSet(NewUserService) \ No newline at end of file diff --git a/protoc-gen-go-crud/internal/users/repositories/wire.go b/protoc-gen-go-crud/internal/users/repositories/wire.go new file mode 100644 index 0000000..04b6c7d --- /dev/null +++ b/protoc-gen-go-crud/internal/users/repositories/wire.go @@ -0,0 +1,5 @@ +package repositories + +import "github.com/google/wire" + +var ProviderSet = wire.NewSet(NewUserRepo) \ No newline at end of file diff --git a/protoc-gen-go-crud/internal/users/usecase/wire.go b/protoc-gen-go-crud/internal/users/usecase/wire.go new file mode 100644 index 0000000..d4bb199 --- /dev/null +++ b/protoc-gen-go-crud/internal/users/usecase/wire.go @@ -0,0 +1,5 @@ +package usecase + +import "github.com/google/wire" + +var ProviderSet = wire.NewSet(NewUserUseCase) \ No newline at end of file diff --git a/protoc-gen-go-crud/protoc-gen-go-crud b/protoc-gen-go-crud/protoc-gen-go-crud index 2dade9c..e793bd4 100755 Binary files a/protoc-gen-go-crud/protoc-gen-go-crud and b/protoc-gen-go-crud/protoc-gen-go-crud differ diff --git a/protoc-gen-go-crud/repositories/wire.go b/protoc-gen-go-crud/repositories/wire.go index bcbbed4..7fa83fd 100644 --- a/protoc-gen-go-crud/repositories/wire.go +++ b/protoc-gen-go-crud/repositories/wire.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "path/filepath" "strings" "text/template" ) @@ -28,13 +29,19 @@ var ProviderSet = wire.NewSet({{ range $index, $element := .FunctionNames }}{{ i f := strings.Split(path, "V1") filename := fmt.Sprintf("./internal/%s/repositories/wire.go", strings.ToLower(f[0])) - dirname := fmt.Sprintf("./internal/%s", strings.ToLower(f[0])) + oldWireName := fmt.Sprintf("./internal/%s/repositories/repository.go", strings.ToLower(f[0])) // 判断文件是否存在 - if _, err := os.Stat(strings.TrimPrefix(dirname, "../")); err == nil { - log.Println("目录已存在,跳过生成:", dirname) + if _, err := os.Stat(strings.TrimPrefix(filename, "../")); err == nil { + log.Println("wire已存在,跳过生成:", filename) return nil } + // 兼容历史数据 + if _, err := os.Stat(strings.TrimPrefix(oldWireName, "../")); err == nil { + log.Println("wire文件已存在,跳过生成:", filename) + return nil + } + functions := []string{} for _, set := range sets { functions = append(functions, fmt.Sprintf("New%sRepo", set)) @@ -44,6 +51,15 @@ var ProviderSet = wire.NewSet({{ range $index, $element := .FunctionNames }}{{ i FunctionNames: functions, } + // 目录检查 + dir := filepath.Dir(filename) + if _, err := os.Stat(dir); os.IsNotExist(err) { + err := os.MkdirAll(dir, 0755) + if err != nil { + log.Fatalf("failed to create directory: %v", err) + } + } + // 检查文件是否存在,如果不存在则创建它 if _, err := os.Stat(filename); os.IsNotExist(err) { file, err := os.Create(filename) diff --git a/protoc-gen-go-crud/usecase/wire.go b/protoc-gen-go-crud/usecase/wire.go index 222f697..c99748c 100644 --- a/protoc-gen-go-crud/usecase/wire.go +++ b/protoc-gen-go-crud/usecase/wire.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "path/filepath" "strings" "text/template" ) @@ -28,11 +29,17 @@ var ProviderSet = wire.NewSet({{ range $index, $element := .FunctionNames }}{{ i f := strings.Split(path, "V1") filename := fmt.Sprintf("./internal/%s/usecase/wire.go", strings.ToLower(f[0])) - dirname := fmt.Sprintf("./internal/%s", strings.ToLower(f[0])) + oldWireName := fmt.Sprintf("./internal/%s/usecase/usecase.go", strings.ToLower(f[0])) // 判断文件是否存在 - if _, err := os.Stat(strings.TrimPrefix(dirname, "../")); err == nil { - log.Println("目录已存在,跳过生成:", dirname) + if _, err := os.Stat(strings.TrimPrefix(filename, "../")); err == nil { + log.Println("wire文件已存在,跳过生成:", filename) + return nil + } + + // 兼容历史数据 + if _, err := os.Stat(strings.TrimPrefix(oldWireName, "../")); err == nil { + log.Println("wire文件已存在,跳过生成:", filename) return nil } @@ -45,6 +52,15 @@ var ProviderSet = wire.NewSet({{ range $index, $element := .FunctionNames }}{{ i FunctionNames: functions, } + // 目录检查 + dir := filepath.Dir(filename) + if _, err := os.Stat(dir); os.IsNotExist(err) { + err := os.MkdirAll(dir, 0755) + if err != nil { + log.Fatalf("failed to create directory: %v", err) + } + } + // 检查文件是否存在,如果不存在则创建它 if _, err := os.Stat(filename); os.IsNotExist(err) { file, err := os.Create(filename)