Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into i18n
  • Loading branch information
MohamedHassanNasr committed Oct 24, 2024
2 parents 2feef35 + 2938304 commit 5049476
Showing 1 changed file with 79 additions and 165 deletions.
244 changes: 79 additions & 165 deletions server/service/system/auto_code_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
utilsAst "github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
"github.com/pkg/errors"
"github.com/tidwall/sjson"
"go/ast"
"go/format"
"go/parser"
Expand Down Expand Up @@ -56,7 +57,6 @@ func (s *autoCodeTemplate) checkPackage(Pkg string, template string) (err error)
// Create 创建生成自动化代码
func (s *autoCodeTemplate) Create(ctx context.Context, info request.AutoCode) error {
history := info.History()
i18nJson := ""
var autoPkg model.SysAutoCodePackage
err := global.GVA_DB.WithContext(ctx).Where("package_name = ?", info.Package).First(&autoPkg).Error
if err != nil {
Expand Down Expand Up @@ -110,15 +110,11 @@ func (s *autoCodeTemplate) Create(ctx context.Context, info request.AutoCode) er
if err != nil {
return err
}
apiDescJson, err := s.getDescriptionJson(info)
if err != nil {
return err
}
getGroupJson, err := s.getGroupJson(info)

err = s.WriteApiJson(info)
if err != nil {
return err
}
i18nJson, err = s.mergeJSON(getGroupJson, apiDescJson)
}

// 自动创建menu
Expand Down Expand Up @@ -154,29 +150,10 @@ func (s *autoCodeTemplate) Create(ctx context.Context, info request.AutoCode) er
}
}
history.MenuID = id
menuJson, err := s.getMenuJson(info)
if err != nil {
return err
}
i18nJson, err = s.mergeJSON(menuJson, i18nJson)
err = s.WriteMenuJson(info)
if err != nil {
return err
}

}

if i18nJson != "" {
localPath := path.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "resource", "lang")
files, err := s.readJSONFiles(localPath)
if err != nil {
return err
}
for _, file := range files {
err = s.reWriteI18nJson(file, i18nJson)
if err != nil {
return err
}
}
}

if info.HasExcel {
Expand Down Expand Up @@ -216,25 +193,11 @@ func (s *autoCodeTemplate) Create(ctx context.Context, info request.AutoCode) er
return err
}

webI18nJson, err := s.createJson(info)

if err != nil {
return err
}

localPath := path.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Web, "locales")

files, err := s.readJSONFiles(localPath)
err = s.WriteWebJson(info)
if err != nil {
return err
}

for _, file := range files {
err := s.reWriteI18nJson(file, webI18nJson)
if err != nil {
return err
}
}
return nil
}

Expand Down Expand Up @@ -463,42 +426,93 @@ func (s *autoCodeTemplate) addTemplateToFile(t string, info request.AutoFunc) er
return nil
}

func (s *autoCodeTemplate) mergeJSON(json1, json2 string) (string, error) {
var obj1 map[string]interface{}
var obj2 map[string]interface{}

err := json.Unmarshal([]byte(json1), &obj1)
func (s *autoCodeTemplate) WriteMenuJson(info request.AutoCode) error {
localPath := path.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "resource", "lang")
jsonFiles, err := s.readJSONFiles(localPath)
if err != nil {
return "", err
return err
}
for _, file := range jsonFiles {
data, e := os.ReadFile(file)
if e != nil {
return e
}
modifiedData, je := sjson.SetBytes(data, fmt.Sprintf("system.menu.%s%s", info.Package, info.StructName), info.Description)
if je != nil {
return je
}
err = os.WriteFile(file, modifiedData, 0666)
if err != nil {
return err
}
}
return nil
}

err = json.Unmarshal([]byte(json2), &obj2)
func (s *autoCodeTemplate) WriteApiJson(info request.AutoCode) error {
localPath := path.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "resource", "lang")
jsonFiles, err := s.readJSONFiles(localPath)
if err != nil {
return "", err
return err
}
for _, file := range jsonFiles {
data, e := os.ReadFile(file)
if e != nil {
return e
}

s.merge(obj1, obj2)
JsonMap := make(map[string]string)

JsonMap[fmt.Sprintf("system.api.desc.add%s%s", info.Package, info.StructName)] = fmt.Sprintf("%s%s", "add", info.Description)
JsonMap[fmt.Sprintf("system.api.desc.delete%s%s", info.Package, info.StructName)] = fmt.Sprintf("%s%s", "delete", info.Description)
JsonMap[fmt.Sprintf("system.api.desc.batch%s%s", info.Package, info.StructName)] = fmt.Sprintf("%s%s", "batch", info.Description)
JsonMap[fmt.Sprintf("system.api.desc.update%s%s", info.Package, info.StructName)] = fmt.Sprintf("%s%s", "update", info.Description)
JsonMap[fmt.Sprintf("system.api.desc.find%s%s", info.Package, info.StructName)] = fmt.Sprintf("%s%s", "find", info.Description)
JsonMap[fmt.Sprintf("system.api.desc.list%s%s", info.Package, info.StructName)] = fmt.Sprintf("%s%s", "list", info.Description)
JsonMap[fmt.Sprintf("system.api.group.%s%s", info.Package, info.StructName)] = info.Description

var je error
for key, value := range JsonMap {
data, je = sjson.SetBytes(data, key, value)
if je != nil {
return je
}
}

mergedJSON, err := json.MarshalIndent(obj1, "", " ")
if err != nil {
return "", err
err = os.WriteFile(file, data, 0666)
if err != nil {
return err
}
}

return string(mergedJSON), nil
return nil
}

func (s *autoCodeTemplate) merge(dst, src map[string]interface{}) {
for key, value := range src {
if dstValue, ok := dst[key]; ok {
if dstMap, ok := dstValue.(map[string]interface{}); ok {
if srcMap, ok := value.(map[string]interface{}); ok {
s.merge(dstMap, srcMap)
continue
}
func (s *autoCodeTemplate) WriteWebJson(info request.AutoCode) error {
localPath := path.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Web, "locales")
jsonFiles, err := s.readJSONFiles(localPath)
if err != nil {
return err
}
for _, file := range jsonFiles {
data, e := os.ReadFile(file)
if e != nil {
return e
}

var je error
for _, field := range info.Fields {
data, je = sjson.SetBytes(data, fmt.Sprintf("%s.%s.%s", info.Package, info.StructName, field.FieldName), field.FieldDesc)
if je != nil {
return je
}
}
dst[key] = value

err = os.WriteFile(file, data, 0666)
if err != nil {
return err
}
}
return nil
}

func (s *autoCodeTemplate) readJSONFiles(dir string) ([]string, error) {
Expand All @@ -517,103 +531,3 @@ func (s *autoCodeTemplate) readJSONFiles(dir string) ([]string, error) {
}
return files, nil
}

func (s *autoCodeTemplate) createJson(info request.AutoCode) (string, error) {
// 创建一个新的map来表示你想要的JSON结构
data := make(map[string]interface{})
fieldsData := make(map[string]interface{})
for _, field := range info.Fields {
fieldsData[field.FieldName] = field.FieldDesc
}
data[info.Package] = map[string]interface{}{
info.StructName: fieldsData,
}

// 使用json.Marshal函数将这个map转换为JSON
jsonBytes, err := json.Marshal(data)
if err != nil {
return "", err
}

return string(jsonBytes), nil
}

func (s *autoCodeTemplate) getDescriptionJson(info request.AutoCode) (string, error) {
data := map[string]interface{}{
"system": map[string]interface{}{
"api": map[string]interface{}{
"desc": map[string]interface{}{
fmt.Sprintf("add%s%s", info.Package, info.StructName): fmt.Sprintf("%s%s", "add", info.Description),
fmt.Sprintf("delete%s%s", info.Package, info.StructName): fmt.Sprintf("%s%s", "delete", info.Description),
fmt.Sprintf("batch%s%s", info.Package, info.StructName): fmt.Sprintf("%s%s", "batch", info.Description),
fmt.Sprintf("update%s%s", info.Package, info.StructName): fmt.Sprintf("%s%s", "update", info.Description),
fmt.Sprintf("find%s%s", info.Package, info.StructName): fmt.Sprintf("%s%s", "find", info.Description),
fmt.Sprintf("list%s%s", info.Package, info.StructName): fmt.Sprintf("%s%s", "list", info.Description),
},
},
},
}

// 使用json.MarshalIndent函数将这个map转换为JSON
jsonBytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "", err
}

return string(jsonBytes), nil
}

func (s *autoCodeTemplate) getGroupJson(info request.AutoCode) (string, error) {
data := map[string]interface{}{
"system": map[string]interface{}{
"api": map[string]interface{}{
"group": map[string]interface{}{
fmt.Sprintf("%s%s", info.Package, info.StructName): info.Description,
},
},
},
}

// 使用json.MarshalIndent函数将这个map转换为JSON
jsonBytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "", err
}

return string(jsonBytes), nil
}

func (s *autoCodeTemplate) getMenuJson(info request.AutoCode) (string, error) {
data := map[string]interface{}{
"system": map[string]interface{}{
"menu": map[string]interface{}{
fmt.Sprintf("%s%s", info.Package, info.StructName): info.Description,
},
},
}

// 使用json.MarshalIndent函数将这个map转换为JSON
jsonBytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "", err
}

return string(jsonBytes), nil
}

func (s *autoCodeTemplate) reWriteI18nJson(file string, i18nJson string) error {
data, err := os.ReadFile(file)
if err != nil {
return err
}
originJson := string(data)
newJson, err := s.mergeJSON(originJson, i18nJson)
if err != nil {
return err
}
err = os.WriteFile(file, []byte(newJson), 0666)
if err != nil {
return err
}
return nil
}

0 comments on commit 5049476

Please sign in to comment.