github.com/turingchain2020/turingchain@v1.1.21/cmd/tools/tasks/gen_dapp_code_task.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package tasks 6 7 import ( 8 "fmt" 9 "os" 10 "path/filepath" 11 "strings" 12 13 "github.com/turingchain2020/turingchain/cmd/tools/gencode" 14 "github.com/turingchain2020/turingchain/cmd/tools/types" 15 util2 "github.com/turingchain2020/turingchain/util" 16 17 "github.com/turingchain2020/turingchain/cmd/tools/util" 18 ) 19 20 // GenDappCodeTask 通过生成好的pb.go和预先设计的模板,生成反射程序源码 21 type GenDappCodeTask struct { 22 TaskBase 23 DappName string 24 DappDir string 25 ProtoFile string 26 PackagePath string 27 replacePairs map[string]string 28 } 29 30 //GetName 获取name 31 func (c *GenDappCodeTask) GetName() string { 32 return "GenDappCodeTask" 33 } 34 35 //Execute 执行 36 func (c *GenDappCodeTask) Execute() error { 37 mlog.Info("Execute generate dapp code task.") 38 39 c.replacePairs = make(map[string]string) 40 pbContext, err := util.ReadFile(c.ProtoFile) 41 if err != nil { 42 mlog.Error("ReadProtoFile", "Err", err.Error(), "proto", c.ProtoFile) 43 return fmt.Errorf("ReadProtoFileErr:%s", err.Error()) 44 } 45 46 pbContent := string(pbContext) 47 48 if err = c.calcReplacePairs(pbContent); err != nil { 49 mlog.Error("CalcReplacePairs", "Err", err.Error()) 50 return fmt.Errorf("CalcReplacePairsErr:%s", err.Error()) 51 } 52 53 if err = c.genDappCode(); err != nil { 54 return fmt.Errorf("GenDappCodeErr:%s", err.Error()) 55 } 56 57 return err 58 } 59 60 func (c *GenDappCodeTask) calcReplacePairs(pbContent string) error { 61 62 dapp := strings.ToLower(c.DappName) 63 className, _ := util2.MakeStringToUpper(dapp, 0, 1) 64 c.replacePairs[types.TagExecName] = dapp 65 c.replacePairs[types.TagExecObject] = dapp[:1] 66 c.replacePairs[types.TagClassName] = className 67 c.replacePairs[types.TagImportPath] = c.PackagePath 68 69 pbAppend := gencode.ProtoFileAppendService 70 if strings.Contains(pbContent, "service") { 71 pbAppend = "" 72 } 73 74 c.replacePairs[types.TagProtoFileContent] = pbContent 75 c.replacePairs[types.TagProtoFileAppend] = pbAppend 76 77 actionName := className + "Action" 78 actionInfos, err := readDappActionFromProto(pbContent, actionName) 79 80 if err != nil { 81 return fmt.Errorf("ReadProtoActionErr:%s", err.Error()) 82 } 83 84 //exec 85 c.replacePairs[types.TagExecFileContent] = formatExecContent(actionInfos, dapp) 86 c.replacePairs[types.TagExecLocalFileContent] = formatExecLocalContent(actionInfos, dapp) 87 c.replacePairs[types.TagExecDelLocalFileContent] = formatExecDelLocalContent(actionInfos, dapp) 88 89 //types 90 c.replacePairs[types.TagTyLogActionType] = buildActionLogTypeText(actionInfos, className) 91 c.replacePairs[types.TagActionIDText] = buildActionIDText(actionInfos, className) 92 c.replacePairs[types.TagLogMapText] = buildLogMapText() 93 c.replacePairs[types.TagTypeMapText] = buildTypeMapText(actionInfos, className) 94 95 return nil 96 97 } 98 99 func (c *GenDappCodeTask) genDappCode() error { 100 101 codeTypes := gencode.GetCodeFilesWithType("dapp") 102 103 for _, code := range codeTypes { 104 105 dirName := code.GetDirName() 106 for _, tag := range code.GetDirReplaceTags() { 107 dirName = strings.Replace(dirName, tag, c.replacePairs[tag], -1) 108 } 109 dirPath := filepath.Join(c.DappDir, dirName) 110 err := os.MkdirAll(dirPath, os.ModePerm) 111 if err != nil { 112 mlog.Error("MakeCodeDir", "Err", err.Error(), "DirPath", dirPath) 113 return err 114 } 115 files := code.GetFiles() 116 tags := code.GetFileReplaceTags() 117 118 for name, content := range files { 119 120 for _, tag := range tags { 121 name = strings.Replace(name, tag, c.replacePairs[tag], -1) 122 content = strings.Replace(content, tag, c.replacePairs[tag], -1) 123 } 124 125 _, err = util.WriteStringToFile(filepath.Join(dirPath, name), content) 126 127 if err != nil { 128 mlog.Error("GenNewCodeFile", "Err", err.Error(), "CodeFile", filepath.Join(dirPath, name)) 129 return err 130 } 131 } 132 133 } 134 135 return nil 136 }