|
| 1 | +// Copyright 2021 gotomicro |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package daocmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "github.com/gotomicro/egen/internal/generate" |
| 21 | + "github.com/gotomicro/egen/internal/model" |
| 22 | + "github.com/gotomicro/egen/internal/model/ast" |
| 23 | + "github.com/gotomicro/egen/internal/utils" |
| 24 | + "os" |
| 25 | + "path/filepath" |
| 26 | + "strings" |
| 27 | +) |
| 28 | + |
| 29 | +func execWrite(src, dst, name, path string) error { |
| 30 | + var ( |
| 31 | + dstDir = utils.IsDir(dst) |
| 32 | + srcDir = utils.IsDir(src) |
| 33 | + ) |
| 34 | + |
| 35 | + if !dstDir && name == allModel { |
| 36 | + return errors.New("-dst 应该是一个目录,或者使用 -type 指定了单个类型") |
| 37 | + } else if srcDir && !dstDir && name == allModel { |
| 38 | + return errors.New("-src为目录的情况下-dst也应为目录 或者使用 -type指定了单个类型") |
| 39 | + } |
| 40 | + |
| 41 | + srcFiles := make([]string, 0, 10) |
| 42 | + if srcDir { |
| 43 | + files, err := os.ReadDir(src) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + for _, file := range files { |
| 48 | + if strings.HasSuffix(file.Name(), ".go") { |
| 49 | + src, err = filepath.Abs(src) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + srcFiles = append(srcFiles, src+"/"+file.Name()) |
| 54 | + } |
| 55 | + } |
| 56 | + } else { |
| 57 | + src, err := filepath.Abs(src) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + srcFiles = append(srcFiles, src) |
| 62 | + } |
| 63 | + |
| 64 | + models := make([]model.Model, 0, len(srcFiles)) |
| 65 | + for _, name := range srcFiles { |
| 66 | + models = append(models, ast.ParseModel(ast.LookUp(name, nil), model.WithImports(path))...) |
| 67 | + } |
| 68 | + |
| 69 | + return WriteToFile(models, dst, name) |
| 70 | +} |
| 71 | + |
| 72 | +func WriteToFile(models []model.Model, dst, name string) error { |
| 73 | + var mg generate.MySQLGenerator |
| 74 | + for _, v := range models { |
| 75 | + if name != "" && v.GoName != name { |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + if utils.IsDir(dst) { |
| 80 | + // 可能要对多个文件进行写入 写入完成后直接close |
| 81 | + f, err := os.Create(dst + fmt.Sprintf("/%s_dao.go", ast.Convert(v.TableName))) |
| 82 | + if err != nil { |
| 83 | + f.Close() // 防止内存泄露 |
| 84 | + return err |
| 85 | + } |
| 86 | + if err = mg.Generate(v, f); err != nil { |
| 87 | + f.Close() // 防止内存泄露 |
| 88 | + return err |
| 89 | + } |
| 90 | + f.Close() |
| 91 | + fmt.Println(f.Name(), "已完成") |
| 92 | + } else { |
| 93 | + f, err := os.Create(dst) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + if err = mg.Generate(v, f); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + f.Close() // 只有单个文件进行写入 直接defer |
| 102 | + fmt.Println(f.Name(), "已完成") |
| 103 | + } |
| 104 | + } |
| 105 | + return nil |
| 106 | +} |
0 commit comments