Skip to content

Commit

Permalink
feat:wire文件生成逻辑判断
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiaosu committed Jun 30, 2023
1 parent f9746f0 commit 69d12b1
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 10 deletions.
26 changes: 22 additions & 4 deletions protoc-gen-go-crud/deliveries/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"text/template"
)
Expand All @@ -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
}

Expand All @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions protoc-gen-go-crud/internal/authorization/deliveries/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package deliveries

import "github.com/google/wire"

var ProviderSet = wire.NewSet(NewRoleService, NewAuthService)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package repositories

import "github.com/google/wire"

var ProviderSet = wire.NewSet(NewRoleRepo, NewAuthRepo)
5 changes: 5 additions & 0 deletions protoc-gen-go-crud/internal/authorization/usecase/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package usecase

import "github.com/google/wire"

var ProviderSet = wire.NewSet(NewRoleUseCase, NewAuthUseCase)
5 changes: 5 additions & 0 deletions protoc-gen-go-crud/internal/users/deliveries/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package deliveries

import "github.com/google/wire"

var ProviderSet = wire.NewSet(NewUserService)
5 changes: 5 additions & 0 deletions protoc-gen-go-crud/internal/users/repositories/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package repositories

import "github.com/google/wire"

var ProviderSet = wire.NewSet(NewUserRepo)
5 changes: 5 additions & 0 deletions protoc-gen-go-crud/internal/users/usecase/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package usecase

import "github.com/google/wire"

var ProviderSet = wire.NewSet(NewUserUseCase)
Binary file modified protoc-gen-go-crud/protoc-gen-go-crud
Binary file not shown.
22 changes: 19 additions & 3 deletions protoc-gen-go-crud/repositories/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"text/template"
)
Expand All @@ -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))
Expand All @@ -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)
Expand Down
22 changes: 19 additions & 3 deletions protoc-gen-go-crud/usecase/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"text/template"
)
Expand All @@ -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
}

Expand All @@ -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)
Expand Down

0 comments on commit 69d12b1

Please sign in to comment.