github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgRpc/kmgRpcSwift/kmgRpcSwift.go (about) 1 package kmgRpcSwift 2 3 import ( 4 "github.com/bronze1man/kmg/kmgCache" 5 "github.com/bronze1man/kmg/kmgCmd" 6 "github.com/bronze1man/kmg/kmgConfig" 7 "github.com/bronze1man/kmg/kmgFile" 8 "github.com/bronze1man/kmg/kmgFileToXcode" 9 "github.com/bronze1man/kmg/kmgGoSource/kmgFormat" 10 "path/filepath" 11 "strings" 12 ) 13 14 type GenerateRequest struct { 15 ObjectPkgPath string 16 ObjectName string 17 ObjectIsPointer bool 18 OutFilePath string //输出的文件路径,请设置为项目路径下的项目名文件,便于文件写入和配置 如 client/INVE/INVE/demo.swift 19 OutClassName string // swift的类的名字 如 RpcDemo 20 OutProjectName string //eg:INVE 21 XcodeprojPath string //.xcodeproj 22 ApiNameFilterCb func(name string) bool 23 NeedSource bool 24 } 25 26 //生成代码 27 // 此处只生成swift代码,不生成golang代码. 28 // 限制: 输出只能有一个参数, 29 func MustGenerateCode(req *GenerateRequest) { 30 config := reflectToTplConfig(req) 31 outBs := tplGenerateCode(config) 32 outB := kmgFormat.RemoteEmptyLine([]byte(outBs)) 33 kmgFile.MustWriteFileWithMkdir(req.OutFilePath, outB) 34 if req.NeedSource { 35 //生成xxx-Bridging-Header.h NSData+Compression.h NSData+Compression.m 36 BridgingHeaderContent := ` 37 // 请将该文件放到根目录的项目名文件下 38 #import "NSData+Compression.h" 39 ` 40 path := strings.Split(req.OutFilePath, "/") 41 parPath := strings.Join(path[:(len(path)-1)], "/") + "/" 42 BridgingHeaderPath := parPath + req.OutProjectName + "-Bridging-Header.h" 43 NSDataCompressionMethodPath := parPath + "NSData+Compression.m" 44 NSDataCompressionHeadPath := parPath + "NSData+Compression.h" 45 InfoListPath := parPath + "Info.plist" 46 path = strings.Split(req.XcodeprojPath, "/") 47 projectPath := strings.Join(path[:(len(path)-1)], "/") + "/" 48 podFilePath := projectPath + "Podfile" 49 xcodeprojPath := req.XcodeprojPath 50 kmgFile.MustWriteFileWithMkdir(BridgingHeaderPath, []byte(BridgingHeaderContent)) 51 kmgFile.MustWriteFileWithMkdir(NSDataCompressionMethodPath, []byte(NSDataCompressionMethod())) 52 kmgFile.MustWriteFileWithMkdir(NSDataCompressionHeadPath, []byte(NSDataCompressionHead())) 53 kmgFile.MustWriteFileWithMkdir(InfoListPath, []byte(InfoList())) 54 kmgFile.MustWriteFileWithMkdir(podFilePath, []byte(Podfile(req.OutProjectName))) 55 kmgFileToXcode.AddFilesToXcode([]string{req.OutFilePath, BridgingHeaderPath, NSDataCompressionMethodPath, NSDataCompressionHeadPath}, xcodeprojPath) 56 cmd := kmgCmd.CmdBash("export LANG=UTF-8;pod install") 57 cmd.SetDir(projectPath) 58 cmd.MustRun() 59 } 60 } 61 62 type tplConfig struct { 63 ClassName string //类名称 如 RpcDemo 64 InnerClassList []*InnerClass //里面包含的类的类型定义的名称 包括rpc辅助类,如 xxxRequest 和 golang里面用户定义的struct. 65 innerClassMap map[string]*InnerClass 66 ApiList []Api //api列表 包括所有大写开头额Api名称 67 } 68 69 func (config *tplConfig) addInnerClass(class *InnerClass) { 70 _, ok := config.innerClassMap[class.Name] 71 if ok { 72 panic("InnerClass name repeat [" + class.Name + "]") 73 } 74 config.innerClassMap[class.Name] = class 75 config.InnerClassList = append(config.InnerClassList, class) 76 } 77 78 type Api struct { 79 Name string //在这个系统里面的名字 80 InArgsList []NameTypePair //输入变量列表 81 OutTypeString string // 有可能是void 82 OutTypeFieldName string // 输出的那个变量的在response里面的名字,如果没有表示直接返回response 83 } 84 85 func (api *Api) getClientFuncInParameter() string { 86 outputList := []string{} 87 for _, arg := range api.InArgsList { 88 outputList = append(outputList, arg.Name+": "+arg.TypeStr) 89 } 90 return strings.Join(outputList, ",") 91 } 92 93 type NameTypePair struct { 94 Name string 95 TypeStr string 96 } 97 98 type InnerClass struct { 99 Name string //此处只有一个层次的名称,如果原先有package会被直接灭掉. 100 FieldList []NameTypePair 101 IsPublic bool 102 } 103 104 // 使用缓存 生成代码 105 func MustGenerateCodeWithCache(req *GenerateRequest) { 106 pkgFilePath := kmgConfig.DefaultEnv().PathInProject(filepath.Join("src", req.ObjectPkgPath)) 107 kmgCache.MustMd5FileChangeCache("kmgRpc_"+req.OutFilePath, []string{req.OutFilePath, pkgFilePath}, func() { 108 MustGenerateCode(req) 109 }) 110 }