github.com/turingchain2020/turingchain@v1.1.21/cmd/tools/strategy/gendapp.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 strategy 6 7 import ( 8 "fmt" 9 "os" 10 "path/filepath" 11 "strings" 12 13 "github.com/turingchain2020/turingchain/cmd/tools/tasks" 14 "github.com/turingchain2020/turingchain/cmd/tools/types" 15 "github.com/turingchain2020/turingchain/cmd/tools/util" 16 ) 17 18 type genDappStrategy struct { 19 strategyBasic 20 21 dappName string 22 dappDir string 23 dappProto string 24 packagePath string 25 } 26 27 func (ad *genDappStrategy) Run() error { 28 fmt.Println("Begin generate turingchain dapp code.") 29 defer fmt.Println("End generate turingchain dapp code.") 30 if !ad.initMember() { 31 return fmt.Errorf("InitError") 32 } 33 34 return ad.runImpl() 35 } 36 37 func (ad *genDappStrategy) checkParamValid() bool { 38 return true 39 } 40 41 func (ad *genDappStrategy) initMember() bool { 42 43 dappName, _ := ad.getParam(types.KeyExecutorName) 44 outDir, _ := ad.getParam(types.KeyDappOutDir) 45 protoPath, _ := ad.getParam(types.KeyProtobufFile) 46 //统一转为小写字母 47 dappName = strings.ToLower(dappName) 48 49 if strings.Contains(dappName, " ") { 50 mlog.Error("InitGenDapp", "Err", "invalid dapp name", "name", dappName) 51 return false 52 } 53 54 goPath := os.Getenv("GOPATH") 55 if goPath == "" { 56 mlog.Error("InitGenDapp", "Err", "$GOPATH not exist") 57 return false 58 } 59 60 // 默认输出到plugin项目的plugin/dapp/目录下 61 if outDir == "" { 62 outDir = filepath.Join("github.com", "turingchain2020", "plugin", "plugin", "dapp") 63 } 64 //兼容win 反斜杠路径 65 packPath := strings.Replace(filepath.Join(outDir), string(filepath.Separator), "/", -1) 66 //绝对路径 67 dappRootDir := filepath.Join(goPath, "src", outDir, dappName) 68 //check dapp output directory exist 69 if util.CheckPathExisted(dappRootDir) { 70 mlog.Error("InitGenDapp", "Err", "generate dapp directory exist", "Dir", dappRootDir) 71 return false 72 } 73 74 if protoPath != "" { 75 bExist, _ := util.CheckFileExists(protoPath) 76 if !bExist { 77 mlog.Error("InitGenDapp", "Err", "specified proto file not exist", "ProtoFile", protoPath) 78 return false 79 } 80 } 81 82 err := os.MkdirAll(dappRootDir, os.ModePerm) 83 84 if err != nil { 85 mlog.Error("GenDappDir", "Err", err, "dir", dappRootDir) 86 return false 87 } 88 89 ad.dappName = dappName 90 ad.dappDir = dappRootDir 91 ad.dappProto = protoPath 92 ad.packagePath = packPath 93 94 return true 95 } 96 97 func (ad *genDappStrategy) runImpl() error { 98 var err error 99 tashSlice := ad.buildTask() 100 for _, task := range tashSlice { 101 102 err = task.Execute() 103 if err != nil { 104 mlog.Error("GenDappExecTaskFailed.", "error", err, "taskname", task.GetName()) 105 break 106 } 107 } 108 return err 109 } 110 111 func (ad *genDappStrategy) buildTask() []tasks.Task { 112 taskSlice := make([]tasks.Task, 0) 113 taskSlice = append(taskSlice, 114 115 &tasks.GenDappCodeTask{ 116 DappName: ad.dappName, 117 DappDir: ad.dappDir, 118 ProtoFile: ad.dappProto, 119 PackagePath: ad.packagePath, 120 }, 121 &tasks.FormatDappSourceTask{ 122 OutputFolder: ad.dappDir, 123 }, 124 ) 125 return taskSlice 126 }